-
Notifications
You must be signed in to change notification settings - Fork 60
fix: correct nested batch behavior. #218
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,23 +8,33 @@ import { | |
| import defaults from '../defaults'; | ||
| import supports from './supported-features'; | ||
|
|
||
| let isInsideBatchedSchedule = false; | ||
| let batchedScheduled = false; | ||
|
|
||
| let batched = []; | ||
|
|
||
| const executeBatched = () => { | ||
| unstable_batchedUpdates(() => { | ||
| while (batched.length) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be also written as: Or am I missing the reason why we would replace batched with an empty array?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a bad tendency for not using array modification methods as they are "slow". Simplifies code a lot, 👍 |
||
| const currentBatched = batched; | ||
| batched = []; | ||
| currentBatched.forEach((fn) => fn()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This now looks a lot like the queue logic in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds logical, batch can handle queue, but there will be a different in behavior
|
||
| } | ||
| // important to reset it before exiting this function | ||
| // as React will dump everything right after | ||
| batchedScheduled = false; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's maybe put this in a minor, but eager to test in Jira and see what breaks there 🙈
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It breaks one component doing read right after set in useEffect ( Removing |
||
| }); | ||
| }; | ||
|
|
||
| export function batch(fn) { | ||
| // if we are in node/tests or nested schedule | ||
| if ( | ||
| !defaults.batchUpdates || | ||
| !supports.scheduling() || | ||
| isInsideBatchedSchedule | ||
| ) { | ||
| if (!defaults.batchUpdates || !supports.scheduling()) { | ||
| return unstable_batchedUpdates(fn); | ||
| } | ||
|
|
||
| isInsideBatchedSchedule = true; | ||
| // Use ImmediatePriority as it has -1ms timeout | ||
| // https://github.com/facebook/react/blob/main/packages/scheduler/src/forks/Scheduler.js#L65 | ||
| return scheduleCallback(ImmediatePriority, function scheduleBatchedUpdates() { | ||
| unstable_batchedUpdates(fn); | ||
| isInsideBatchedSchedule = false; | ||
| }); | ||
| batched.push(fn); | ||
|
|
||
| if (!batchedScheduled) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that we handle batched inside the callback, do we need this check and
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. having |
||
| batchedScheduled = true; | ||
| return scheduleCallback(ImmediatePriority, executeBatched); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this mock would be called twice without the fix