mw.loader: Simplify getVersion and getState
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.js
index c5989c0..7e02df4 100644 (file)
                                registry[ module ].state = 'executing';
 
                                runScript = function () {
-                                       var script, markModuleReady, nestedAddScript, implicitDependencies, implicitWait;
+                                       var script, markModuleReady, nestedAddScript;
 
                                        script = registry[ module ].script;
                                        markModuleReady = function () {
                                                } );
                                        };
 
-                                       implicitDependencies = [];
-
-                                       if ( module === 'user' ) {
-                                               // Implicit dependency on the site module. Not real dependency because
-                                               // it should run after 'site' regardless of whether it succeeds or fails.
-                                               implicitDependencies.push( 'site' );
-                                       }
-
-                                       implicitWait = implicitDependencies.length ?
-                                               mw.loader.using( implicitDependencies ) :
-                                               $.Deferred().resolve();
-
-                                       implicitWait.always( function () {
-                                               try {
-                                                       if ( Array.isArray( script ) ) {
-                                                               nestedAddScript( script, markModuleReady, 0 );
-                                                       } 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 );
-                                                               markModuleReady();
-
-                                                       } else if ( typeof script === 'string' ) {
-                                                               // Site and user modules are legacy scripts that run in the global scope.
-                                                               // This is transported as a string instead of a function to avoid needing
-                                                               // to use string manipulation to undo the function wrapper.
-                                                               $.globalEval( script );
-                                                               markModuleReady();
+                                       try {
+                                               if ( Array.isArray( script ) ) {
+                                                       nestedAddScript( script, markModuleReady, 0 );
+                                               } 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 );
+                                                       markModuleReady();
+
+                                               } else if ( typeof script === 'string' ) {
+                                                       // Site and user modules are legacy scripts that run in the global scope.
+                                                       // This is transported as a string instead of a function to avoid needing
+                                                       // to use string manipulation to undo the function wrapper.
+                                                       $.globalEval( script );
+                                                       markModuleReady();
 
-                                                       } else {
-                                                               // Module without script
-                                                               markModuleReady();
-                                                       }
-                                               } catch ( e ) {
-                                                       // Use mw.track instead of mw.log because these errors are common in production mode
-                                                       // (e.g. undefined variable), and mw.log is only enabled in debug mode.
-                                                       registry[ module ].state = 'error';
-                                                       mw.track( 'resourceloader.exception', { exception: e, module: module, source: 'module-execute' } );
-                                                       handlePending( module );
+                                               } else {
+                                                       // Module without script
+                                                       markModuleReady();
                                                }
-                                       } );
+                                       } catch ( e ) {
+                                               // Use mw.track instead of mw.log because these errors are common in production mode
+                                               // (e.g. undefined variable), and mw.log is only enabled in debug mode.
+                                               registry[ module ].state = 'error';
+                                               mw.track( 'resourceloader.exception', { exception: e, module: module, source: 'module-execute' } );
+                                               handlePending( module );
+                                       }
                                };
 
                                // Add localizations to message system
                                                // cssHandlesRegistered ensures we don't take off too soon, e.g. when
                                                // one of the cssHandles is fired while we're still creating more handles.
                                                if ( cssHandlesRegistered && pending === 0 && runScript ) {
-                                                       runScript();
+                                                       if ( module === 'user' ) {
+                                                               // Implicit dependency on the site module. Not real dependency because
+                                                               // it should run after 'site' regardless of whether it succeeds or fails.
+                                                               mw.loader.using( [ 'site' ] ).always( runScript );
+                                                       } else {
+                                                               runScript();
+                                                       }
                                                        runScript = undefined; // Revoke
                                                }
                                        };
                                        a = [];
 
                                for ( key in o ) {
-                                       if ( hasOwn.call( o, key ) ) {
-                                               a.push( key );
-                                       }
+                                       a.push( key );
                                }
                                a.sort();
                                for ( key = 0; key < a.length; key++ ) {
                         * @param {string} sourceLoadScript URL of load.php
                         */
                        function doRequest( moduleMap, currReqBase, sourceLoadScript ) {
-                               var query = $.extend(
-                                       { modules: buildModulesString( moduleMap ) },
-                                       currReqBase
-                               );
+                               // Optimisation: Inherit (Object.create), not copy ($.extend)
+                               var query = Object.create( currReqBase );
+                               query.modules = buildModulesString( moduleMap );
                                query = sortQuery( query );
                                addScript( sourceLoadScript + '?' + $.param( query ) );
                        }
                                                // modules for this group from this source.
                                                modules = splits[ source ][ group ];
 
-                                               currReqBase = $.extend( {
-                                                       version: getCombinedVersion( modules )
-                                               }, reqBase );
+                                               // Optimisation: Inherit (Object.create), not copy ($.extend)
+                                               currReqBase = Object.create( reqBase );
+                                               currReqBase.version = getCombinedVersion( modules );
+
                                                // For user modules append a user name to the query string.
                                                if ( group === 'user' && mw.config.get( 'wgUserName' ) !== null ) {
                                                        currReqBase.user = mw.config.get( 'wgUserName' );
                                 *  in the registry.
                                 */
                                getVersion: function ( module ) {
-                                       if ( !hasOwn.call( registry, module ) || registry[ module ].version === undefined ) {
-                                               return null;
-                                       }
-                                       return registry[ module ].version;
+                                       return hasOwn.call( registry, module ) ? registry[ module ].version : null;
                                },
 
                                /**
                                 *  in the registry.
                                 */
                                getState: function ( module ) {
-                                       if ( !hasOwn.call( registry, module ) || registry[ module ].state === undefined ) {
-                                               return null;
-                                       }
-                                       return registry[ module ].state;
+                                       return hasOwn.call( registry, module ) ? registry[ module ].state : null;
                                },
 
                                /**
                        msg += ( e ? ':' : '.' );
                        console.log( msg );
 
-                       // If we have an exception object, log it to the error channel to trigger
-                       // proper stacktraces in browsers that support it. No fallback as we have
-                       // no browsers that don't support error(), but do support log().
-                       if ( e && console.error ) {
-                               console.error( String( e ), e );
+                       // If we have an exception object, log it to the warning channel to trigger
+                       // proper stacktraces in browsers that support it.
+                       if ( e && console.warn ) {
+                               console.warn( String( e ), e );
                        }
                }
                /* eslint-enable no-console */