Merge "resourceloader: Remove support for `state(name, state)` signature"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 14 Aug 2018 18:41:06 +0000 (18:41 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 14 Aug 2018 18:41:06 +0000 (18:41 +0000)
includes/resourceloader/ResourceLoader.php
resources/src/startup/mediawiki.js
tests/qunit/data/load.mock.php
tests/qunit/suites/resources/mediawiki/mediawiki.loader.test.js

index 151b271..45c19eb 100644 (file)
@@ -1298,8 +1298,8 @@ MESSAGE;
        }
 
        /**
-        * 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
@@ -1307,24 +1307,19 @@ MESSAGE;
         *    - 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()
+               );
        }
 
        /**
index 63d23d8..d586fc9 100644 (file)
                                /**
                                 * 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 );
                                        }
                                },
 
index 3b710c4..2238fce 100644 (file)
@@ -103,7 +103,7 @@ if ( isset( $_GET['modules'] ) ) {
                                . '} );';
                } else {
                        // Default
-                       $response .= 'mw.loader.state(' . json_encode( $module ) . ', "missing" );' . "\n";
+                       $response .= 'mw.loader.state(' . json_encode( [ $module => 'missing' ] ) . ');' . "\n";
                }
        }
 }
index 05b85be..1a1affa 100644 (file)
                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.' );