cssBuffer = '',
cssBufferTimer = null,
cssCallbacks = $.Callbacks(),
- isIE9 = document.documentMode === 9;
+ isIE9 = document.documentMode === 9,
+ rAF = window.requestAnimationFrame || setTimeout;
function getMarker() {
if ( !marker ) {
if ( !cssBuffer || cssText.slice( 0, '@import'.length ) !== '@import' ) {
// Linebreak for somewhat distinguishable sections
cssBuffer += '\n' + cssText;
- // TODO: Using requestAnimationFrame would perform better by not injecting
- // styles while the browser is busy painting.
if ( !cssBufferTimer ) {
- cssBufferTimer = setTimeout( function () {
+ cssBufferTimer = rAF( function () {
+ // Wrap in anonymous function that takes no arguments
// Support: Firefox < 13
// Firefox 12 has non-standard behaviour of passing a number
// as first argument to a setTimeout callback.
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' ) {
// Force jQuery behaviour to be for crossDomain. Otherwise jQuery would use
// XHR for a same domain request instead of <script>, which changes the request
// headers (potentially missing a cache hit), and reduces caching in general
- // since browsers cache XHR much less (if at all). And XHR means we retreive
+ // since browsers cache XHR much less (if at all). And XHR means we retrieve
// text, so we'd need to $.globalEval, which then messes up line numbers.
crossDomain: true,
cache: true
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 ] );
+ }
}
- } );
+ }
}
/**
}
}
+ /**
+ * Evaluate a batch of load.php responses retrieved from mw.loader.store.
+ *
+ * @private
+ * @param {string[]} implementations Array containing pieces of JavaScript code in the
+ * form of calls to mw.loader#implement().
+ * @param {Function} cb Callback in case of failure
+ * @param {Error} cb.err
+ */
+ function batchEval( implementations, cb ) {
+ if ( !implementations.length ) {
+ return;
+ }
+ mw.requestIdleCallback( function iterate( deadline ) {
+ while ( implementations[ 0 ] && deadline.timeRemaining() > 5 ) {
+ try {
+ $.globalEval( implementations.shift() );
+ } catch ( err ) {
+ cb( err );
+ return;
+ }
+ }
+ if ( implementations[ 0 ] ) {
+ mw.requestIdleCallback( iterate );
+ }
+ } );
+ }
+
/* Public Members */
return {
/**
* @protected
*/
work: function () {
- var q, batch, concatSource, origBatch;
+ var q, batch, implementations, sourceModules;
batch = [];
}
}
+ // Now that the queue has been processed into a batch, clear the queue.
+ // This MUST happen before we initiate any eval or network request. Otherwise,
+ // it is possible for a cached script to instantly trigger the same work queue
+ // again; all before we've cleared it causing each request to include modules
+ // which are already loaded.
+ queue = [];
+
+ if ( !batch.length ) {
+ return;
+ }
+
mw.loader.store.init();
if ( mw.loader.store.enabled ) {
- concatSource = [];
- origBatch = batch;
+ implementations = [];
+ sourceModules = [];
batch = $.grep( batch, function ( module ) {
- var source = mw.loader.store.get( module );
- if ( source ) {
- concatSource.push( source );
+ var implementation = mw.loader.store.get( module );
+ if ( implementation ) {
+ implementations.push( implementation );
+ sourceModules.push( module );
return false;
}
return true;
} );
- try {
- $.globalEval( concatSource.join( ';' ) );
- } catch ( err ) {
+ batchEval( implementations, function ( err ) {
// Not good, the cached mw.loader.implement calls failed! This should
// never happen, barring ResourceLoader bugs, browser bugs and PEBKACs.
// Depending on how corrupt the string is, it is likely that some
// modules' implement() succeeded while the ones after the error will
// never run and leave their modules in the 'loading' state forever.
-
// Since this is an error not caused by an individual module but by
// something that infected the implement call itself, don't take any
// risks and clear everything in this cache.
mw.loader.store.clear();
- // Re-add the ones still pending back to the batch and let the server
- // repopulate these modules to the cache.
- // This means that at most one module will be useless (the one that had
- // the error) instead of all of them.
mw.track( 'resourceloader.exception', { exception: err, source: 'store-eval' } );
- origBatch = $.grep( origBatch, function ( module ) {
+
+ // Re-add the failed ones that are still pending back to the batch
+ var failed = $.grep( sourceModules, function ( module ) {
return registry[ module ].state === 'loading';
} );
- batch = batch.concat( origBatch );
- }
+ batchRequest( failed );
+ } );
}
- // Now that the queue has been processed into a batch, clear up the queue.
- // This MUST happen before we initiate any network request. Else it's possible
- // that a script will be locally cached, instantly load, and work the queue
- // again; all before we've cleared it causing each request to include modules
- // which are already loaded.
- queue = [];
-
batchRequest( batch );
},
* @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
) {
var loading = $.grep( mw.loader.getModuleNames(), function ( module ) {
return mw.loader.getState( module ) === 'loading';
} );
- // In order to use jQuery.when (which stops early if one of the promises got rejected)
- // cast any loading failures into successes. We only need a callback, not the module.
- loading = $.map( loading, function ( module ) {
- return mw.loader.using( module ).then( null, function () {
- return $.Deferred().resolve();
+ // We only need a callback, not any actual module. First try a single using()
+ // for all loading modules. If one fails, fall back to tracking each module
+ // separately via $.when(), this is expensive.
+ loading = mw.loader.using( loading ).then( null, function () {
+ var all = $.map( loading, function ( module ) {
+ return mw.loader.using( module ).then( null, function () {
+ return $.Deferred().resolve();
+ } );
} );
+ return $.when.apply( $, all );
} );
- $.when.apply( $, loading ).then( function () {
+ loading.then( function () {
mwPerformance.mark( 'mwLoadEnd' );
mw.hook( 'resourceloader.loadEnd' ).fire();
} );