Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Check requestIdleCallback if it is native function #749

Merged
merged 2 commits into from
May 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions agent/Bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ var dehydrate = require('./dehydrate');
var getIn = require('./getIn');
var performanceNow = require('fbjs/lib/performanceNow');

// Use the polyfill if the function is not native implementation
function getWindowFunction(name, polyfill): Function {
if (String(window[name]).indexOf(`[native code]`) === -1) {
return polyfill;
}
return window[name];
}

// Custom polyfill that runs the queue with a backoff.
// If you change it, make sure it behaves reasonably well in Firefox.
var lastRunTimeMS = 5;
var cancelIdleCallback = window.cancelIdleCallback || clearTimeout;
var requestIdleCallback = window.requestIdleCallback || function(cb, options) {
var cancelIdleCallback = getWindowFunction('cancelIdleCallback', clearTimeout);
var requestIdleCallback = getWindowFunction('requestIdleCallback', function(cb, options) {
// Magic numbers determined by tweaking in Firefox.
// There is no special meaning to them.
var delayMS = 3000 * lastRunTimeMS;
Expand All @@ -39,7 +47,7 @@ var requestIdleCallback = window.requestIdleCallback || function(cb, options) {
var endTime = performanceNow();
lastRunTimeMS = (endTime - startTime) / 1000;
}, delayMS);
};
});

type AnyFn = (...x: any) => any;
export type Wall = {
Expand Down