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

Add support for multiple callbacks in ReactScheduler #12682

Closed
wants to merge 14 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Restructure 'schedule' rIC code for clarity
**what is the change?:**
We previously had the following logic:
- pull any old callback out of the 'scheduledCallback' variable
- put the new callback into 'scheduledCallback'
- call the old callback if we had found one.

Splitting out the logic for handling the old callback into two blocks
was hard to read, so we restructured as so:
- if no old callback was found, just put the new callback into the
'scheduledCallback' variable.
- Otherwise, if we do find an old callback, then:
  - pull any old callback out of the 'scheduledCallback' variable
  - put the new callback into 'scheduledCallback'
  - call the old callback

**why make this change?:**
Code clarity

**test plan:**
Ran the tests
  • Loading branch information
flarnie committed May 2, 2018
commit 5ba093b8ffa2c7a27025d00a85a2b9c8c735b298
52 changes: 30 additions & 22 deletions packages/react-scheduler/src/ReactScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,29 +249,37 @@ if (!ExecutionEnvironment.canUseDOM) {
// having been called before it runs.
// So we call anything in the queue before the latest callback

let previouslyScheduledCallbackConfig;
if (scheduledCallbackConfig !== null) {
// If we have previous callback config, save it and handle it below
previouslyScheduledCallbackConfig = scheduledCallbackConfig;
}
// Then set up the next callback config
let timeoutTime = -1;
if (options != null && typeof options.timeout === 'number') {
timeoutTime = now() + options.timeout;
}
const latestCallbackId = getCallbackId();
scheduledCallbackConfig = {
scheduledCallback: callback,
timeoutTime,
callbackId: latestCallbackId,
};
registeredCallbackIds.set(latestCallbackId, true);
// If we have previousCallback, call it. This may trigger recursion.
if (
previouslyScheduledCallbackConfig &&
// make flow happy
typeof previouslyScheduledCallbackConfig.timeoutTime === 'number'
) {
if (scheduledCallbackConfig === null) {
// Set up the next callback config
let timeoutTime = -1;
if (options != null && typeof options.timeout === 'number') {
timeoutTime = now() + options.timeout;
}
scheduledCallbackConfig = {
scheduledCallback: callback,
timeoutTime,
callbackId: latestCallbackId,
};
registeredCallbackIds.set(latestCallbackId, true);
} else {
// If we have a previous callback config, we call that and then schedule
// the latest callback.
const previouslyScheduledCallbackConfig = scheduledCallbackConfig;

// Then set up the next callback config
let timeoutTime = -1;
if (options != null && typeof options.timeout === 'number') {
timeoutTime = now() + options.timeout;
}
scheduledCallbackConfig = {
scheduledCallback: callback,
timeoutTime,
callbackId: latestCallbackId,
};
registeredCallbackIds.set(latestCallbackId, true);

// If we have previousCallback, call it. This may trigger recursion.
const previousCallbackTimeout: number =
previouslyScheduledCallbackConfig.timeoutTime;
const previousCallback =
Expand Down