Skip to content

Commit 3d8f11c

Browse files
committed
Get legacy ReactCurrentActQueue checks
1 parent eb17de0 commit 3d8f11c

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

packages/react/src/ReactAct.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import type {RendererTask} from './ReactCurrentActQueue';
1212
import ReactCurrentActQueue from './ReactCurrentActQueue';
1313
import queueMacrotask from 'shared/enqueueTask';
1414

15+
import {disableLegacyMode} from 'shared/ReactFeatureFlags';
16+
1517
// `act` calls can be nested, so we track the depth. This represents the
1618
// number of `act` scopes on the stack.
1719
let actScopeDepth = 0;
@@ -38,7 +40,9 @@ export function act<T>(callback: () => T | Thenable<T>): Thenable<T> {
3840
// `act` calls can be nested.
3941
//
4042
// If we're already inside an `act` scope, reuse the existing queue.
41-
const prevIsBatchingLegacy = ReactCurrentActQueue.isBatchingLegacy;
43+
const prevIsBatchingLegacy = !disableLegacyMode
44+
? ReactCurrentActQueue.isBatchingLegacy
45+
: false;
4246
const prevActQueue = ReactCurrentActQueue.current;
4347
const prevActScopeDepth = actScopeDepth;
4448
actScopeDepth++;
@@ -48,7 +52,9 @@ export function act<T>(callback: () => T | Thenable<T>): Thenable<T> {
4852
// set to `true` while the given callback is executed, not for updates
4953
// triggered during an async event, because this is how the legacy
5054
// implementation of `act` behaved.
51-
ReactCurrentActQueue.isBatchingLegacy = true;
55+
if (!disableLegacyMode) {
56+
ReactCurrentActQueue.isBatchingLegacy = true;
57+
}
5258

5359
let result;
5460
// This tracks whether the `act` call is awaited. In certain cases, not
@@ -58,10 +64,13 @@ export function act<T>(callback: () => T | Thenable<T>): Thenable<T> {
5864
// Reset this to `false` right before entering the React work loop. The
5965
// only place we ever read this fields is just below, right after running
6066
// the callback. So we don't need to reset after the callback runs.
61-
ReactCurrentActQueue.didScheduleLegacyUpdate = false;
67+
if (!disableLegacyMode) {
68+
ReactCurrentActQueue.didScheduleLegacyUpdate = false;
69+
}
6270
result = callback();
63-
const didScheduleLegacyUpdate =
64-
ReactCurrentActQueue.didScheduleLegacyUpdate;
71+
const didScheduleLegacyUpdate = !disableLegacyMode
72+
? ReactCurrentActQueue.didScheduleLegacyUpdate
73+
: false;
6574

6675
// Replicate behavior of original `act` implementation in legacy mode,
6776
// which flushed updates immediately after the scope function exits, even
@@ -73,7 +82,9 @@ export function act<T>(callback: () => T | Thenable<T>): Thenable<T> {
7382
// one used to track `act` scopes. Why, you may be wondering? Because
7483
// that's how it worked before version 18. Yes, it's confusing! We should
7584
// delete legacy mode!!
76-
ReactCurrentActQueue.isBatchingLegacy = prevIsBatchingLegacy;
85+
if (!disableLegacyMode) {
86+
ReactCurrentActQueue.isBatchingLegacy = prevIsBatchingLegacy;
87+
}
7788
} catch (error) {
7889
// `isBatchingLegacy` gets reset using the regular stack, not the async
7990
// one used to track `act` scopes. Why, you may be wondering? Because
@@ -82,7 +93,9 @@ export function act<T>(callback: () => T | Thenable<T>): Thenable<T> {
8293
ReactCurrentActQueue.thrownErrors.push(error);
8394
}
8495
if (ReactCurrentActQueue.thrownErrors.length > 0) {
85-
ReactCurrentActQueue.isBatchingLegacy = prevIsBatchingLegacy;
96+
if (!disableLegacyMode) {
97+
ReactCurrentActQueue.isBatchingLegacy = prevIsBatchingLegacy;
98+
}
8699
popActScope(prevActQueue, prevActScopeDepth);
87100
const thrownError = aggregateErrors(ReactCurrentActQueue.thrownErrors);
88101
ReactCurrentActQueue.thrownErrors.length = 0;

0 commit comments

Comments
 (0)