mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[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
This commit is contained in:
@@ -42,7 +42,7 @@ describe('SchedulerBrowser', () => {
|
||||
);
|
||||
|
||||
runtime = installMockBrowserRuntime();
|
||||
performance = window.performance;
|
||||
performance = global.performance;
|
||||
Scheduler = require('scheduler');
|
||||
cancelCallback = Scheduler.unstable_cancelCallback;
|
||||
scheduleCallback = Scheduler.unstable_scheduleCallback;
|
||||
@@ -50,6 +50,8 @@ describe('SchedulerBrowser', () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete global.performance;
|
||||
|
||||
if (!runtime.isLogEmpty()) {
|
||||
throw Error('Test exited without clearing log.');
|
||||
}
|
||||
@@ -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?
|
||||
|
||||
@@ -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.
|
||||
@@ -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.
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user