Skip to content

Commit 3fb5432

Browse files
author
Brian Vaughn
committed
SchedulerDOM falls back to globalThis if window is undefined
1 parent c62986c commit 3fb5432

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

packages/scheduler/src/forks/SchedulerDOM.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,23 @@ var isPerformingWork = false;
8585
var isHostCallbackScheduled = false;
8686
var isHostTimeoutScheduled = false;
8787

88+
// Web Workers can't access window.
89+
// Many browsers provide a globalThis property for this reason.
90+
// For legacy reasons (mostly unit tests) this code prefers window if defined.
91+
// eslint-disable-next-line no-undef
92+
const globalObject = typeof window !== 'undefined' ? window : globalThis;
93+
8894
// Capture local references to native APIs, in case a polyfill overrides them.
89-
const setTimeout = window.setTimeout;
90-
const clearTimeout = window.clearTimeout;
91-
const setImmediate = window.setImmediate; // IE and Node.js + jsdom
95+
const setTimeout = globalObject.setTimeout;
96+
const clearTimeout = globalObject.clearTimeout;
97+
const setImmediate = globalObject.setImmediate; // IE and Node.js + jsdom
9298

9399
if (typeof console !== 'undefined') {
94100
// TODO: Scheduler no longer requires these methods to be polyfilled. But
95101
// maybe we want to continue warning if they don't exist, to preserve the
96102
// option to rely on it in the future?
97-
const requestAnimationFrame = window.requestAnimationFrame;
98-
const cancelAnimationFrame = window.cancelAnimationFrame;
103+
const requestAnimationFrame = globalObject.requestAnimationFrame;
104+
const cancelAnimationFrame = globalObject.cancelAnimationFrame;
99105

100106
if (typeof requestAnimationFrame !== 'function') {
101107
// Using console['error'] to evade Babel and ESLint

0 commit comments

Comments
 (0)