function setGlobalMapValue( map, key, value ) {
map.values[ key ] = value;
log.deprecate(
- window,
- key,
- value,
- // Deprecation notice for mw.config globals (T58550, T72470)
- map === mw.config && 'Use mw.config instead.'
+ window,
+ key,
+ value,
+ // Deprecation notice for mw.config globals (T58550, T72470)
+ map === mw.config && 'Use mw.config instead.'
);
}
* @param {Function} callback
*/
trackUnsubscribe: function ( callback ) {
- trackHandlers = $.grep( trackHandlers, function ( fns ) {
+ trackHandlers = trackHandlers.filter( function ( fns ) {
if ( fns[ 1 ] === callback ) {
trackCallbacks.remove( fns[ 0 ] );
// Ensure the tuple is removed to avoid holding on to closures
* is used)
* - load-callback: exception thrown by user callback
* - module-execute: exception thrown by module code
+ * - resolve: failed to sort dependencies for a module in mw.loader.load
* - store-eval: could not evaluate module code cached in localStorage
* - store-localstorage-init: localStorage or JSON parse error in mw.loader.store.init
* - store-localstorage-json: JSON conversion error in mw.loader.store.set
registry[ module ].dependencies = [ registry[ module ].dependencies ];
}
}
- if ( $.inArray( module, resolved ) !== -1 ) {
+ if ( resolved.indexOf( module ) !== -1 ) {
// Module already resolved; nothing to do
return;
}
// Tracks down dependencies
deps = registry[ module ].dependencies;
for ( i = 0; i < deps.length; i++ ) {
- if ( $.inArray( deps[ i ], resolved ) === -1 ) {
+ if ( resolved.indexOf( deps[ i ] ) === -1 ) {
if ( unresolved.has( deps[ i ] ) ) {
throw new Error( mw.format(
'Circular reference detected: $1 -> $2',
return resolved;
}
+ /**
+ * Like #resolve(), except it will silently ignore modules that
+ * are missing or have missing dependencies.
+ *
+ * @private
+ * @param {string[]} modules Array of string module names
+ * @return {Array} List of dependencies.
+ */
+ function resolveStubbornly( modules ) {
+ var i, saved, resolved = [];
+ for ( i = 0; i < modules.length; i++ ) {
+ saved = resolved.slice();
+ try {
+ sortDependencies( modules[ i ], resolved );
+ } catch ( err ) {
+ // This module is unknown or has unknown dependencies.
+ // Undo any incomplete resolutions made and keep going.
+ resolved = saved;
+ mw.track( 'resourceloader.exception', {
+ exception: err,
+ source: 'resolve'
+ } );
+ }
+ }
+ return resolved;
+ }
+
/**
* Load and execute a script.
*
registry[ module ].state = 'executing';
runScript = function () {
- var script, markModuleReady, nestedAddScript, implicitDependencies, implicitWait;
+ var script, markModuleReady, nestedAddScript;
script = registry[ module ].script;
markModuleReady = function () {
} );
};
- implicitDependencies = [];
-
- if ( module === 'user' ) {
- // Implicit dependency on the site module. Not real dependency because
- // it should run after 'site' regardless of whether it succeeds or fails.
- implicitDependencies.push( 'site' );
- }
-
- implicitWait = implicitDependencies.length ?
- mw.loader.using( implicitDependencies ) :
- $.Deferred().resolve();
-
- implicitWait.always( function () {
- try {
- if ( Array.isArray( script ) ) {
- nestedAddScript( script, markModuleReady, 0 );
- } else if ( typeof script === 'function' ) {
- // Pass jQuery twice so that the signature of the closure which wraps
- // the script can bind both '$' and 'jQuery'.
- script( $, $, mw.loader.require, registry[ module ].module );
- markModuleReady();
-
- } else if ( typeof script === 'string' ) {
- // Site and user modules are legacy scripts that run in the global scope.
- // This is transported as a string instead of a function to avoid needing
- // to use string manipulation to undo the function wrapper.
- $.globalEval( script );
- markModuleReady();
+ try {
+ if ( Array.isArray( script ) ) {
+ nestedAddScript( script, markModuleReady, 0 );
+ } else if ( typeof script === 'function' ) {
+ // Pass jQuery twice so that the signature of the closure which wraps
+ // the script can bind both '$' and 'jQuery'.
+ script( $, $, mw.loader.require, registry[ module ].module );
+ markModuleReady();
+
+ } else if ( typeof script === 'string' ) {
+ // Site and user modules are legacy scripts that run in the global scope.
+ // This is transported as a string instead of a function to avoid needing
+ // to use string manipulation to undo the function wrapper.
+ $.globalEval( script );
+ markModuleReady();
- } else {
- // Module without script
- markModuleReady();
- }
- } catch ( e ) {
- // Use mw.track instead of mw.log because these errors are common in production mode
- // (e.g. undefined variable), and mw.log is only enabled in debug mode.
- registry[ module ].state = 'error';
- mw.track( 'resourceloader.exception', { exception: e, module: module, source: 'module-execute' } );
- handlePending( module );
+ } else {
+ // Module without script
+ markModuleReady();
}
- } );
+ } catch ( e ) {
+ // Use mw.track instead of mw.log because these errors are common in production mode
+ // (e.g. undefined variable), and mw.log is only enabled in debug mode.
+ registry[ module ].state = 'error';
+ mw.track( 'resourceloader.exception', { exception: e, module: module, source: 'module-execute' } );
+ handlePending( module );
+ }
};
// Add localizations to message system
// cssHandlesRegistered ensures we don't take off too soon, e.g. when
// one of the cssHandles is fired while we're still creating more handles.
if ( cssHandlesRegistered && pending === 0 && runScript ) {
- runScript();
+ if ( module === 'user' ) {
+ // Implicit dependency on the site module. Not real dependency because
+ // it should run after 'site' regardless of whether it succeeds or fails.
+ mw.loader.using( [ 'site' ] ).always( runScript );
+ } else {
+ runScript();
+ }
runScript = undefined; // Revoke
}
};
if ( ready !== undefined || error !== undefined ) {
jobs.push( {
// Narrow down the list to modules that are worth waiting for
- dependencies: $.grep( dependencies, function ( module ) {
+ dependencies: dependencies.filter( function ( module ) {
var state = mw.loader.getState( module );
return state === 'registered' || state === 'loaded' || state === 'loading' || state === 'executing';
} ),
} );
}
- $.each( dependencies, function ( idx, module ) {
+ dependencies.forEach( function ( module ) {
var state = mw.loader.getState( module );
// Only queue modules that are still in the initial 'registered' state
// (not ones already loading, ready or error).
- if ( state === 'registered' && $.inArray( module, queue ) === -1 ) {
+ if ( state === 'registered' && queue.indexOf( module ) === -1 ) {
// Private modules must be embedded in the page. Don't bother queuing
// these as the server will deny them anyway (T101806).
if ( registry[ module ].group === 'private' ) {
a = [];
for ( key in o ) {
- if ( hasOwn.call( o, key ) ) {
- a.push( key );
- }
+ a.push( key );
}
a.sort();
for ( key = 0; key < a.length; key++ ) {
* @param {string} sourceLoadScript URL of load.php
*/
function doRequest( moduleMap, currReqBase, sourceLoadScript ) {
- var query = $.extend(
- { modules: buildModulesString( moduleMap ) },
- currReqBase
- );
+ // Optimisation: Inherit (Object.create), not copy ($.extend)
+ var query = Object.create( currReqBase );
+ query.modules = buildModulesString( moduleMap );
query = sortQuery( query );
addScript( sourceLoadScript + '?' + $.param( query ) );
}
// modules for this group from this source.
modules = splits[ source ][ group ];
- currReqBase = $.extend( {
- version: getCombinedVersion( modules )
- }, reqBase );
+ // Optimisation: Inherit (Object.create), not copy ($.extend)
+ currReqBase = Object.create( reqBase );
+ currReqBase.version = getCombinedVersion( modules );
+
// For user modules append a user name to the query string.
if ( group === 'user' && mw.config.get( 'wgUserName' ) !== null ) {
currReqBase.user = mw.config.get( 'wgUserName' );
// Only load modules which are registered
if ( hasOwn.call( registry, queue[ q ] ) && registry[ queue[ q ] ].state === 'registered' ) {
// Prevent duplicate entries
- if ( $.inArray( queue[ q ], batch ) === -1 ) {
+ if ( batch.indexOf( queue[ q ] ) === -1 ) {
batch.push( queue[ q ] );
// Mark registered modules as loading
registry[ queue[ q ] ].state = 'loading';
if ( mw.loader.store.enabled ) {
implementations = [];
sourceModules = [];
- batch = $.grep( batch, function ( module ) {
+ batch = batch.filter( function ( module ) {
var implementation = mw.loader.store.get( module );
if ( implementation ) {
implementations.push( implementation );
mw.track( 'resourceloader.exception', { exception: err, source: 'store-eval' } );
// Re-add the failed ones that are still pending back to the batch
- failed = $.grep( sourceModules, function ( module ) {
+ failed = sourceModules.filter( function ( module ) {
return registry[ module ].state === 'loading';
} );
batchRequest( failed );
registry[ name ].messages = messages || null;
registry[ name ].templates = templates || null;
// The module may already have been marked as erroneous
- if ( $.inArray( registry[ name ].state, [ 'error', 'missing' ] ) === -1 ) {
+ if ( registry[ name ].state !== 'error' && registry[ name ].state !== 'missing' ) {
registry[ name ].state = 'loaded';
if ( allReady( registry[ name ].dependencies ) ) {
execute( name );
/**
* Load an external script or one or more modules.
*
+ * This method takes a list of unrelated modules. Use cases:
+ *
+ * - A web page will be composed of many different widgets. These widgets independently
+ * queue their ResourceLoader modules (`OutputPage::addModules()`). If any of them
+ * have problems, or are no longer known (e.g. cached HTML), the other modules
+ * should still be loaded.
+ * - This method is used for preloading, which must not throw. Later code that
+ * calls #using() will handle the error.
+ *
* @param {string|Array} modules Either the name of a module, array of modules,
* or a URL of an external script or style
* @param {string} [type='text/javascript'] MIME type to use if calling with a URL of an
modules = [ modules ];
}
- // Filter out undefined modules, otherwise resolve() will throw
- // an exception for trying to load an undefined module.
- // Undefined modules are acceptable here in load(), because load() takes
- // an array of unrelated modules, whereas the modules passed to
- // using() are related and must all be loaded.
- filtered = $.grep( modules, function ( module ) {
+ // Filter out top-level modules that are unknown or failed to load before.
+ filtered = modules.filter( function ( module ) {
var state = mw.loader.getState( module );
- return state !== null && state !== 'error' && state !== 'missing';
+ return state !== 'error' && state !== 'missing';
} );
-
- if ( filtered.length === 0 ) {
- return;
- }
- // Resolve entire dependency map
- filtered = resolve( filtered );
+ // Resolve remaining list using the known dependency tree.
+ // This also filters out modules with unknown dependencies. (T36853)
+ filtered = resolveStubbornly( filtered );
// If all modules are ready, or if any modules have errors, nothing to be done.
if ( allReady( filtered ) || anyFailed( filtered ) ) {
return;
}
+ if ( filtered.length === 0 ) {
+ return;
+ }
// Some modules are not yet ready, add to module load queue.
enqueue( filtered, undefined, undefined );
},
mw.loader.register( module );
}
registry[ module ].state = state;
- if ( $.inArray( state, [ 'ready', 'error', 'missing' ] ) !== -1 ) {
+ if ( state === 'ready' || state === 'error' || state === 'missing' ) {
// Make sure pending modules depending on this one get executed if their
// dependencies are now fulfilled!
handlePending( module );
* in the registry.
*/
getVersion: function ( module ) {
- if ( !hasOwn.call( registry, module ) || registry[ module ].version === undefined ) {
- return null;
- }
- return registry[ module ].version;
+ return hasOwn.call( registry, module ) ? registry[ module ].version : null;
},
/**
* in the registry.
*/
getState: function ( module ) {
- if ( !hasOwn.call( registry, module ) || registry[ module ].state === undefined ) {
- return null;
- }
- return registry[ module ].state;
+ return hasOwn.call( registry, module ) ? registry[ module ].state : null;
},
/**
// Module failed to load
descriptor.state !== 'ready' ||
// Unversioned, private, or site-/user-specific
- ( !descriptor.version || $.inArray( descriptor.group, [ 'private', 'user' ] ) !== -1 ) ||
+ !descriptor.version ||
+ descriptor.group === 'private' ||
+ descriptor.group === 'user' ||
// Partial descriptor
// (e.g. skipped module, or style module with state=ready)
- $.inArray( undefined, [ descriptor.script, descriptor.style,
- descriptor.messages, descriptor.templates ] ) !== -1
+ [ descriptor.script, descriptor.style, descriptor.messages,
+ descriptor.templates ].indexOf( undefined ) !== -1
) {
// Decline to store
return false;
msg += ( e ? ':' : '.' );
console.log( msg );
- // If we have an exception object, log it to the error channel to trigger
- // proper stacktraces in browsers that support it. No fallback as we have
- // no browsers that don't support error(), but do support log().
- if ( e && console.error ) {
- console.error( String( e ), e );
+ // If we have an exception object, log it to the warning channel to trigger
+ // proper stacktraces in browsers that support it.
+ if ( e && console.warn ) {
+ console.warn( String( e ), e );
}
}
/* eslint-enable no-console */
$( function () {
var loading, modules;
- modules = $.grep( mw.loader.getModuleNames(), function ( module ) {
+ modules = mw.loader.getModuleNames().filter( function ( module ) {
return mw.loader.getState( module ) === 'loading';
} );
// We only need a callback, not any actual module. First try a single using()