Commit 70c085f7 authored by Jude Gao's avatar Jude Gao Committed by Jiachi Liu

clean up useReducer code re dev indicator (#77354)

Follow-up to https://github.com/vercel/next.js/pull/76976

Since we want to make explicit mode check from outside, we can now
remove the implicitly checked one from inside.
parent f99caa68
import { useEffect, useTransition } from 'react'
import { devRenderIndicator } from './dev-render-indicator'
export const useSyncDevRenderIndicatorInternal = () => {
const [isPending, startTransition] = useTransition()
useEffect(() => {
if (isPending) {
devRenderIndicator.show()
} else {
devRenderIndicator.hide()
}
}, [isPending])
return startTransition
}
const NOOP = (fn: () => void) => fn() import { useEffect, useTransition } from 'react'
import { devRenderIndicator } from './dev-render-indicator'
/**
* Returns a transition function that can be used to wrap router actions.
* This allows us to tap into the transition state of the router as an
* approximation of React render time.
*/
export const useSyncDevRenderIndicator = () => { export const useSyncDevRenderIndicator = () => {
let syncDevRenderIndicator = NOOP const [isPending, startTransition] = useTransition()
if (process.env.NODE_ENV === 'development') { useEffect(() => {
const { useSyncDevRenderIndicatorInternal } = if (isPending) {
require('./use-sync-dev-render-indicator-internal') as typeof import('./use-sync-dev-render-indicator-internal') devRenderIndicator.show()
} else {
devRenderIndicator.hide()
}
}, [isPending])
// eslint-disable-next-line react-hooks/rules-of-hooks return startTransition
syncDevRenderIndicator = useSyncDevRenderIndicatorInternal()
}
return syncDevRenderIndicator
} }
...@@ -17,36 +17,38 @@ export function useUnwrapState(state: ReducerState): AppRouterState { ...@@ -17,36 +17,38 @@ export function useUnwrapState(state: ReducerState): AppRouterState {
return state return state
} }
export function useReducer( export function useReducer(
actionQueue: AppRouterActionQueue actionQueue: AppRouterActionQueue
): [ReducerState, Dispatch<ReducerActions>] { ): [ReducerState, Dispatch<ReducerActions>] {
const [state, setState] = React.useState<ReducerState>(actionQueue.state) const [state, setState] = React.useState<ReducerState>(actionQueue.state)
const actionDispatch = (action: ReducerActions) => {
actionQueue.dispatch(action, setState)
}
let syncDevRenderIndicator const syncDevRenderIndicatorInDevelopment =
useSyncDevRenderIndicatorInDevelopment()
const dispatch = useCallback(
(action: ReducerActions) => {
syncDevRenderIndicatorInDevelopment(() =>
actionQueue.dispatch(action, setState)
)
},
[actionQueue, syncDevRenderIndicatorInDevelopment]
)
return [state, dispatch]
}
const PASS_THROUGH = (fn: () => void) => fn()
const useSyncDevRenderIndicatorInDevelopment = () => {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
const useSyncDevRenderIndicator = const useSyncDevRenderIndicator =
require('./react-dev-overlay/utils/dev-indicator/use-sync-dev-render-indicator') require('./react-dev-overlay/utils/dev-indicator/use-sync-dev-render-indicator')
.useSyncDevRenderIndicator as typeof import('./react-dev-overlay/utils/dev-indicator/use-sync-dev-render-indicator').useSyncDevRenderIndicator .useSyncDevRenderIndicator as typeof import('./react-dev-overlay/utils/dev-indicator/use-sync-dev-render-indicator').useSyncDevRenderIndicator
// eslint-disable-next-line react-hooks/rules-of-hooks // eslint-disable-next-line react-hooks/rules-of-hooks
syncDevRenderIndicator = useSyncDevRenderIndicator() const syncDevRenderIndicator = useSyncDevRenderIndicator()
return syncDevRenderIndicator
} else {
return PASS_THROUGH
} }
const dispatchCallback =
process.env.NODE_ENV !== 'production'
? (action: ReducerActions) => {
syncDevRenderIndicator!(() => actionDispatch(action))
}
: actionDispatch
const dispatch = useCallback(dispatchCallback, [
actionQueue,
dispatchCallback,
syncDevRenderIndicator,
])
return [state, dispatch]
} }
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