GeezerVison is showing github.com — Async action support for React.startTransition (#28097) · EdisonVan/react@11bd519
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberAsyncAction.js +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ import type { | 13 | 13 | RejectedThenable, | 14 | 14 | } from 'shared/ReactTypes'; | 15 | 15 | import type {Lane} from './ReactFiberLane'; | | 16 | +import type {BatchConfigTransition} from './ReactFiberTracingMarkerComponent'; | 16 | 17 |
| 17 | 18 | import {requestTransitionLane} from './ReactFiberRootScheduler'; | 18 | 19 | import {NoLane} from './ReactFiberLane'; | @@ -36,15 +37,18 @@ let currentEntangledLane: Lane = NoLane; | 36 | 37 | // until the async action scope has completed. | 37 | 38 | let currentEntangledActionThenable: Thenable<void> | null = null; | 38 | 39 |
| 39 | | -export function entangleAsyncAction<S>(thenable: Thenable<S>): Thenable<S> { | | 40 | +export function entangleAsyncAction<S>( | | 41 | + transition: BatchConfigTransition, | | 42 | + thenable: Thenable<S>, | | 43 | +): Thenable<S> { | 40 | 44 | // `thenable` is the return value of the async action scope function. Create | 41 | 45 | // a combined thenable that resolves once every entangled scope function | 42 | 46 | // has finished. | 43 | 47 | if (currentEntangledListeners === null) { | 44 | 48 | // There's no outer async action scope. Create a new one. | 45 | 49 | const entangledListeners = (currentEntangledListeners = []); | 46 | 50 | currentEntangledPendingCount = 0; | 47 | | - currentEntangledLane = requestTransitionLane(); | | 51 | + currentEntangledLane = requestTransitionLane(transition); | 48 | 52 | const entangledThenable: Thenable<void> = { | 49 | 53 | status: 'pending', | 50 | 54 | value: undefined, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHooks.js +32-15Lines changed: 32 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -145,14 +145,17 @@ import { | 145 | 145 | import type {ThenableState} from './ReactFiberThenable'; | 146 | 146 | import type {BatchConfigTransition} from './ReactFiberTracingMarkerComponent'; | 147 | 147 | import { | 148 | | - entangleAsyncAction, | 149 | 148 | peekEntangledActionLane, | 150 | 149 | peekEntangledActionThenable, | 151 | 150 | chainThenableValue, | 152 | 151 | } from './ReactFiberAsyncAction'; | 153 | 152 | import {HostTransitionContext} from './ReactFiberHostContext'; | 154 | 153 | import {requestTransitionLane} from './ReactFiberRootScheduler'; | 155 | 154 | import {isCurrentTreeHidden} from './ReactFiberHiddenContext'; | | 155 | +import { | | 156 | + notifyTransitionCallbacks, | | 157 | + requestCurrentTransition, | | 158 | +} from './ReactFiberTransition'; | 156 | 159 |
| 157 | 160 | const {ReactCurrentDispatcher, ReactCurrentBatchConfig} = ReactSharedInternals; | 158 | 161 |
| @@ -1319,13 +1322,6 @@ function updateReducerImpl<S, A>( | 1319 | 1322 | } else { | 1320 | 1323 | // This update does have sufficient priority. | 1321 | 1324 |
| 1322 | | - // Check if this update is part of a pending async action. If so, | 1323 | | - // we'll need to suspend until the action has finished, so that it's | 1324 | | - // batched together with future updates in the same action. | 1325 | | - if (updateLane !== NoLane && updateLane === peekEntangledActionLane()) { | 1326 | | - didReadFromEntangledAsyncAction = true; | 1327 | | - } | 1328 | | - | 1329 | 1325 | // Check if this is an optimistic update. | 1330 | 1326 | const revertLane = update.revertLane; | 1331 | 1327 | if (!enableAsyncActions || revertLane === NoLane) { | @@ -1346,6 +1342,13 @@ function updateReducerImpl<S, A>( | 1346 | 1342 | }; | 1347 | 1343 | newBaseQueueLast = newBaseQueueLast.next = clone; | 1348 | 1344 | } | | 1345 | + | | 1346 | + // Check if this update is part of a pending async action. If so, | | 1347 | + // we'll need to suspend until the action has finished, so that it's | | 1348 | + // batched together with future updates in the same action. | | 1349 | + if (updateLane === peekEntangledActionLane()) { | | 1350 | + didReadFromEntangledAsyncAction = true; | | 1351 | + } | 1349 | 1352 | } else { | 1350 | 1353 | // This is an optimistic update. If the "revert" priority is | 1351 | 1354 | // sufficient, don't apply the update. Otherwise, apply the update, | @@ -1356,6 +1359,13 @@ function updateReducerImpl<S, A>( | 1356 | 1359 | // has finished. Pretend the update doesn't exist by skipping | 1357 | 1360 | // over it. | 1358 | 1361 | update = update.next; | | 1362 | + | | 1363 | + // Check if this update is part of a pending async action. If so, | | 1364 | + // we'll need to suspend until the action has finished, so that it's | | 1365 | + // batched together with future updates in the same action. | | 1366 | + if (revertLane === peekEntangledActionLane()) { | | 1367 | + didReadFromEntangledAsyncAction = true; | | 1368 | + } | 1359 | 1369 | continue; | 1360 | 1370 | } else { | 1361 | 1371 | const clone: Update<S, A> = { | @@ -1964,13 +1974,17 @@ function runFormStateAction<S, P>( | 1964 | 1974 |
| 1965 | 1975 | // This is a fork of startTransition | 1966 | 1976 | const prevTransition = ReactCurrentBatchConfig.transition; | 1967 | | - ReactCurrentBatchConfig.transition = ({}: BatchConfigTransition); | 1968 | | - const currentTransition = ReactCurrentBatchConfig.transition; | | 1977 | + const currentTransition: BatchConfigTransition = { | | 1978 | + _callbacks: new Set<(BatchConfigTransition, mixed) => mixed>(), | | 1979 | + }; | | 1980 | + ReactCurrentBatchConfig.transition = currentTransition; | 1969 | 1981 | if (__DEV__) { | 1970 | 1982 | ReactCurrentBatchConfig.transition._updatedFibers = new Set(); | 1971 | 1983 | } | 1972 | 1984 | try { | 1973 | 1985 | const returnValue = action(prevState, payload); | | 1986 | + notifyTransitionCallbacks(currentTransition, returnValue); | | 1987 | + | 1974 | 1988 | if ( | 1975 | 1989 | returnValue !== null && | 1976 | 1990 | typeof returnValue === 'object' && | @@ -1989,7 +2003,6 @@ function runFormStateAction<S, P>( | 1989 | 2003 | () => finishRunningFormStateAction(actionQueue, (setState: any)), | 1990 | 2004 | ); | 1991 | 2005 |
| 1992 | | - entangleAsyncAction<Awaited<S>>(thenable); | 1993 | 2006 | setState((thenable: any)); | 1994 | 2007 | } else { | 1995 | 2008 | setState((returnValue: any)); | @@ -2808,7 +2821,9 @@ function startTransition<S>( | 2808 | 2821 | ); | 2809 | 2822 |
| 2810 | 2823 | const prevTransition = ReactCurrentBatchConfig.transition; | 2811 | | - const currentTransition: BatchConfigTransition = {}; | | 2824 | + const currentTransition: BatchConfigTransition = { | | 2825 | + _callbacks: new Set<(BatchConfigTransition, mixed) => mixed>(), | | 2826 | + }; | 2812 | 2827 |
| 2813 | 2828 | if (enableAsyncActions) { | 2814 | 2829 | // We don't really need to use an optimistic update here, because we | @@ -2839,6 +2854,7 @@ function startTransition<S>( | 2839 | 2854 | try { | 2840 | 2855 | if (enableAsyncActions) { | 2841 | 2856 | const returnValue = callback(); | | 2857 | + notifyTransitionCallbacks(currentTransition, returnValue); | 2842 | 2858 |
| 2843 | 2859 | // Check if we're inside an async action scope. If so, we'll entangle | 2844 | 2860 | // this new action with the existing scope. | @@ -2854,7 +2870,6 @@ function startTransition<S>( | 2854 | 2870 | typeof returnValue.then === 'function' | 2855 | 2871 | ) { | 2856 | 2872 | const thenable = ((returnValue: any): Thenable<mixed>); | 2857 | | - entangleAsyncAction<mixed>(thenable); | 2858 | 2873 | // Create a thenable that resolves to `finishedState` once the async | 2859 | 2874 | // action has completed. | 2860 | 2875 | const thenableForFinishedState = chainThenableValue( | @@ -3281,8 +3296,10 @@ function dispatchOptimisticSetState<S, A>( | 3281 | 3296 | queue: UpdateQueue<S, A>, | 3282 | 3297 | action: A, | 3283 | 3298 | ): void { | | 3299 | + const transition = requestCurrentTransition(); | | 3300 | + | 3284 | 3301 | if (__DEV__) { | 3285 | | - if (ReactCurrentBatchConfig.transition === null) { | | 3302 | + if (transition === null) { | 3286 | 3303 | // An optimistic update occurred, but startTransition is not on the stack. | 3287 | 3304 | // There are two likely scenarios. | 3288 | 3305 |
| @@ -3323,7 +3340,7 @@ function dispatchOptimisticSetState<S, A>( | 3323 | 3340 | lane: SyncLane, | 3324 | 3341 | // After committing, the optimistic update is "reverted" using the same | 3325 | 3342 | // lane as the transition it's associated with. | 3326 | | - revertLane: requestTransitionLane(), | | 3343 | + revertLane: requestTransitionLane(transition), | 3327 | 3344 | action, | 3328 | 3345 | hasEagerState: false, | 3329 | 3346 | eagerState: null, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberRootScheduler.js +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 | import type {FiberRoot} from './ReactInternalTypes'; | 11 | 11 | import type {Lane} from './ReactFiberLane'; | 12 | 12 | import type {PriorityLevel} from 'scheduler/src/SchedulerPriorities'; | | 13 | +import type {BatchConfigTransition} from './ReactFiberTracingMarkerComponent'; | 13 | 14 |
| 14 | 15 | import {enableDeferRootSchedulingToMicrotask} from 'shared/ReactFeatureFlags'; | 15 | 16 | import { | @@ -492,7 +493,12 @@ function scheduleImmediateTask(cb: () => mixed) { | 492 | 493 | } | 493 | 494 | } | 494 | 495 |
| 495 | | -export function requestTransitionLane(): Lane { | | 496 | +export function requestTransitionLane( | | 497 | + // This argument isn't used, it's only here to encourage the caller to | | 498 | + // check that it's inside a transition before calling this function. | | 499 | + // TODO: Make this non-nullable. Requires a tweak to useOptimistic. | | 500 | + transition: BatchConfigTransition | null, | | 501 | +): Lane { | 496 | 502 | // The algorithm for assigning an update to a lane should be stable for all | 497 | 503 | // updates at the same priority within the same event. To do this, the | 498 | 504 | // inputs to the algorithm must be the same. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberTracingMarkerComponent.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,7 @@ export type PendingTransitionCallbacks = { | 36 | 36 | markerComplete: Map<string, Set<Transition>> | null, | 37 | 37 | }; | 38 | 38 |
| | 39 | +// TODO: Unclear to me why these are separate types | 39 | 40 | export type Transition = { | 40 | 41 | name: string, | 41 | 42 | startTime: number, | @@ -45,6 +46,7 @@ export type BatchConfigTransition = { | 45 | 46 | name?: string, | 46 | 47 | startTime?: number, | 47 | 48 | _updatedFibers?: Set<Fiber>, | | 49 | + _callbacks: Set<(BatchConfigTransition, mixed) => mixed>, | 48 | 50 | }; | 49 | 51 |
| 50 | 52 | // TODO: Is there a way to not include the tag or name here? | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberTransition.js +43-4Lines changed: 43 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | * @flow | 8 | 8 | */ | 9 | 9 | import type {Fiber, FiberRoot} from './ReactInternalTypes'; | | 10 | +import type {Thenable} from 'shared/ReactTypes'; | 10 | 11 | import type {Lanes} from './ReactFiberLane'; | 11 | 12 | import type {StackCursor} from './ReactFiberStack'; | 12 | 13 | import type {Cache, SpawnedCachePool} from './ReactFiberCacheComponent'; | 13 | | -import type {Transition} from './ReactFiberTracingMarkerComponent'; | | 14 | +import type { | | 15 | + BatchConfigTransition, | | 16 | + Transition, | | 17 | +} from './ReactFiberTracingMarkerComponent'; | 14 | 18 |
| 15 | | -import {enableCache, enableTransitionTracing} from 'shared/ReactFeatureFlags'; | | 19 | +import { | | 20 | + enableCache, | | 21 | + enableTransitionTracing, | | 22 | + enableAsyncActions, | | 23 | +} from 'shared/ReactFeatureFlags'; | 16 | 24 | import {isPrimaryRenderer} from './ReactFiberConfig'; | 17 | 25 | import {createCursor, push, pop} from './ReactFiberStack'; | 18 | 26 | import { | @@ -26,13 +34,44 @@ import { | 26 | 34 | } from './ReactFiberCacheComponent'; | 27 | 35 |
| 28 | 36 | import ReactSharedInternals from 'shared/ReactSharedInternals'; | | 37 | +import {entangleAsyncAction} from './ReactFiberAsyncAction'; | 29 | 38 |
| 30 | 39 | const {ReactCurrentBatchConfig} = ReactSharedInternals; | 31 | 40 |
| 32 | 41 | export const NoTransition = null; | 33 | 42 |
| 34 | | -export function requestCurrentTransition(): Transition | null { | 35 | | - return ReactCurrentBatchConfig.transition; | | 43 | +export function requestCurrentTransition(): BatchConfigTransition | null { | | 44 | + const transition = ReactCurrentBatchConfig.transition; | | 45 | + if (transition !== null) { | | 46 | + // Whenever a transition update is scheduled, register a callback on the | | 47 | + // transition object so we can get the return value of the scope function. | | 48 | + transition._callbacks.add(handleTransitionScopeResult); | | 49 | + } | | 50 | + return transition; | | 51 | +} | | 52 | + | | 53 | +function handleTransitionScopeResult( | | 54 | + transition: BatchConfigTransition, | | 55 | + returnValue: mixed, | | 56 | +): void { | | 57 | + if ( | | 58 | + enableAsyncActions && | | 59 | + returnValue !== null && | | 60 | + typeof returnValue === 'object' && | | 61 | + typeof returnValue.then === 'function' | | 62 | + ) { | | 63 | + // This is an async action. | | 64 | + const thenable: Thenable<mixed> = (returnValue: any); | | 65 | + entangleAsyncAction(transition, thenable); | | 66 | + } | | 67 | +} | | 68 | + | | 69 | +export function notifyTransitionCallbacks( | | 70 | + transition: BatchConfigTransition, | | 71 | + returnValue: mixed, | | 72 | +) { | | 73 | + const callbacks = transition._callbacks; | | 74 | + callbacks.forEach(callback => callback(transition, returnValue)); | 36 | 75 | } | 37 | 76 |
| 38 | 77 | // When retrying a Suspense/Offscreen boundary, we restore the cache that was | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.js +11-10Lines changed: 11 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -161,6 +161,7 @@ import { | 161 | 161 | OffscreenLane, | 162 | 162 | SyncUpdateLanes, | 163 | 163 | UpdateLanes, | | 164 | + claimNextTransitionLane, | 164 | 165 | } from './ReactFiberLane'; | 165 | 166 | import { | 166 | 167 | DiscreteEventPriority, | @@ -170,7 +171,7 @@ import { | 170 | 171 | lowerEventPriority, | 171 | 172 | lanesToEventPriority, | 172 | 173 | } from './ReactEventPriorities'; | 173 | | -import {requestCurrentTransition, NoTransition} from './ReactFiberTransition'; | | 174 | +import {requestCurrentTransition} from './ReactFiberTransition'; | 174 | 175 | import { | 175 | 176 | SelectiveHydrationException, | 176 | 177 | beginWork as originalBeginWork, | @@ -633,15 +634,15 @@ export function requestUpdateLane(fiber: Fiber): Lane { | 633 | 634 | return pickArbitraryLane(workInProgressRootRenderLanes); | 634 | 635 | } | 635 | 636 |
| 636 | | - const isTransition = requestCurrentTransition() !== NoTransition; | 637 | | - if (isTransition) { | 638 | | - if (__DEV__ && ReactCurrentBatchConfig.transition !== null) { | 639 | | - const transition = ReactCurrentBatchConfig.transition; | 640 | | - if (!transition._updatedFibers) { | 641 | | - transition._updatedFibers = new Set(); | | 637 | + const transition = requestCurrentTransition(); | | 638 | + if (transition !== null) { | | 639 | + if (__DEV__) { | | 640 | + const batchConfigTransition = ReactCurrentBatchConfig.transition; | | 641 | + if (!batchConfigTransition._updatedFibers) { | | 642 | + batchConfigTransition._updatedFibers = new Set(); | 642 | 643 | } | 643 | 644 |
| 644 | | - transition._updatedFibers.add(fiber); | | 645 | + batchConfigTransition._updatedFibers.add(fiber); | 645 | 646 | } | 646 | 647 |
| 647 | 648 | const actionScopeLane = peekEntangledActionLane(); | @@ -651,7 +652,7 @@ export function requestUpdateLane(fiber: Fiber): Lane { | 651 | 652 | : // We may or may not be inside an async action scope. If we are, this | 652 | 653 | // is the first update in that scope. Either way, we need to get a | 653 | 654 | // fresh transition lane. | 654 | | - requestTransitionLane(); | | 655 | + requestTransitionLane(transition); | 655 | 656 | } | 656 | 657 |
| 657 | 658 | // Updates originating inside certain React methods, like flushSync, have | @@ -712,7 +713,7 @@ export function requestDeferredLane(): Lane { | 712 | 713 | workInProgressDeferredLane = OffscreenLane; | 713 | 714 | } else { | 714 | 715 | // Everything else is spawned as a transition. | 715 | | - workInProgressDeferredLane = requestTransitionLane(); | | 716 | + workInProgressDeferredLane = claimNextTransitionLane(); | 716 | 717 | } | 717 | 718 | } | 718 | 719 |
| |
|