Skip to content

Commit

Permalink
[Scheduler] Get current time from performance.now in non-DOM environm…
Browse files Browse the repository at this point in the history
…ents (#19532)

* Get current time from performance.now in non-DOM environments

* Use local references to native APIs for Date and Performance

* Refactored to read globals directly
  • Loading branch information
emilisb authored Aug 5, 2020
1 parent e9721e1 commit 5cff775
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
12 changes: 7 additions & 5 deletions packages/scheduler/src/__tests__/SchedulerBrowser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ describe('SchedulerBrowser', () => {
);

runtime = installMockBrowserRuntime();
performance = window.performance;
performance = global.performance;
Scheduler = require('scheduler');
cancelCallback = Scheduler.unstable_cancelCallback;
scheduleCallback = Scheduler.unstable_scheduleCallback;
NormalPriority = Scheduler.unstable_NormalPriority;
});

afterEach(() => {
delete global.performance;

if (!runtime.isLogEmpty()) {
throw Error('Test exited without clearing log.');
}
Expand All @@ -63,17 +65,17 @@ describe('SchedulerBrowser', () => {

let eventLog = [];

const window = {};
global.window = window;

let currentTime = 0;

window.performance = {
global.performance = {
now() {
return currentTime;
},
};

const window = {};
global.window = window;

// TODO: Scheduler no longer requires these methods to be polyfilled. But
// maybe we want to continue warning if they don't exist, to preserve the
// option to rely on it in the future?
Expand Down
28 changes: 12 additions & 16 deletions packages/scheduler/src/forks/SchedulerHostConfig.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ export let requestPaint;
export let getCurrentTime;
export let forceFrameRate;

const hasPerformanceNow =
typeof performance === 'object' && typeof performance.now === 'function';

if (hasPerformanceNow) {
const localPerformance = performance;
getCurrentTime = () => localPerformance.now();
} else {
const localDate = Date;
const initialTime = localDate.now();
getCurrentTime = () => localDate.now() - initialTime;
}

if (
// If Scheduler runs in a non-DOM environment, it falls back to a naive
// implementation using setTimeout.
Expand All @@ -40,10 +52,6 @@ if (
}
}
};
const initialTime = Date.now();
getCurrentTime = function() {
return Date.now() - initialTime;
};
requestHostCallback = function(cb) {
if (_callback !== null) {
// Protect against re-entrancy.
Expand All @@ -68,8 +76,6 @@ if (
requestPaint = forceFrameRate = function() {};
} else {
// Capture local references to native APIs, in case a polyfill overrides them.
const performance = window.performance;
const Date = window.Date;
const setTimeout = window.setTimeout;
const clearTimeout = window.clearTimeout;

Expand Down Expand Up @@ -98,16 +104,6 @@ if (
}
}

if (
typeof performance === 'object' &&
typeof performance.now === 'function'
) {
getCurrentTime = () => performance.now();
} else {
const initialTime = Date.now();
getCurrentTime = () => Date.now() - initialTime;
}

let isMessageLoopRunning = false;
let scheduledHostCallback = null;
let taskTimeoutID = -1;
Expand Down

0 comments on commit 5cff775

Please sign in to comment.