Unverified Commit a986e173 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Fix crash when suspending in Components using `useActionQueue` (#85459)

parent 40672141
...@@ -22,6 +22,12 @@ export function dispatchAppRouterAction(action: ReducerActions) { ...@@ -22,6 +22,12 @@ export function dispatchAppRouterAction(action: ReducerActions) {
dispatch(action) dispatch(action)
} }
const __DEV__ = process.env.NODE_ENV !== 'production'
const promisesWithDebugInfo: WeakMap<
Promise<AppRouterState>,
Promise<AppRouterState> & { _debugInfo?: Array<unknown> }
> = __DEV__ ? new WeakMap() : (null as any)
export function useActionQueue( export function useActionQueue(
actionQueue: AppRouterActionQueue actionQueue: AppRouterActionQueue
): AppRouterState { ): AppRouterState {
...@@ -56,15 +62,27 @@ export function useActionQueue( ...@@ -56,15 +62,27 @@ export function useActionQueue(
// passed to React (via `use`) so that the latency is accurately represented // passed to React (via `use`) so that the latency is accurately represented
// in the React DevTools. // in the React DevTools.
const stateWithDebugInfo = useMemo(() => { const stateWithDebugInfo = useMemo(() => {
if (!__DEV__) {
return state
}
if (isThenable(state)) { if (isThenable(state)) {
const debugInfo: Array<unknown> = [] // useMemo can't be used to cache a Promise since the memoized value is thrown
const promiseWithDebugInfo = Promise.resolve(state).then((asyncState) => { // away when we suspend. So we use a WeakMap to cache the Promise with debug info.
if (asyncState.debugInfo !== null) { let promiseWithDebugInfo = promisesWithDebugInfo.get(state)
debugInfo.push(...asyncState.debugInfo) if (promiseWithDebugInfo === undefined) {
} const debugInfo: Array<unknown> = []
return asyncState promiseWithDebugInfo = Promise.resolve(state).then((asyncState) => {
}) as Promise<AppRouterState> & { _debugInfo?: Array<unknown> } if (asyncState.debugInfo !== null) {
promiseWithDebugInfo._debugInfo = debugInfo debugInfo.push(...asyncState.debugInfo)
}
return asyncState
}) as Promise<AppRouterState> & { _debugInfo?: Array<unknown> }
promiseWithDebugInfo._debugInfo = debugInfo
promisesWithDebugInfo.set(state, promiseWithDebugInfo)
}
return promiseWithDebugInfo return promiseWithDebugInfo
} }
return state return state
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment