j -= 1;
try {
if ( hasErrors ) {
- if ( $.isFunction( job.error ) ) {
+ if ( typeof job.error === 'function' ) {
job.error( new Error( 'Module ' + module + ' has failed dependencies' ), [ module ] );
}
} else {
- if ( $.isFunction( job.ready ) ) {
+ if ( typeof job.ready === 'function' ) {
job.ready();
}
}
}
// Resolves dynamic loader function and replaces it with its own results
- if ( $.isFunction( registry[ module ].dependencies ) ) {
+ if ( typeof registry[ module ].dependencies === 'function' ) {
registry[ module ].dependencies = registry[ module ].dependencies();
// Ensures the module's dependencies are always in an array
if ( typeof registry[ module ].dependencies !== 'object' ) {
legacyWait.always( function () {
if ( $.isArray( script ) ) {
nestedAddScript( script, markModuleReady, 0 );
- } else if ( $.isFunction( script ) ) {
+ } 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 );
* @param {Array} modules Modules array
*/
function resolveIndexedDependencies( modules ) {
- $.each( modules, function ( idx, module ) {
- if ( module[ 2 ] ) {
- module[ 2 ] = $.map( module[ 2 ], function ( dep ) {
- return typeof dep === 'number' ? modules[ dep ][ 0 ] : dep;
- } );
+ var i, j, deps;
+ function resolveIndex( dep ) {
+ return typeof dep === 'number' ? modules[ dep ][ 0 ] : dep;
+ }
+ for ( i = 0; i < modules.length; i++ ) {
+ deps = modules[ i ][ 2 ];
+ if ( deps ) {
+ for ( j = 0; j < deps.length; j++ ) {
+ deps[ j ] = resolveIndex( deps[ j ] );
+ }
}
- } );
+ }
}
/**
* @param {string} [skip=null] Script body of the skip function
*/
register: function ( module, version, dependencies, group, source, skip ) {
- var i;
+ var i, deps;
// Allow multiple registration
if ( typeof module === 'object' ) {
resolveIndexedDependencies( module );
if ( hasOwn.call( registry, module ) ) {
throw new Error( 'module already registered: ' + module );
}
+ if ( typeof dependencies === 'string' ) {
+ // A single module name
+ deps = [ dependencies ];
+ } else if ( typeof dependencies === 'object' || typeof dependencies === 'function' ) {
+ // Array of module names or a function that returns an array
+ deps = dependencies;
+ }
// List the module as registered
registry[ module ] = {
// Exposed to execute() for mw.loader.implement() closures.
exports: {}
},
version: version !== undefined ? String( version ) : '',
- dependencies: [],
+ dependencies: deps || [],
group: typeof group === 'string' ? group : null,
source: typeof source === 'string' ? source : 'local',
state: 'registered',
skip: typeof skip === 'string' ? skip : null
};
- if ( typeof dependencies === 'string' ) {
- // Allow dependencies to be given as a single module name
- registry[ module ].dependencies = [ dependencies ];
- } else if ( typeof dependencies === 'object' || $.isFunction( dependencies ) ) {
- // Allow dependencies to be given as an array of module names
- // or a function which returns an array
- registry[ module ].dependencies = dependencies;
- }
},
/**
if ( !hasOwn.call( registry, module ) ) {
mw.loader.register( module );
}
- if ( $.inArray( state, [ 'ready', 'error', 'missing' ] ) !== -1
- && registry[ module ].state !== state ) {
+ registry[ module ].state = state;
+ if ( $.inArray( state, [ 'ready', 'error', 'missing' ] ) !== -1 ) {
// Make sure pending modules depending on this one get executed if their
// dependencies are now fulfilled!
- registry[ module ].state = state;
handlePending( module );
- } else {
- registry[ module ].state = state;
}
},
// Unversioned, private, or site-/user-specific
( !descriptor.version || $.inArray( descriptor.group, [ 'private', 'user' ] ) !== -1 ) ||
// Partial descriptor
+ // (e.g. skipped module, or style module with state=ready)
$.inArray( undefined, [ descriptor.script, descriptor.style,
descriptor.messages, descriptor.templates ] ) !== -1
) {