resourceloader: Remove redundant closure of some startup and base files
[lhc/web/wiklou.git] / resources / src / startup / mediawiki.requestIdleCallback.js
1 /* global mw */
2 mw.requestIdleCallbackInternal = function ( callback ) {
3 setTimeout( function () {
4 var start = mw.now();
5 callback( {
6 didTimeout: false,
7 timeRemaining: function () {
8 // Hard a target maximum busy time of 50 milliseconds
9 return Math.max( 0, 50 - ( mw.now() - start ) );
10 }
11 } );
12 }, 1 );
13 };
14
15 /**
16 * Schedule a deferred task to run in the background.
17 *
18 * This allows code to perform tasks in the main thread without impacting
19 * time-critical operations such as animations and response to input events.
20 *
21 * Basic logic is as follows:
22 *
23 * - User input event should be acknowledged within 100ms per [RAIL].
24 * - Idle work should be grouped in blocks of upto 50ms so that enough time
25 * remains for the event handler to execute and any rendering to take place.
26 * - Whenever a native event happens (e.g. user input), the deadline for any
27 * running idle callback drops to 0.
28 * - As long as the deadline is non-zero, other callbacks pending may be
29 * executed in the same idle period.
30 *
31 * See also:
32 *
33 * - <https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback>
34 * - <https://w3c.github.io/requestidlecallback/>
35 * - <https://developers.google.com/web/updates/2015/08/using-requestidlecallback>
36 * [RAIL]: https://developers.google.com/web/fundamentals/performance/rail
37 *
38 * @member mw
39 * @param {Function} callback
40 * @param {Object} [options]
41 * @param {number} [options.timeout] If set, the callback will be scheduled for
42 * immediate execution after this amount of time (in milliseconds) if it didn't run
43 * by that time.
44 */
45 mw.requestIdleCallback = window.requestIdleCallback ?
46 // Bind because it throws TypeError if context is not window
47 window.requestIdleCallback.bind( window ) :
48 mw.requestIdleCallbackInternal;
49 // Note: Polyfill was previously disabled due to
50 // https://bugs.chromium.org/p/chromium/issues/detail?id=647870
51 // See also <http://codepen.io/Krinkle/full/XNGEvv>