@samitouri / QOS-React-2 / commits / 5cff775502

[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

Emilis Baliukonis committed Aug 5, 2020 at 19:29 UTC 5cff7755022220f24c1066643e0036b41806e8fa
2 files changed +19 -21
packages/scheduler/src/__tests__/SchedulerBrowser-test.js
+7 -5
@@ -42,7 +42,7 @@ describe('SchedulerBrowser', () => {
42 );
43
44 runtime = installMockBrowserRuntime();
45 - performance = window.performance;
45 + performance = global.performance;
46 Scheduler = require('scheduler');
47 cancelCallback = Scheduler.unstable_cancelCallback;
48 scheduleCallback = Scheduler.unstable_scheduleCallback;
@@ -50,6 +50,8 @@ describe('SchedulerBrowser', () => {
50 });
51
52 afterEach(() => {
53 + delete global.performance;
54 +
55 if (!runtime.isLogEmpty()) {
56 throw Error('Test exited without clearing log.');
57 }
@@ -63,17 +65,17 @@ describe('SchedulerBrowser', () => {
65
66 let eventLog = [];
67
66 - const window = {};
67 - global.window = window;
68 -
68 let currentTime = 0;
69
71 - window.performance = {
70 + global.performance = {
71 now() {
72 return currentTime;
73 },
74 };
75
76 + const window = {};
77 + global.window = window;
78 +
79 // TODO: Scheduler no longer requires these methods to be polyfilled. But
80 // maybe we want to continue warning if they don't exist, to preserve the
81 // option to rely on it in the future?
packages/scheduler/src/forks/SchedulerHostConfig.default.js
+12 -16
@@ -16,6 +16,18 @@ export let requestPaint;
16 export let getCurrentTime;
17 export let forceFrameRate;
18
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 +
31 if (
32 // If Scheduler runs in a non-DOM environment, it falls back to a naive
33 // implementation using setTimeout.
@@ -40,10 +52,6 @@ if (
52 }
53 }
54 };
43 - const initialTime = Date.now();
44 - getCurrentTime = function() {
45 - return Date.now() - initialTime;
46 - };
55 requestHostCallback = function(cb) {
56 if (_callback !== null) {
57 // Protect against re-entrancy.
@@ -68,8 +76,6 @@ if (
76 requestPaint = forceFrameRate = function() {};
77 } else {
78 // Capture local references to native APIs, in case a polyfill overrides them.
71 - const performance = window.performance;
72 - const Date = window.Date;
79 const setTimeout = window.setTimeout;
80 const clearTimeout = window.clearTimeout;
81
@@ -98,16 +104,6 @@ if (
104 }
105 }
106
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 -
107 let isMessageLoopRunning = false;
108 let scheduledHostCallback = null;
109 let taskTimeoutID = -1;