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

Rename Scheduler methods more accurately #12770

Merged
merged 4 commits into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-art/src/ReactARTHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ const ReactARTHostConfig = {
return emptyObject;
},

scheduleDeferredCallback: ReactScheduler.rIC,
scheduleDeferredCallback: ReactScheduler.scheduleWork,

shouldSetTextContent(type, props) {
return (
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
**
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
Expand Down
4 changes: 2 additions & 2 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,8 @@ const ReactDOMHostConfig = {
},
},

scheduleDeferredCallback: ReactScheduler.rIC,
cancelDeferredCallback: ReactScheduler.cIC,
scheduleDeferredCallback: ReactScheduler.scheduleWork,
cancelDeferredCallback: ReactScheduler.cancelScheduledWork,
};

export default ReactDOMHostConfig;
22 changes: 11 additions & 11 deletions packages/react-scheduler/src/ReactScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* A scheduling library to allow scheduling work with more granular priority and
* control than requestAnimationFrame and requestIdleCallback.
* Current TODO items:
* X- Pull out the rIC polyfill built into React
* X- Pull out the scheduleWork polyfill built into React
* X- Initial test coverage
* X- Support for multiple callbacks
* - Support for two priorities; serial and deferred
Expand Down Expand Up @@ -68,13 +68,13 @@ if (hasNativePerformanceNow) {
}

