Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the 'alwaysUseRequestIdleCallbackPolyfill' feature flag #12648

Merged
merged 3 commits into from
Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ describe('ReactDOMFiberAsync', () => {
let container;

beforeEach(() => {
// TODO pull this into helper method, reduce repetition.
// mock the browser APIs which are used in react-scheduler:
// - requestAnimationFrame should pass the DOMHighResTimeStamp argument
// - calling 'window.postMessage' should actually fire postmessage handlers
global.requestAnimationFrame = function(cb) {
return setTimeout(() => {
cb(Date.now());
});
};
const originalAddEventListener = global.addEventListener;
let postMessageCallback;
global.addEventListener = function(eventName, callback, useCapture) {
if (eventName === 'message') {
postMessageCallback = callback;
} else {
originalAddEventListener(eventName, callback, useCapture);
}
};
global.postMessage = function(messageKey, targetOrigin) {
const postMessageEvent = {source: window, data: messageKey};
if (postMessageCallback) {
postMessageCallback(postMessageEvent);
}
};
jest.resetModules();
container = document.createElement('div');
ReactDOM = require('react-dom');
});
Expand Down
101 changes: 50 additions & 51 deletions packages/react-dom/src/__tests__/ReactDOMRoot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,46 @@ let AsyncMode = React.unstable_AsyncMode;
describe('ReactDOMRoot', () => {
let container;

let scheduledCallback;
let flush;
let now;
let expire;
let advanceCurrentTime;

beforeEach(() => {
container = document.createElement('div');

// Override requestIdleCallback
scheduledCallback = null;
flush = function(units = Infinity) {
if (scheduledCallback !== null) {
let didStop = false;
while (scheduledCallback !== null && !didStop) {
const cb = scheduledCallback;
scheduledCallback = null;
cb({
timeRemaining() {
if (units > 0) {
return 999;
}
didStop = true;
return 0;
},
});
units--;
}
// TODO pull this into helper method, reduce repetition.
// mock the browser APIs which are used in react-scheduler:
// - requestAnimationFrame should pass the DOMHighResTimeStamp argument
// - calling 'window.postMessage' should actually fire postmessage handlers
// - must allow artificially changing time returned by Date.now
// Performance.now is not supported in the test environment
const originalDateNow = Date.now;
let advancedTime = null;
global.Date.now = function() {
if (advancedTime) {
return originalDateNow() + advancedTime;
}
return originalDateNow();
};
global.performance = {
now() {
return now;
},
advanceCurrentTime = function(amount) {
advancedTime = amount;
};
global.requestIdleCallback = function(cb) {
scheduledCallback = cb;
global.requestAnimationFrame = function(cb) {
return setTimeout(() => {
cb(Date.now());
});
};

now = 0;
expire = function(ms) {
now += ms;
const originalAddEventListener = global.addEventListener;
let postMessageCallback;
global.addEventListener = function(eventName, callback, useCapture) {
if (eventName === 'message') {
postMessageCallback = callback;
} else {
originalAddEventListener(eventName, callback, useCapture);
}
};
global.postMessage = function(messageKey, targetOrigin) {
const postMessageEvent = {source: window, data: messageKey};
if (postMessageCallback) {
postMessageCallback(postMessageEvent);
}
};

jest.resetModules();
Expand All @@ -70,17 +69,17 @@ describe('ReactDOMRoot', () => {
it('renders children', () => {
const root = ReactDOM.unstable_createRoot(container);
root.render(<div>Hi</div>);
flush();
jest.runAllTimers();
expect(container.textContent).toEqual('Hi');
});

it('unmounts children', () => {
const root = ReactDOM.unstable_createRoot(container);
root.render(<div>Hi</div>);
flush();
jest.runAllTimers();
expect(container.textContent).toEqual('Hi');
root.unmount();
flush();
jest.runAllTimers();
expect(container.textContent).toEqual('');
});

Expand All @@ -92,7 +91,7 @@ describe('ReactDOMRoot', () => {
ops.push('inside callback: ' + container.textContent);
});
ops.push('before committing: ' + container.textContent);
flush();
jest.runAllTimers();
ops.push('after committing: ' + container.textContent);
expect(ops).toEqual([
'before committing: ',
Expand All @@ -105,7 +104,7 @@ describe('ReactDOMRoot', () => {
it('resolves `work.then` callback synchronously if the work already committed', () => {
const root = ReactDOM.unstable_createRoot(container);
const work = root.render(<AsyncMode>Hi</AsyncMode>);
flush();
jest.runAllTimers();
let ops = [];
work.then(() => {
ops.push('inside callback');
Expand Down Expand Up @@ -133,7 +132,7 @@ describe('ReactDOMRoot', () => {
<span />
</div>,
);
flush();
jest.runAllTimers();

// Accepts `hydrate` option
const container2 = document.createElement('div');
Expand All @@ -144,7 +143,7 @@ describe('ReactDOMRoot', () => {
<span />
</div>,
);
expect(flush).toWarnDev('Extra attributes');
expect(jest.runAllTimers).toWarnDev('Extra attributes');
});

it('does not clear existing children', async () => {
Expand All @@ -156,15 +155,15 @@ describe('ReactDOMRoot', () => {
<span>d</span>
</div>,
);
flush();
jest.runAllTimers();
expect(container.textContent).toEqual('abcd');
root.render(
<div>
<span>d</span>
<span>c</span>
</div>,
);
flush();
jest.runAllTimers();
expect(container.textContent).toEqual('abdc');
});

Expand Down Expand Up @@ -200,7 +199,7 @@ describe('ReactDOMRoot', () => {
</AsyncMode>,
);

flush();
jest.runAllTimers();

// Hasn't updated yet
expect(container.textContent).toEqual('');
Expand Down Expand Up @@ -229,7 +228,7 @@ describe('ReactDOMRoot', () => {
const batch = root.createBatch();
batch.render(<Foo>Hi</Foo>);
// Flush all async work.
flush();
jest.runAllTimers();
// Root should complete without committing.
expect(ops).toEqual(['Foo']);
expect(container.textContent).toEqual('');
Expand All @@ -247,7 +246,7 @@ describe('ReactDOMRoot', () => {
const batch = root.createBatch();
batch.render(<AsyncMode>Foo</AsyncMode>);

flush();
jest.runAllTimers();

// Hasn't updated yet
expect(container.textContent).toEqual('');
Expand Down Expand Up @@ -287,15 +286,15 @@ describe('ReactDOMRoot', () => {
const root = ReactDOM.unstable_createRoot(container);
root.render(<AsyncMode>1</AsyncMode>);

expire(2000);
advanceCurrentTime(2000);
// This batch has a later expiration time than the earlier update.
const batch = root.createBatch();

// This should not flush the earlier update.
batch.commit();
expect(container.textContent).toEqual('');

flush();
jest.runAllTimers();
expect(container.textContent).toEqual('1');
});

Expand All @@ -322,7 +321,7 @@ describe('ReactDOMRoot', () => {
batch1.render(1);

// This batch has a later expiration time
expire(2000);
advanceCurrentTime(2000);
const batch2 = root.createBatch();
batch2.render(2);

Expand All @@ -341,7 +340,7 @@ describe('ReactDOMRoot', () => {
batch1.render(1);

// This batch has a later expiration time
expire(2000);
advanceCurrentTime(2000);
const batch2 = root.createBatch();
batch2.render(2);

Expand All @@ -351,7 +350,7 @@ describe('ReactDOMRoot', () => {
expect(container.textContent).toEqual('2');

batch1.commit();
flush();
jest.runAllTimers();
expect(container.textContent).toEqual('1');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ describe('ChangeEventPlugin', () => {
let container;

beforeEach(() => {
// TODO pull this into helper method, reduce repetition.
// mock the browser APIs which are used in react-scheduler:
// - requestAnimationFrame should pass the DOMHighResTimeStamp argument
// - calling 'window.postMessage' should actually fire postmessage handlers
global.requestAnimationFrame = function(cb) {
return setTimeout(() => {
cb(Date.now());
});
};
const originalAddEventListener = global.addEventListener;
let postMessageCallback;
global.addEventListener = function(eventName, callback, useCapture) {
if (eventName === 'message') {
postMessageCallback = callback;
} else {
originalAddEventListener(eventName, callback, useCapture);
}
};
global.postMessage = function(messageKey, targetOrigin) {
const postMessageEvent = {source: window, data: messageKey};
if (postMessageCallback) {
postMessageCallback(postMessageEvent);
}
};
jest.resetModules();
container = document.createElement('div');
document.body.appendChild(container);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ describe('SimpleEventPlugin', function() {
}

beforeEach(function() {
// TODO pull this into helper method, reduce repetition.
// mock the browser APIs which are used in react-scheduler:
// - requestAnimationFrame should pass the DOMHighResTimeStamp argument
// - calling 'window.postMessage' should actually fire postmessage handlers
global.requestAnimationFrame = function(cb) {
return setTimeout(() => {
cb(Date.now());
});
};
const originalAddEventListener = global.addEventListener;
let postMessageCallback;
global.addEventListener = function(eventName, callback, useCapture) {
if (eventName === 'message') {
postMessageCallback = callback;
} else {
originalAddEventListener(eventName, callback, useCapture);
}
};
global.postMessage = function(messageKey, targetOrigin) {
const postMessageEvent = {source: window, data: messageKey};
if (postMessageCallback) {
postMessageCallback(postMessageEvent);
}
};
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactTestUtils = require('react-dom/test-utils');
Expand Down
13 changes: 3 additions & 10 deletions packages/react-scheduler/src/ReactScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import type {Deadline} from 'react-reconciler';

import {alwaysUseRequestIdleCallbackPolyfill} from 'shared/ReactFeatureFlags';
import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment';
import warning from 'fbjs/lib/warning';

Expand Down Expand Up @@ -85,12 +84,8 @@ if (!ExecutionEnvironment.canUseDOM) {
cIC = function(timeoutID: number) {
clearTimeout(timeoutID);
};
} else if (
alwaysUseRequestIdleCallbackPolyfill ||
typeof requestIdleCallback !== 'function' ||
typeof cancelIdleCallback !== 'function'
) {
// Polyfill requestIdleCallback and cancelIdleCallback
} else {
// Always polyfill requestIdleCallback and cancelIdleCallback

let scheduledRICCallback = null;
let isIdleScheduled = false;
Expand Down Expand Up @@ -175,6 +170,7 @@ if (!ExecutionEnvironment.canUseDOM) {
window.addEventListener('message', idleTick, false);

const animationTick = function(rafTime) {
console.log('animationTick called and rafTime is ', rafTime);
isAnimationFrameScheduled = false;
let nextFrameTime = rafTime - frameDeadline + activeFrameTime;
if (
Expand Down Expand Up @@ -231,9 +227,6 @@ if (!ExecutionEnvironment.canUseDOM) {
isIdleScheduled = false;
timeoutTime = -1;
};
} else {
rIC = window.requestIdleCallback;
cIC = window.cancelIdleCallback;
}

export {now, rIC, cIC};