From fdde08f88279b39afbe7e76a5cbeeea79cc2b070 Mon Sep 17 00:00:00 2001 From: Liad Yosef Date: Mon, 9 Sep 2019 13:56:38 +0300 Subject: [PATCH 1/4] Bind rAF and cAF to window (#16606) When capturing local references of requestAnimationFrame and cancelAnimationFrame - bind them to the window object. Fixes #16606 --- packages/scheduler/src/forks/SchedulerHostConfig.default.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/scheduler/src/forks/SchedulerHostConfig.default.js b/packages/scheduler/src/forks/SchedulerHostConfig.default.js index 77e15c1000b52..149ccf38b1037 100644 --- a/packages/scheduler/src/forks/SchedulerHostConfig.default.js +++ b/packages/scheduler/src/forks/SchedulerHostConfig.default.js @@ -83,8 +83,8 @@ if ( const Date = window.Date; const setTimeout = window.setTimeout; const clearTimeout = window.clearTimeout; - const requestAnimationFrame = window.requestAnimationFrame; - const cancelAnimationFrame = window.cancelAnimationFrame; + const requestAnimationFrame = window.requestAnimationFrame && window.requestAnimationFrame.bind(window); + const cancelAnimationFrame = window.cancelAnimationFrame && window.cancelAnimationFrame.bind(window); if (typeof console !== 'undefined') { // TODO: Remove fb.me link From 5c6b97db1c66fbb5c57103d3f18e25ce355bcf22 Mon Sep 17 00:00:00 2001 From: Liad Yosef Date: Mon, 9 Sep 2019 14:11:03 +0300 Subject: [PATCH 2/4] Prettier --- packages/scheduler/src/forks/SchedulerHostConfig.default.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/scheduler/src/forks/SchedulerHostConfig.default.js b/packages/scheduler/src/forks/SchedulerHostConfig.default.js index 149ccf38b1037..7d1c5d5e2cb7f 100644 --- a/packages/scheduler/src/forks/SchedulerHostConfig.default.js +++ b/packages/scheduler/src/forks/SchedulerHostConfig.default.js @@ -83,8 +83,10 @@ if ( const Date = window.Date; const setTimeout = window.setTimeout; const clearTimeout = window.clearTimeout; - const requestAnimationFrame = window.requestAnimationFrame && window.requestAnimationFrame.bind(window); - const cancelAnimationFrame = window.cancelAnimationFrame && window.cancelAnimationFrame.bind(window); + const requestAnimationFrame = + window.requestAnimationFrame && window.requestAnimationFrame.bind(window); + const cancelAnimationFrame = + window.cancelAnimationFrame && window.cancelAnimationFrame.bind(window); if (typeof console !== 'undefined') { // TODO: Remove fb.me link From 2dc1147faf436464507a62e07d2921c1e04665c0 Mon Sep 17 00:00:00 2001 From: Liad Yosef Date: Tue, 10 Sep 2019 16:15:07 +0300 Subject: [PATCH 3/4] Add a comment in the code Add a comment to clarify why we explicitly bind the functions to window. --- packages/scheduler/src/forks/SchedulerHostConfig.default.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/scheduler/src/forks/SchedulerHostConfig.default.js b/packages/scheduler/src/forks/SchedulerHostConfig.default.js index 7d1c5d5e2cb7f..24409944ab5a6 100644 --- a/packages/scheduler/src/forks/SchedulerHostConfig.default.js +++ b/packages/scheduler/src/forks/SchedulerHostConfig.default.js @@ -83,6 +83,8 @@ if ( const Date = window.Date; const setTimeout = window.setTimeout; const clearTimeout = window.clearTimeout; + // Explicitly bind local references to `window`, in order to support + // environments where the global `this` is not the window (e.g extensions) const requestAnimationFrame = window.requestAnimationFrame && window.requestAnimationFrame.bind(window); const cancelAnimationFrame = From 0562fb512ad91691e28129cc042b422363a7274c Mon Sep 17 00:00:00 2001 From: Liad Yosef Date: Tue, 10 Sep 2019 17:28:05 +0300 Subject: [PATCH 4/4] more specific check for function type (rAF and cAF) Check specifically that requestAnimationFrame and cancelAnimationFrame are functions, and not just that they are truthy. --- .../scheduler/src/forks/SchedulerHostConfig.default.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/scheduler/src/forks/SchedulerHostConfig.default.js b/packages/scheduler/src/forks/SchedulerHostConfig.default.js index 24409944ab5a6..8789ceb50b9eb 100644 --- a/packages/scheduler/src/forks/SchedulerHostConfig.default.js +++ b/packages/scheduler/src/forks/SchedulerHostConfig.default.js @@ -86,9 +86,13 @@ if ( // Explicitly bind local references to `window`, in order to support // environments where the global `this` is not the window (e.g extensions) const requestAnimationFrame = - window.requestAnimationFrame && window.requestAnimationFrame.bind(window); + typeof window.requestAnimationFrame === 'function' + ? window.requestAnimationFrame.bind(window) + : undefined; const cancelAnimationFrame = - window.cancelAnimationFrame && window.cancelAnimationFrame.bind(window); + typeof window.cancelAnimationFrame === 'function' + ? window.cancelAnimationFrame.bind(window) + : undefined; if (typeof console !== 'undefined') { // TODO: Remove fb.me link