// TODO: There's no way to cancel, because Fiber doesn't atm.
let rIC: (
let scheduleWork: (
callback: (deadline: Deadline, options?: {timeout: number}) => void,
) => number;
let cIC: (callbackID: number) => void;
let cancelScheduledWork: (callbackID: number) => void;

if (!ExecutionEnvironment.canUseDOM) {
rIC = function(
scheduleWork = function(
frameCallback: (deadline: Deadline, options?: {timeout: number}) => void,
): number {
return setTimeout(() => {
Expand All @@ -86,12 +86,12 @@ if (!ExecutionEnvironment.canUseDOM) {
});
});
};
cIC = function(timeoutID: number) {
cancelScheduledWork = function(timeoutID: number) {
clearTimeout(timeoutID);
};
} else {
// We keep callbacks in a queue.
// Calling rIC will push in a new callback at the end of the queue.
// Calling scheduleWork will push in a new callback at the end of the queue.
// When we get idle time, callbacks are removed from the front of the queue
// and called.
const pendingCallbacks: Array<CallbackConfigType> = [];
Expand All @@ -104,7 +104,7 @@ if (!ExecutionEnvironment.canUseDOM) {

// When a callback is scheduled, we register it by adding it's id to this
// object.
// If the user calls 'cIC' with the id of that callback, it will be
// If the user calls 'cancelScheduledWork' with the id of that callback, it will be
// unregistered by removing the id from this object.
// Then we skip calling any callback which is not registered.
// This means cancelling is an O(1) time complexity instead of O(n).
Expand Down Expand Up @@ -265,7 +265,7 @@ if (!ExecutionEnvironment.canUseDOM) {
}
};

rIC = function(
scheduleWork = function(
callback: (deadline: Deadline) => void,
options?: {timeout: number},
): number {
Expand All @@ -289,17 +289,17 @@ if (!ExecutionEnvironment.canUseDOM) {
if (!isAnimationFrameScheduled) {
// If rAF didn't already schedule one, we need to schedule a frame.
// TODO: If this rAF doesn't materialize because the browser throttles, we
// might want to still have setTimeout trigger rIC as a backup to ensure
// might want to still have setTimeout trigger scheduleWork as a backup to ensure
// that we keep performing work.
isAnimationFrameScheduled = true;
requestAnimationFrame(animationTick);
}
return newCallbackId;
};

cIC = function(callbackId: number) {
cancelScheduledWork = function(callbackId: number) {
delete registeredCallbackIds[callbackId];
};
}

export {now, rIC, cIC};
export {now, scheduleWork, cancelScheduledWork};
62 changes: 31 additions & 31 deletions packages/react-scheduler/src/__tests__/ReactScheduler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ describe('ReactScheduler', () => {
ReactScheduler = require('react-scheduler');
});

describe('rIC', () => {
describe('scheduleWork', () => {
it('calls the callback within the frame when not blocked', () => {
const {rIC} = ReactScheduler;
const {scheduleWork} = ReactScheduler;
const cb = jest.fn();
rIC(cb);
scheduleWork(cb);
jest.runAllTimers();
expect(cb.mock.calls.length).toBe(1);
// should not have timed out and should include a timeRemaining method
Expand All @@ -55,15 +55,15 @@ describe('ReactScheduler', () => {

describe('with multiple callbacks', () => {
it('accepts multiple callbacks and calls within frame when not blocked', () => {
const {rIC} = ReactScheduler;
const {scheduleWork} = ReactScheduler;
const callbackLog = [];
const callbackA = jest.fn(() => callbackLog.push('A'));
const callbackB = jest.fn(() => callbackLog.push('B'));
rIC(callbackA);
scheduleWork(callbackA);
// initially waits to call the callback
expect(callbackLog).toEqual([]);
// waits while second callback is passed
rIC(callbackB);
scheduleWork(callbackB);
expect(callbackLog).toEqual([]);
// after a delay, calls as many callbacks as it has time for
jest.runAllTimers();
Expand All @@ -84,11 +84,11 @@ describe('ReactScheduler', () => {
'schedules callbacks in correct order and' +
'keeps calling them if there is time',
() => {
const {rIC} = ReactScheduler;
const {scheduleWork} = ReactScheduler;
const callbackLog = [];
const callbackA = jest.fn(() => {
callbackLog.push('A');
rIC(callbackC);
scheduleWork(callbackC);
});
const callbackB = jest.fn(() => {
callbackLog.push('B');
Expand All @@ -97,11 +97,11 @@ describe('ReactScheduler', () => {
callbackLog.push('C');
});

rIC(callbackA);
scheduleWork(callbackA);
// initially waits to call the callback
expect(callbackLog).toEqual([]);
// continues waiting while B is scheduled
rIC(callbackB);
scheduleWork(callbackB);
expect(callbackLog).toEqual([]);
// after a delay, calls the scheduled callbacks,
// and also calls new callbacks scheduled by current callbacks
Expand All @@ -110,18 +110,18 @@ describe('ReactScheduler', () => {
},
);

it('schedules callbacks in correct order when callbacks have many nested rIC calls', () => {
const {rIC} = ReactScheduler;
it('schedules callbacks in correct order when callbacks have many nested scheduleWork calls', () => {
const {scheduleWork} = ReactScheduler;
const callbackLog = [];
const callbackA = jest.fn(() => {
callbackLog.push('A');
rIC(callbackC);
rIC(callbackD);
scheduleWork(callbackC);
scheduleWork(callbackD);
});
const callbackB = jest.fn(() => {
callbackLog.push('B');
rIC(callbackE);
rIC(callbackF);
scheduleWork(callbackE);
scheduleWork(callbackF);
});
const callbackC = jest.fn(() => {
callbackLog.push('C');
Expand All @@ -136,32 +136,32 @@ describe('ReactScheduler', () => {
callbackLog.push('F');
});

rIC(callbackA);
rIC(callbackB);
scheduleWork(callbackA);
scheduleWork(callbackB);
// initially waits to call the callback
expect(callbackLog).toEqual([]);
// while flushing callbacks, calls as many as it has time for
jest.runAllTimers();
expect(callbackLog).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
});

it('schedules callbacks in correct order when they use rIC to schedule themselves', () => {
const {rIC} = ReactScheduler;
it('schedules callbacks in correct order when they use scheduleWork to schedule themselves', () => {
const {scheduleWork} = ReactScheduler;
const callbackLog = [];
let callbackAIterations = 0;
const callbackA = jest.fn(() => {
if (callbackAIterations < 1) {
rIC(callbackA);
scheduleWork(callbackA);
}
callbackLog.push('A' + callbackAIterations);
callbackAIterations++;
});
const callbackB = jest.fn(() => callbackLog.push('B'));

rIC(callbackA);
scheduleWork(callbackA);
// initially waits to call the callback
expect(callbackLog).toEqual([]);
rIC(callbackB);
scheduleWork(callbackB);
expect(callbackLog).toEqual([]);
// after a delay, calls the latest callback passed
jest.runAllTimers();
Expand All @@ -170,29 +170,29 @@ describe('ReactScheduler', () => {
});
});

describe('cIC', () => {
describe('cancelScheduledWork', () => {
it('cancels the scheduled callback', () => {
const {rIC, cIC} = ReactScheduler;
const {scheduleWork, cancelScheduledWork} = ReactScheduler;
const cb = jest.fn();
const callbackId = rIC(cb);
const callbackId = scheduleWork(cb);
expect(cb.mock.calls.length).toBe(0);
cIC(callbackId);
cancelScheduledWork(callbackId);
jest.runAllTimers();
expect(cb.mock.calls.length).toBe(0);
});

describe('with multiple callbacks', () => {
it('when one callback cancels the next one', () => {
const {rIC, cIC} = ReactScheduler;
const {scheduleWork, cancelScheduledWork} = ReactScheduler;
const callbackLog = [];
let callbackBId;
const callbackA = jest.fn(() => {
callbackLog.push('A');
cIC(callbackBId);
cancelScheduledWork(callbackBId);
});
const callbackB = jest.fn(() => callbackLog.push('B'));
rIC(callbackA);
callbackBId = rIC(callbackB);
scheduleWork(callbackA);
callbackBId = scheduleWork(callbackB);
// Initially doesn't call anything
expect(callbackLog).toEqual([]);
jest.runAllTimers();
Expand Down