Skip to content

Commit

Permalink
Fixes facebook#9102 by fake polyfilling rAF (and rIC)
Browse files Browse the repository at this point in the history
  • Loading branch information
ManasJayanth committed Apr 26, 2017
1 parent d12c41c commit 63ccd6f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
4 changes: 4 additions & 0 deletions scripts/fiber/tests-passing.txt
Expand Up @@ -1642,6 +1642,10 @@ src/renderers/native/__tests__/ReactNativeMount-test.js
* returns the correct instance and calls it in the callback
* renders and reorders children

src/renderers/shared/__tests__/ReactDOMFrameScheduling-test.js
* throws when requestAnimationFrame is not polyfilled in the browser.
* can import findDOMNode in Node environment.

src/renderers/shared/__tests__/ReactDebugTool-test.js
* should add and remove hooks
* warns once when an error is thrown in hook
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
44 changes: 44 additions & 0 deletions src/renderers/shared/__tests__/ReactDOMFrameScheduling-test.js
@@ -0,0 +1,44 @@
/**
* Copyright 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails react-core
*/

'use strict';

const ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
const describeFiber = ReactDOMFeatureFlags.useFiber ? describe : xdescribe;

describeFiber('ReactDOMFrameScheduling', () => {
it('throws when requestAnimationFrame is not polyfilled in the browser.', () => {
const previousRAF = global.requestAnimationFrame;
global.requestAnimationFrame = undefined;
jest.resetModules();
expect(() => {
require('ReactDOM');
}).toThrow(
'React depends on requestAnimationFrame. Make sure that you load a ' +
'polyfill in older browsers.',
);
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;
});
});

0 comments on commit 63ccd6f

Please sign in to comment.