diff --git a/packages/scheduler/src/__tests__/SchedulerBrowser-test.js b/packages/scheduler/src/__tests__/SchedulerBrowser-test.js index 90c527d87d7b8..50a9927ae3c6b 100644 --- a/packages/scheduler/src/__tests__/SchedulerBrowser-test.js +++ b/packages/scheduler/src/__tests__/SchedulerBrowser-test.js @@ -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? diff --git a/packages/scheduler/src/forks/SchedulerHostConfig.default.js b/packages/scheduler/src/forks/SchedulerHostConfig.default.js index 9b802f01e2580..998380bd110a3 100644 --- a/packages/scheduler/src/forks/SchedulerHostConfig.default.js +++ b/packages/scheduler/src/forks/SchedulerHostConfig.default.js @@ -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;