From cc8191d8219bc47b6bfc03e1fe630e13a084a295 Mon Sep 17 00:00:00 2001 From: Manas Date: Tue, 18 Apr 2017 23:23:49 +0530 Subject: [PATCH] Attempt fixing #9102. Fake polyfill rAF (and rIC) --- .../dom/fiber/__tests__/ReactDOMFiber-test.js | 26 +++++++++++++++++++ .../shared/ReactDOMFrameScheduling.js | 20 +++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/renderers/dom/fiber/__tests__/ReactDOMFiber-test.js b/src/renderers/dom/fiber/__tests__/ReactDOMFiber-test.js index c659d65123e59f..623a00c2a74fdf 100644 --- a/src/renderers/dom/fiber/__tests__/ReactDOMFiber-test.js +++ b/src/renderers/dom/fiber/__tests__/ReactDOMFiber-test.js @@ -81,6 +81,32 @@ describe('ReactDOMFiber', () => { }); if (ReactDOMFeatureFlags.useFiber) { + + it('throws when requestAnimationFrame is not polyfilled in the browser.', () => { + const previousRAF = global.requestAnimationFrame; + const previousRIC = global.requestIdleCallback; + global.requestAnimationFrame = undefined; + jest.resetModules(); + expect(() => { + require('ReactDOM'); + }).toThrow(); + global.requestAnimationFrame = previousRAF; + }); + + it('can import findDOMNode in Node environment.', () => { + const previousRAF = global.requestAnimationFrame; + const previousRIC = global.requestIdleCallback; + global.requestAnimationFrame = undefined; + global.requestIdleCallback = undefined; + const prevWindow = global.window; + delete global.window; + jest.resetModules(); + require('ReactDOM'); + global.requestAnimationFrame = previousRAF; + global.requestIdleCallback = previousRIC; + global.window = prevWindow; + }); + it('should render a component returning strings directly from render', () => { const Text = ({value}) => value; diff --git a/src/renderers/shared/ReactDOMFrameScheduling.js b/src/renderers/shared/ReactDOMFrameScheduling.js index 17a8749154d370..e08d43039361e0 100644 --- a/src/renderers/shared/ReactDOMFrameScheduling.js +++ b/src/renderers/shared/ReactDOMFrameScheduling.js @@ -23,11 +23,29 @@ import type {Deadline} from 'ReactFiberReconciler'; var invariant = require('fbjs/lib/invariant'); +var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment'); // TODO: There's no way to cancel these, because Fiber doesn't atm. let rAF: (callback: (time: number) => void) => number; let rIC: (callback: (deadline: Deadline) => void) => number; -if (typeof requestAnimationFrame !== 'function') { + +if (!ExecutionEnvironment.canUseDOM) { + rAF = function(frameCallback: (time: number) => void): number { + setTimeout(frameCallback, 16); + return 0; + }; + + rIC = function(frameCallback: (deadline: Deadline) => void): number { + setTimeout(() => { + frameCallback({ + timeRemaining() { + return Infinity; + }, + }); + }); + return 0; + }; +} else if (typeof requestAnimationFrame !== 'function') { invariant( false, 'React depends on requestAnimationFrame. Make sure that you load a ' +