Skip to content

Commit

Permalink
Attempt fixing facebook#9102. Fake polyfill rAF (and rIC)
Browse files Browse the repository at this point in the history
  • Loading branch information
ManasJayanth committed Apr 25, 2017
1 parent d12c41c commit cc8191d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/renderers/dom/fiber/__tests__/ReactDOMFiber-test.js
Expand Up @@ -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;

Expand Down
20 changes: 19 additions & 1 deletion src/renderers/shared/ReactDOMFrameScheduling.js
Expand Up @@ -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 ' +
Expand Down

0 comments on commit cc8191d

Please sign in to comment.