@@ -12,6 +12,8 @@ import type {RendererTask} from './ReactCurrentActQueue';
12
12
import ReactCurrentActQueue from './ReactCurrentActQueue' ;
13
13
import queueMacrotask from 'shared/enqueueTask' ;
14
14
15
+ import { disableLegacyMode } from 'shared/ReactFeatureFlags' ;
16
+
15
17
// `act` calls can be nested, so we track the depth. This represents the
16
18
// number of `act` scopes on the stack.
17
19
let actScopeDepth = 0 ;
@@ -38,7 +40,9 @@ export function act<T>(callback: () => T | Thenable<T>): Thenable<T> {
38
40
// `act` calls can be nested.
39
41
//
40
42
// 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 ;
42
46
const prevActQueue = ReactCurrentActQueue . current ;
43
47
const prevActScopeDepth = actScopeDepth ;
44
48
actScopeDepth ++ ;
@@ -48,7 +52,9 @@ export function act<T>(callback: () => T | Thenable<T>): Thenable<T> {
48
52
// set to `true` while the given callback is executed, not for updates
49
53
// triggered during an async event, because this is how the legacy
50
54
// implementation of `act` behaved.
51
- ReactCurrentActQueue . isBatchingLegacy = true ;
55
+ if ( ! disableLegacyMode ) {
56
+ ReactCurrentActQueue . isBatchingLegacy = true ;
57
+ }
52
58
53
59
let result ;
54
60
// 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> {
58
64
// Reset this to `false` right before entering the React work loop. The
59
65
// only place we ever read this fields is just below, right after running
60
66
// 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
+ }
62
70
result = callback ( ) ;
63
- const didScheduleLegacyUpdate =
64
- ReactCurrentActQueue . didScheduleLegacyUpdate ;
71
+ const didScheduleLegacyUpdate = ! disableLegacyMode
72
+ ? ReactCurrentActQueue . didScheduleLegacyUpdate
73
+ : false ;
65
74
66
75
// Replicate behavior of original `act` implementation in legacy mode,
67
76
// which flushed updates immediately after the scope function exits, even
@@ -73,7 +82,9 @@ export function act<T>(callback: () => T | Thenable<T>): Thenable<T> {
73
82
// one used to track `act` scopes. Why, you may be wondering? Because
74
83
// that's how it worked before version 18. Yes, it's confusing! We should
75
84
// delete legacy mode!!
76
- ReactCurrentActQueue . isBatchingLegacy = prevIsBatchingLegacy ;
85
+ if ( ! disableLegacyMode ) {
86
+ ReactCurrentActQueue . isBatchingLegacy = prevIsBatchingLegacy ;
87
+ }
77
88
} catch ( error ) {
78
89
// `isBatchingLegacy` gets reset using the regular stack, not the async
79
90
// 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> {
82
93
ReactCurrentActQueue . thrownErrors . push ( error ) ;
83
94
}
84
95
if ( ReactCurrentActQueue . thrownErrors . length > 0 ) {
85
- ReactCurrentActQueue . isBatchingLegacy = prevIsBatchingLegacy ;
96
+ if ( ! disableLegacyMode ) {
97
+ ReactCurrentActQueue . isBatchingLegacy = prevIsBatchingLegacy ;
98
+ }
86
99
popActScope ( prevActQueue , prevActScopeDepth ) ;
87
100
const thrownError = aggregateErrors ( ReactCurrentActQueue . thrownErrors ) ;
88
101
ReactCurrentActQueue . thrownErrors . length = 0 ;
0 commit comments