From a5e37774683104ffa00dcb66859f5b38ffe7ab4f Mon Sep 17 00:00:00 2001 From: Krinkle Date: Thu, 10 Mar 2011 18:49:14 +0000 Subject: [PATCH] Fixing minor issues with mw.loader * Using $.isArray/isFunction instead of "instanceof" or typeof. ** "instanceof Array" can throw SyntaxErrors in some cases ** Some non-functions return 'function' as their type * JSHint: ** using === to compare to null (faster and safer) ** Missing semicolons ** Mixed spaces with tabs * Added mw.log call when module is loaded * Added support for custom prefix to mw.log (the mw.config value for 'mw.log.prefix' is pretty ugly as it sets it globally, when calling a function later again it's no longer the same. Perhaps just get rid of it) --- resources/mediawiki/mediawiki.js | 64 ++++++++++++++-------------- resources/mediawiki/mediawiki.log.js | 6 ++- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index 893e4e4bd8..37e3b1784d 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -4,10 +4,10 @@ jQuery.extend({ trimLeft : function( str ) { - return str == null ? '' : str.toString().replace( /^\s+/, '' ); + return str === null ? '' : str.toString().replace( /^\s+/, '' ); }, trimRight : function( str ) { - return str == null ? + return str === null ? '' : str.toString().replace( /\s+$/, '' ); }, ucFirst : function( str ) { @@ -300,7 +300,7 @@ window.mediaWiki = new ( function( $ ) { Message.prototype.escaped = function() { this.format = 'escaped'; return this.toString(); - } + }; /** * Checks if message exists @@ -376,7 +376,7 @@ window.mediaWiki = new ( function( $ ) { */ this.sessionId = function () { var sessionId = $.cookie( 'mediaWiki.user.sessionId' ); - if ( typeof sessionId == 'undefined' || sessionId == null ) { + if ( typeof sessionId == 'undefined' || sessionId === null ) { sessionId = generateId(); $.cookie( 'mediaWiki.user.sessionId', sessionId, { 'expires': null, 'path': '/' } ); } @@ -402,7 +402,7 @@ window.mediaWiki = new ( function( $ ) { return name; } var id = $.cookie( 'mediaWiki.user.id' ); - if ( typeof id == 'undefined' || id == null ) { + if ( typeof id == 'undefined' || id === null ) { id = generateId(); } // Set cookie if not set, or renew it if already set @@ -489,16 +489,16 @@ window.mediaWiki = new ( function( $ ) { * mediawiki. * * Format: - * { - * 'moduleName': { - * 'dependencies': ['required module', 'required module', ...], (or) function() {} - * 'state': 'registered', 'loading', 'loaded', 'ready', or 'error' - * 'script': function() {}, - * 'style': 'css code string', - * 'messages': { 'key': 'value' }, - * 'version': ############## (unix timestamp) - * } - * } + * { + * 'moduleName': { + * 'dependencies': ['required module', 'required module', ...], (or) function() {} + * 'state': 'registered', 'loading', 'loaded', 'ready', or 'error' + * 'script': function() {}, + * 'style': 'css code string', + * 'messages': { 'key': 'value' }, + * 'version': ############## (unix timestamp) + * } + * } */ var registry = {}; // List of modules which will be loaded as when ready @@ -556,7 +556,7 @@ window.mediaWiki = new ( function( $ ) { throw new Error( 'Unknown dependency: ' + module ); } // Resolves dynamic loader function and replaces it with its own results - if ( typeof registry[module].dependencies === 'function' ) { + if ( $.isFunction( registry[module].dependencies ) ) { registry[module].dependencies = registry[module].dependencies(); // Ensures the module's dependencies are always in an array if ( typeof registry[module].dependencies !== 'object' ) { @@ -625,7 +625,7 @@ window.mediaWiki = new ( function( $ ) { states = [states]; } // If called without a list of modules, build and use a list of all modules - var list = []; + var list = [], module; if ( typeof modules === 'undefined' ) { modules = []; for ( module in registry ) { @@ -659,6 +659,7 @@ window.mediaWiki = new ( function( $ ) { * @param module string module name to execute */ function execute( module ) { + var _method = 'mw.loader::execute'; if ( typeof registry[module] === 'undefined' ) { throw new Error( 'Module has not been registered yet: ' + module ); } else if ( registry[module].state === 'registered' ) { @@ -675,7 +676,7 @@ window.mediaWiki = new ( function( $ ) { new mediaWiki.html.Cdata( registry[module].style ) ) ); } else if ( typeof registry[module].style === 'object' - && !( registry[module].style instanceof Array ) ) + && !( $.isArray( registry[module].style ) ) ) { for ( var media in registry[module].style ) { $marker.before( mediaWiki.html.element( 'style', @@ -691,6 +692,7 @@ window.mediaWiki = new ( function( $ ) { // Execute script try { registry[module].script( jQuery ); + mw.log( 'State ready: ' + module, _method ) registry[module].state = 'ready'; // Run jobs who's dependencies have just been met for ( var j = 0; j < jobs.length; j++ ) { @@ -698,7 +700,7 @@ window.mediaWiki = new ( function( $ ) { filter( 'ready', jobs[j].dependencies ), jobs[j].dependencies ) ) { - if ( typeof jobs[j].ready === 'function' ) { + if ( $.isFunction( jobs[j].ready ) ) { jobs[j].ready(); } jobs.splice( j, 1 ); @@ -706,7 +708,7 @@ window.mediaWiki = new ( function( $ ) { } } // Execute modules who's dependencies have just been met - for ( r in registry ) { + for ( var r in registry ) { if ( registry[r].state == 'loaded' ) { if ( compare( filter( ['ready'], registry[r].dependencies ), @@ -717,13 +719,13 @@ window.mediaWiki = new ( function( $ ) { } } } catch ( e ) { - mediaWiki.log( 'Exception thrown by ' + module + ': ' + e.message ); + mediaWiki.log( 'Exception thrown by ' + module + ': ' + e.message, _method ); mediaWiki.log( e ); registry[module].state = 'error'; // Run error callbacks of jobs affected by this condition for ( var j = 0; j < jobs.length; j++ ) { if ( $.inArray( module, jobs[j].dependencies ) !== -1 ) { - if ( typeof jobs[j].error === 'function' ) { + if ( $.isFunction( jobs[j].error ) ) { jobs[j].error(); } jobs.splice( j, 1 ); @@ -863,7 +865,7 @@ window.mediaWiki = new ( function( $ ) { } // Load asynchronously after documument ready if ( ready ) { - setTimeout( function() { $( 'body' ).append( request() ); }, 0 ) + setTimeout( function() { $( 'body' ).append( request() ); }, 0 ); } else { document.write( request() ); } @@ -898,12 +900,12 @@ window.mediaWiki = new ( function( $ ) { 'state': 'registered', 'group': typeof group === 'string' ? group : null, 'dependencies': [], - 'version': typeof version !== 'undefined' ? parseInt( version ) : 0 + 'version': typeof version !== 'undefined' ? parseInt( version, 10 ) : 0 }; if ( typeof dependencies === 'string' ) { // Allow dependencies to be given as a single module name registry[module].dependencies = [dependencies]; - } else if ( typeof dependencies === 'object' || typeof dependencies === 'function' ) { + } 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; @@ -921,7 +923,7 @@ window.mediaWiki = new ( function( $ ) { mediaWiki.loader.register( module ); } // Validate input - if ( typeof script !== 'function' ) { + if ( !$.isFunction( script ) ) { throw new Error( 'script must be a function, not a ' + typeof script ); } if ( typeof style !== 'undefined' @@ -976,7 +978,7 @@ window.mediaWiki = new ( function( $ ) { // Validate input if ( typeof dependencies !== 'object' && typeof dependencies !== 'string' ) { throw new Error( 'dependencies must be a string or an array, not a ' + - typeof dependencies ) + typeof dependencies ); } // Allow calling with a single dependency as a string if ( typeof dependencies === 'string' ) { @@ -986,13 +988,13 @@ window.mediaWiki = new ( function( $ ) { dependencies = resolve( dependencies ); // If all dependencies are met, execute ready immediately if ( compare( filter( ['ready'], dependencies ), dependencies ) ) { - if ( typeof ready === 'function' ) { + if ( $.isFunction( ready ) ) { ready(); } } // If any dependencies have errors execute error immediately else if ( filter( ['error'], dependencies ).length ) { - if ( typeof error === 'function' ) { + if ( $.isFunction( error ) ) { error(); } } @@ -1016,7 +1018,7 @@ window.mediaWiki = new ( function( $ ) { // Validate input if ( typeof modules !== 'object' && typeof modules !== 'string' ) { throw new Error( 'modules must be a string or an array, not a ' + - typeof modules ) + typeof modules ); } // Allow calling with an external script or single dependency as a string if ( typeof modules === 'string' ) { @@ -1205,7 +1207,7 @@ window.mediaWiki = new ( function( $ ) { /* Auto-register from pre-loaded startup scripts */ -if ( typeof startUp === 'function' ) { +if ( $.isFunction( startUp ) ) { startUp(); delete startUp; } diff --git a/resources/mediawiki/mediawiki.log.js b/resources/mediawiki/mediawiki.log.js index 1c63b9ec46..595ce36f09 100644 --- a/resources/mediawiki/mediawiki.log.js +++ b/resources/mediawiki/mediawiki.log.js @@ -15,9 +15,11 @@ * @author Trevor Parscal * @param {string} string Message to output to console */ - mw.log = function( string ) { + mw.log = function( string, prefix ) { // Allow log messages to use a configured prefix - if ( mw.config.exists( 'mw.log.prefix' ) ) { + if ( typeof prefix == 'string' ) { + string = prefix + '> ' + string; + } else if ( mw.config.exists( 'mw.log.prefix' ) ) { string = mw.config.get( 'mw.log.prefix' ) + '> ' + string; } // Try to use an existing console -- 2.20.1