}
/**
- * Returns a JS call to mw.loader.state, which sets the state of a
- * module or modules to a given value. Has two calling conventions:
+ * Returns a JS call to mw.loader.state, which sets the state of one
+ * ore more modules to a given value. Has two calling conventions:
*
* - ResourceLoader::makeLoaderStateScript( $name, $state ):
* Set the state of a single module called $name to $state
* - ResourceLoader::makeLoaderStateScript( [ $name => $state, ... ] ):
* Set the state of modules with the given names to the given states
*
- * @param string $name
+ * @param array|string $states
* @param string|null $state
* @return string JavaScript code
*/
- public static function makeLoaderStateScript( $name, $state = null ) {
- if ( is_array( $name ) ) {
- return Xml::encodeJsCall(
- 'mw.loader.state',
- [ $name ],
- self::inDebugMode()
- );
- } else {
- return Xml::encodeJsCall(
- 'mw.loader.state',
- [ $name, $state ],
- self::inDebugMode()
- );
+ public static function makeLoaderStateScript( $states, $state = null ) {
+ if ( !is_array( $states ) ) {
+ $states = [ $states => $state ];
}
+ return Xml::encodeJsCall(
+ 'mw.loader.state',
+ [ $states ],
+ self::inDebugMode()
+ );
}
/**
/**
* Change the state of one or more modules.
*
- * @param {string|Object} module Module name or object of module name/state pairs
- * @param {string} state State name
+ * @param {Object|string} modules Object of module name/state pairs
*/
- state: function ( module, state ) {
- var m;
-
- if ( typeof module === 'object' ) {
- for ( m in module ) {
- mw.loader.state( m, module[ m ] );
+ state: function ( modules ) {
+ var module, state;
+ for ( module in modules ) {
+ state = modules[ module ];
+ if ( !hasOwn.call( registry, module ) ) {
+ mw.loader.register( module );
+ }
+ registry[ module ].state = state;
+ 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 );
}
- return;
- }
- if ( !hasOwn.call( registry, module ) ) {
- mw.loader.register( module );
- }
- registry[ module ].state = state;
- 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 );
}
},
. '} );';
} else {
// Default
- $response .= 'mw.loader.state(' . json_encode( $module ) . ', "missing" );' . "\n";
+ $response .= 'mw.loader.state(' . json_encode( [ $module => 'missing' ] ) . ');' . "\n";
}
}
}
assert.strictEqual( mw.loader.getState( 'test.module7' ), 'registered', 'Expected "registered" state for test.module7' );
assert.strictEqual( mw.loader.getState( 'test.module8' ), 'loaded', 'Expected "loaded" state for test.module8' );
assert.strictEqual( mw.loader.getState( 'test.module9' ), 'registered', 'Expected "registered" state for test.module9' );
- mw.loader.state( 'test.module7', 'missing' );
+ mw.loader.state( { 'test.module7': 'missing' } );
assert.strictEqual( mw.loader.getState( 'test.module7' ), 'missing', 'Expected "missing" state for test.module7' );
assert.strictEqual( mw.loader.getState( 'test.module8' ), 'error', 'Expected "error" state for test.module8' );
assert.strictEqual( mw.loader.getState( 'test.module9' ), 'error', 'Expected "error" state for test.module9' );
},
function ( e, badmodules ) {
assert.ok( true, 'Error handler should be invoked.' );
- // As soon as server spits out state('testMissing', 'missing');
+ // As soon as server sets state of 'testMissing' to 'missing'
// it will bubble up and trigger the error callback.
// Therefor the badmodules array is not testUsesMissing or testUsesNestedMissing.
assert.deepEqual( badmodules, [ 'testMissing' ], 'Bad modules as expected.' );