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) {
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(
actionQueue: AppRouterActionQueue
): AppRouterState {
......@@ -56,15 +62,27 @@ export function useActionQueue(
// passed to React (via `use`) so that the latency is accurately represented
// in the React DevTools.
const stateWithDebugInfo = useMemo(() => {
if (!__DEV__) {
return state
}
if (isThenable(state)) {
const debugInfo: Array<unknown> = []
const promiseWithDebugInfo = Promise.resolve(state).then((asyncState) => {
if (asyncState.debugInfo !== null) {
debugInfo.push(...asyncState.debugInfo)
}
return asyncState
}) as Promise<AppRouterState> & { _debugInfo?: Array<unknown> }
promiseWithDebugInfo._debugInfo = debugInfo
// useMemo can't be used to cache a Promise since the memoized value is thrown
// away when we suspend. So we use a WeakMap to cache the Promise with debug info.
let promiseWithDebugInfo = promisesWithDebugInfo.get(state)
if (promiseWithDebugInfo === undefined) {
const debugInfo: Array<unknown> = []
promiseWithDebugInfo = Promise.resolve(state).then((asyncState) => {
if (asyncState.debugInfo !== null) {
debugInfo.push(...asyncState.debugInfo)
}
return asyncState
}) as Promise<AppRouterState> & { _debugInfo?: Array<unknown> }
promiseWithDebugInfo._debugInfo = debugInfo
promisesWithDebugInfo.set(state, promiseWithDebugInfo)
}
return promiseWithDebugInfo
}
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