// Override #set to also set the global variable
this.set = function ( selection, value ) {
var s;
-
- if ( $.isPlainObject( selection ) ) {
- for ( s in selection ) {
- setGlobalMapValue( this, s, selection[ s ] );
+ if ( arguments.length > 1 ) {
+ if ( typeof selection !== 'string' ) {
+ return false;
}
+ setGlobalMapValue( this, selection, value );
return true;
}
- if ( typeof selection === 'string' && arguments.length ) {
- setGlobalMapValue( this, selection, value );
+ if ( typeof selection === 'object' ) {
+ for ( s in selection ) {
+ setGlobalMapValue( this, s, selection[ s ] );
+ }
return true;
}
return false;
*/
set: function ( selection, value ) {
var s;
-
- if ( $.isPlainObject( selection ) ) {
- for ( s in selection ) {
- this.values[ s ] = selection[ s ];
+ // Use `arguments.length` because `undefined` is also a valid value.
+ if ( arguments.length > 1 ) {
+ if ( typeof selection !== 'string' ) {
+ return false;
}
+ this.values[ selection ] = value;
return true;
}
- if ( typeof selection === 'string' && arguments.length > 1 ) {
- this.values[ selection ] = value;
+ if ( typeof selection === 'object' ) {
+ for ( s in selection ) {
+ this.values[ s ] = selection[ s ];
+ }
return true;
}
return false;
*
* @private
* @param {string} src URL to script, will be used as the src attribute in the script tag
- * @return {jQuery.Promise}
+ * @param {Function} [callback] Callback to run after request resolution
*/
- function addScript( src ) {
- return $.ajax( {
- url: src,
- dataType: 'script',
- // 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 retrieve
- // text, so we'd need to $.globalEval, which then messes up line numbers.
- crossDomain: true,
- cache: true
- } );
+ function addScript( src, callback ) {
+ var script = document.createElement( 'script' );
+ script.src = src;
+ script.onload = script.onerror = function () {
+ if ( script.parentNode ) {
+ script.parentNode.removeChild( script );
+ }
+ script = null;
+ if ( callback ) {
+ callback();
+ callback = null;
+ }
+ };
+ document.head.appendChild( script );
}
/**
*
* @private
* @param {string} src URL of the script
- * @param {string} [moduleName] Name of currently executing module
- * @return {jQuery.Promise}
+ * @param {string} moduleName Name of currently executing module
+ * @param {Function} callback Callback to run after addScript() resolution
*/
- function queueModuleScript( src, moduleName ) {
- var r = $.Deferred();
-
+ function queueModuleScript( src, moduleName, callback ) {
pendingRequests.push( function () {
- if ( moduleName && hasOwn.call( registry, moduleName ) ) {
+ if ( hasOwn.call( registry, moduleName ) ) {
// Emulate runScript() part of execute()
window.require = mw.loader.require;
window.module = registry[ moduleName ].module;
}
- addScript( src ).always( function () {
+ addScript( src, function () {
// 'module.exports' should not persist after the file is executed to
// avoid leakage to unrelated code. 'require' should be kept, however,
// as asynchronous access to 'require' is allowed and expected. (T144879)
delete window.module;
- r.resolve();
-
+ callback();
// Start the next one (if any)
if ( pendingRequests[ 0 ] ) {
pendingRequests.shift()();
handlingPendingRequests = true;
pendingRequests.shift()();
}
- return r.promise();
}
/**
return;
}
- queueModuleScript( arr[ i ], module ).always( function () {
+ queueModuleScript( arr[ i ], module, function () {
nestedAddScript( arr, callback, i + 1 );
} );
};