From 8d5cba2525f2879fd6db36041f84364bdd69eebc Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Tue, 2 Apr 2024 17:18:01 -0400 Subject: [PATCH] Get legacy ReactCurrentActQueue checks --- packages/react/src/ReactAct.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/react/src/ReactAct.js b/packages/react/src/ReactAct.js index 269c0b2de2eda..6a98a21ac29dd 100644 --- a/packages/react/src/ReactAct.js +++ b/packages/react/src/ReactAct.js @@ -12,6 +12,8 @@ import type {RendererTask} from './ReactCurrentActQueue'; import ReactCurrentActQueue from './ReactCurrentActQueue'; import queueMacrotask from 'shared/enqueueTask'; +import {disableLegacyMode} from 'shared/ReactFeatureFlags'; + // `act` calls can be nested, so we track the depth. This represents the // number of `act` scopes on the stack. let actScopeDepth = 0; @@ -38,7 +40,9 @@ export function act(callback: () => T | Thenable): Thenable { // `act` calls can be nested. // // If we're already inside an `act` scope, reuse the existing queue. - const prevIsBatchingLegacy = ReactCurrentActQueue.isBatchingLegacy; + const prevIsBatchingLegacy = !disableLegacyMode + ? ReactCurrentActQueue.isBatchingLegacy + : false; const prevActQueue = ReactCurrentActQueue.current; const prevActScopeDepth = actScopeDepth; actScopeDepth++; @@ -48,7 +52,9 @@ export function act(callback: () => T | Thenable): Thenable { // set to `true` while the given callback is executed, not for updates // triggered during an async event, because this is how the legacy // implementation of `act` behaved. - ReactCurrentActQueue.isBatchingLegacy = true; + if (!disableLegacyMode) { + ReactCurrentActQueue.isBatchingLegacy = true; + } let result; // This tracks whether the `act` call is awaited. In certain cases, not @@ -58,10 +64,13 @@ export function act(callback: () => T | Thenable): Thenable { // Reset this to `false` right before entering the React work loop. The // only place we ever read this fields is just below, right after running // the callback. So we don't need to reset after the callback runs. - ReactCurrentActQueue.didScheduleLegacyUpdate = false; + if (!disableLegacyMode) { + ReactCurrentActQueue.didScheduleLegacyUpdate = false; + } result = callback(); - const didScheduleLegacyUpdate = - ReactCurrentActQueue.didScheduleLegacyUpdate; + const didScheduleLegacyUpdate = !disableLegacyMode + ? ReactCurrentActQueue.didScheduleLegacyUpdate + : false; // Replicate behavior of original `act` implementation in legacy mode, // which flushed updates immediately after the scope function exits, even @@ -73,7 +82,9 @@ export function act(callback: () => T | Thenable): Thenable { // one used to track `act` scopes. Why, you may be wondering? Because // that's how it worked before version 18. Yes, it's confusing! We should // delete legacy mode!! - ReactCurrentActQueue.isBatchingLegacy = prevIsBatchingLegacy; + if (!disableLegacyMode) { + ReactCurrentActQueue.isBatchingLegacy = prevIsBatchingLegacy; + } } catch (error) { // `isBatchingLegacy` gets reset using the regular stack, not the async // one used to track `act` scopes. Why, you may be wondering? Because @@ -82,7 +93,9 @@ export function act(callback: () => T | Thenable): Thenable { ReactCurrentActQueue.thrownErrors.push(error); } if (ReactCurrentActQueue.thrownErrors.length > 0) { - ReactCurrentActQueue.isBatchingLegacy = prevIsBatchingLegacy; + if (!disableLegacyMode) { + ReactCurrentActQueue.isBatchingLegacy = prevIsBatchingLegacy; + } popActScope(prevActQueue, prevActScopeDepth); const thrownError = aggregateErrors(ReactCurrentActQueue.thrownErrors); ReactCurrentActQueue.thrownErrors.length = 0;