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