Skip to content

Commit

Permalink
act: Move didScheduleLegacyUpdate to ensureRootIsScheduled
Browse files Browse the repository at this point in the history
`act` uses the `didScheduleLegacyUpdate` field to simulate the behavior
of batching in React <17 and below. It's a quirk leftover from a
previous implementation, not intentionally designed.

This sets `didScheduleLegacyUpdate` every time a legacy root receives an
update as opposed to only when the `executionContext` is empty. There's
no real reason to do it this way over some other way except that it's
how it used to work before facebook#26512 and we should try our best to maintain
the existing behavior, quirks and all, since existing tests may have
come to accidentally rely on it.

This should fix some (though not all) of the internal Meta tests that
started failing after facebook#26512 landed.
  • Loading branch information
acdlite committed Apr 4, 2023
1 parent d5fd60f commit dac65c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/react-reconciler/src/ReactFiberRootScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ export function ensureRootIsScheduled(root: FiberRoot): void {
// unblock additional features we have planned.
scheduleTaskForRootDuringMicrotask(root, now());
}

if (
__DEV__ &&
ReactCurrentActQueue.isBatchingLegacy &&
root.tag === LegacyRoot
) {
// Special `act` case: Record whenever a legacy update is scheduled.
ReactCurrentActQueue.didScheduleLegacyUpdate = true;
}
}

export function flushSyncWorkOnAllRoots() {
Expand Down
1 change: 0 additions & 1 deletion packages/react-reconciler/src/ReactFiberWorkLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,6 @@ export function scheduleUpdateOnFiber(
) {
if (__DEV__ && ReactCurrentActQueue.isBatchingLegacy) {
// Treat `act` as if it's inside `batchedUpdates`, even in legacy mode.
ReactCurrentActQueue.didScheduleLegacyUpdate = true;
} else {
// Flush the synchronous work now, unless we're already working or inside
// a batch. This is intentionally inside scheduleUpdateOnFiber instead of
Expand Down

0 comments on commit dac65c1

Please sign in to comment.