Skip to content

Commit 5cff775

Browse files
authored
[Scheduler] Get current time from performance.now in non-DOM environments (#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
1 parent e9721e1 commit 5cff775

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

packages/scheduler/src/__tests__/SchedulerBrowser-test.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ describe('SchedulerBrowser', () => {
4242
);
4343

4444
runtime = installMockBrowserRuntime();
45-
performance = window.performance;
45+
performance = global.performance;
4646
Scheduler = require('scheduler');
4747
cancelCallback = Scheduler.unstable_cancelCallback;
4848
scheduleCallback = Scheduler.unstable_scheduleCallback;
4949
NormalPriority = Scheduler.unstable_NormalPriority;
5050
});
5151

5252
afterEach(() => {
53+
delete global.performance;
54+
5355
if (!runtime.isLogEmpty()) {
5456
throw Error('Test exited without clearing log.');
5557
}
@@ -63,17 +65,17 @@ describe('SchedulerBrowser', () => {
6365

6466
let eventLog = [];
6567

66-
const window = {};
67-
global.window = window;
68-
6968
let currentTime = 0;
7069

71-
window.performance = {
70+
global.performance = {
7271
now() {
7372
return currentTime;
7473
},
7574
};
7675

76+
const window = {};
77+
global.window = window;
78+
7779
// TODO: Scheduler no longer requires these methods to be polyfilled. But
7880
// maybe we want to continue warning if they don't exist, to preserve the
7981
// option to rely on it in the future?

packages/scheduler/src/forks/SchedulerHostConfig.default.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ export let requestPaint;
1616
export let getCurrentTime;
1717
export let forceFrameRate;
1818

19+
const hasPerformanceNow =
20+
typeof performance === 'object' && typeof performance.now === 'function';
21+
22+
if (hasPerformanceNow) {
23+
const localPerformance = performance;
24+
getCurrentTime = () => localPerformance.now();
25+
} else {
26+
const localDate = Date;
27+
const initialTime = localDate.now();
28+
getCurrentTime = () => localDate.now() - initialTime;
29+
}
30+
1931
if (
2032
// If Scheduler runs in a non-DOM environment, it falls back to a naive
2133
// implementation using setTimeout.
@@ -40,10 +52,6 @@ if (
4052
}
4153
}
4254
};
43-
const initialTime = Date.now();
44-
getCurrentTime = function() {
45-
return Date.now() - initialTime;
46-
};
4755
requestHostCallback = function(cb) {
4856
if (_callback !== null) {
4957
// Protect against re-entrancy.
@@ -68,8 +76,6 @@ if (
6876
requestPaint = forceFrameRate = function() {};
6977
} else {
7078
// Capture local references to native APIs, in case a polyfill overrides them.
71-
const performance = window.performance;
72-
const Date = window.Date;
7379
const setTimeout = window.setTimeout;
7480
const clearTimeout = window.clearTimeout;
7581

@@ -98,16 +104,6 @@ if (
98104
}
99105
}
100106

101-
if (
102-
typeof performance === 'object' &&
103-
typeof performance.now === 'function'
104-
) {
105-
getCurrentTime = () => performance.now();
106-
} else {
107-
const initialTime = Date.now();
108-
getCurrentTime = () => Date.now() - initialTime;
109-
}
110-
111107
let isMessageLoopRunning = false;
112108
let scheduledHostCallback = null;
113109
let taskTimeoutID = -1;

0 commit comments

Comments
 (0)