From: Timo Tijhof Date: Tue, 24 Sep 2019 00:09:04 +0000 (+0100) Subject: resourceloader: Remove redundant closure of some startup and base files X-Git-Tag: 1.34.0-rc.0~115^2 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=f4975bc6de776c8eb06490932329a3c8d657eabd resourceloader: Remove redundant closure of some startup and base files * errorLogger: Contains only top-level assignments and statements, no scope needed. If it does need a scope at some point, then it should be converted to packageFiles first so that the closure still isn't needed (see T50886). * requestIdleCallback: This file should do nothing in modern browsers, except alias a native method, and expose the fallback for testing. Remove the closure and with it the (usually) unused local var. * mediawiki.js: Use the shorter Object.hasOwnProperty instead, we already do this in several files. Both provide a quick reference to the native method. Object.prototype.hasOwnProperty is more direct visually, but also less direct at run-time (3 lookups instead of 2). MW does not support environments that extend native classes such as Object or their prototypes, so this makes no difference in that regard and is already done in several core files. (More wide changes to be done separately). Change-Id: I98ed4da4807c54254669053cef47a3e67b4ad2cf --- diff --git a/resources/src/mediawiki.base/mediawiki.errorLogger.js b/resources/src/mediawiki.base/mediawiki.errorLogger.js index b6aa5fdce1..2bf685ea06 100644 --- a/resources/src/mediawiki.base/mediawiki.errorLogger.js +++ b/resources/src/mediawiki.base/mediawiki.errorLogger.js @@ -4,57 +4,58 @@ * @class mw.errorLogger * @singleton */ -( function () { - 'use strict'; +mw.errorLogger = { + /** + * Fired via mw.track when an error is not handled by local code and is caught by the + * window.onerror handler. + * + * @event global_error + * @param {string} errorMessage Error errorMessage. + * @param {string} url URL where error was raised. + * @param {number} lineNumber Line number where error was raised. + * @param {number} [columnNumber] Line number where error was raised. Not all browsers + * support this. + * @param {Error|Mixed} [errorObject] The error object. Typically an instance of Error, but anything + * (even a primitive value) passed to a throw clause will end up here. + */ - mw.errorLogger = { - /** - * Fired via mw.track when an error is not handled by local code and is caught by the - * window.onerror handler. - * - * @event global_error - * @param {string} errorMessage Error errorMessage. - * @param {string} url URL where error was raised. - * @param {number} lineNumber Line number where error was raised. - * @param {number} [columnNumber] Line number where error was raised. Not all browsers - * support this. - * @param {Error|Mixed} [errorObject] The error object. Typically an instance of Error, but anything - * (even a primitive value) passed to a throw clause will end up here. - */ + /** + * Install a window.onerror handler that will report via mw.track, while preserving + * any previous handler. + * + * @param {Object} window + */ + installGlobalHandler: function ( window ) { + // We will preserve the return value of the previous handler. window.onerror works the + // opposite way than normal event handlers (returning true will prevent the default + // action, returning false will let the browser handle the error normally, by e.g. + // logging to the console), so our fallback old handler needs to return false. + var oldHandler = window.onerror || function () { + return false; + }; /** - * Install a window.onerror handler that will report via mw.track, while preserving - * any previous handler. + * Dumb window.onerror handler which forwards the errors via mw.track. * - * @param {Object} window + * @param {string} errorMessage + * @param {string} url + * @param {number} lineNumber + * @param {number} [columnNumber] + * @param {Error|Mixed} [errorObject] + * @return {boolean} True to prevent the default action + * @fires global_error */ - installGlobalHandler: function ( window ) { - // We will preserve the return value of the previous handler. window.onerror works the - // opposite way than normal event handlers (returning true will prevent the default - // action, returning false will let the browser handle the error normally, by e.g. - // logging to the console), so our fallback old handler needs to return false. - var oldHandler = window.onerror || function () { - return false; - }; - - /** - * Dumb window.onerror handler which forwards the errors via mw.track. - * - * @param {string} errorMessage - * @param {string} url - * @param {number} lineNumber - * @param {number} [columnNumber] - * @param {Error|Mixed} [errorObject] - * @return {boolean} True to prevent the default action - * @fires global_error - */ - window.onerror = function ( errorMessage, url, lineNumber, columnNumber, errorObject ) { - mw.track( 'global.error', { errorMessage: errorMessage, url: url, - lineNumber: lineNumber, columnNumber: columnNumber, errorObject: errorObject } ); - return oldHandler.apply( this, arguments ); - }; - } - }; + window.onerror = function ( errorMessage, url, lineNumber, columnNumber, errorObject ) { + mw.track( 'global.error', { + errorMessage: errorMessage, + url: url, + lineNumber: lineNumber, + columnNumber: columnNumber, + errorObject: errorObject + } ); + return oldHandler.apply( this, arguments ); + }; + } +}; - mw.errorLogger.installGlobalHandler( window ); -}() ); +mw.errorLogger.installGlobalHandler( window ); diff --git a/resources/src/startup/mediawiki.js b/resources/src/startup/mediawiki.js index 21d95dfb44..6a6aa3d885 100644 --- a/resources/src/startup/mediawiki.js +++ b/resources/src/startup/mediawiki.js @@ -13,7 +13,7 @@ 'use strict'; var mw, StringSet, log, - hasOwn = Object.prototype.hasOwnProperty; + hasOwn = Object.hasOwnProperty; /** * FNV132 hash function diff --git a/resources/src/startup/mediawiki.requestIdleCallback.js b/resources/src/startup/mediawiki.requestIdleCallback.js index afb4737631..2787f585d4 100644 --- a/resources/src/startup/mediawiki.requestIdleCallback.js +++ b/resources/src/startup/mediawiki.requestIdleCallback.js @@ -1,54 +1,51 @@ /* global mw */ -( function () { - var maxBusy = 50; +mw.requestIdleCallbackInternal = function ( callback ) { + setTimeout( function () { + var start = mw.now(); + callback( { + didTimeout: false, + timeRemaining: function () { + // Hard a target maximum busy time of 50 milliseconds + return Math.max( 0, 50 - ( mw.now() - start ) ); + } + } ); + }, 1 ); +}; - mw.requestIdleCallbackInternal = function ( callback ) { - setTimeout( function () { - var start = mw.now(); - callback( { - didTimeout: false, - timeRemaining: function () { - return Math.max( 0, maxBusy - ( mw.now() - start ) ); - } - } ); - }, 1 ); - }; - - /** - * Schedule a deferred task to run in the background. - * - * This allows code to perform tasks in the main thread without impacting - * time-critical operations such as animations and response to input events. - * - * Basic logic is as follows: - * - * - User input event should be acknowledged within 100ms per [RAIL]. - * - Idle work should be grouped in blocks of upto 50ms so that enough time - * remains for the event handler to execute and any rendering to take place. - * - Whenever a native event happens (e.g. user input), the deadline for any - * running idle callback drops to 0. - * - As long as the deadline is non-zero, other callbacks pending may be - * executed in the same idle period. - * - * See also: - * - * - - * - - * - - * [RAIL]: https://developers.google.com/web/fundamentals/performance/rail - * - * @member mw - * @param {Function} callback - * @param {Object} [options] - * @param {number} [options.timeout] If set, the callback will be scheduled for - * immediate execution after this amount of time (in milliseconds) if it didn't run - * by that time. - */ - mw.requestIdleCallback = window.requestIdleCallback ? - // Bind because it throws TypeError if context is not window - window.requestIdleCallback.bind( window ) : - mw.requestIdleCallbackInternal; - // Note: Polyfill was previously disabled due to - // https://bugs.chromium.org/p/chromium/issues/detail?id=647870 - // See also -}() ); +/** + * Schedule a deferred task to run in the background. + * + * This allows code to perform tasks in the main thread without impacting + * time-critical operations such as animations and response to input events. + * + * Basic logic is as follows: + * + * - User input event should be acknowledged within 100ms per [RAIL]. + * - Idle work should be grouped in blocks of upto 50ms so that enough time + * remains for the event handler to execute and any rendering to take place. + * - Whenever a native event happens (e.g. user input), the deadline for any + * running idle callback drops to 0. + * - As long as the deadline is non-zero, other callbacks pending may be + * executed in the same idle period. + * + * See also: + * + * - + * - + * - + * [RAIL]: https://developers.google.com/web/fundamentals/performance/rail + * + * @member mw + * @param {Function} callback + * @param {Object} [options] + * @param {number} [options.timeout] If set, the callback will be scheduled for + * immediate execution after this amount of time (in milliseconds) if it didn't run + * by that time. + */ +mw.requestIdleCallback = window.requestIdleCallback ? + // Bind because it throws TypeError if context is not window + window.requestIdleCallback.bind( window ) : + mw.requestIdleCallbackInternal; +// Note: Polyfill was previously disabled due to +// https://bugs.chromium.org/p/chromium/issues/detail?id=647870 +// See also