-
Notifications
You must be signed in to change notification settings - Fork 27.1k
/
use-reducer-with-devtools.ts
167 lines (143 loc) · 4.26 KB
/
use-reducer-with-devtools.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import type { Dispatch } from 'react'
import React, { use, useContext } from 'react'
import { useRef, useEffect, useCallback } from 'react'
import {
isThenable,
type AppRouterState,
type ReducerActions,
type ReducerState,
} from './router-reducer/router-reducer-types'
import { ActionQueueContext } from '../../shared/lib/router/action-queue'
function normalizeRouterState(val: any): any {
if (val instanceof Map) {
const obj: { [key: string]: any } = {}
for (const [key, value] of val.entries()) {
if (typeof value === 'function') {
obj[key] = 'fn()'
continue
}
if (typeof value === 'object' && value !== null) {
if (value.$$typeof) {
obj[key] = value.$$typeof.toString()
continue
}
if (value._bundlerConfig) {
obj[key] = 'FlightData'
continue
}
}
obj[key] = normalizeRouterState(value)
}
return obj
}
if (typeof val === 'object' && val !== null) {
const obj: { [key: string]: any } = {}
for (const key in val) {
const value = val[key]
if (typeof value === 'function') {
obj[key] = 'fn()'
continue
}
if (typeof value === 'object' && value !== null) {
if (value.$$typeof) {
obj[key] = value.$$typeof.toString()
continue
}
if (value.hasOwnProperty('_bundlerConfig')) {
obj[key] = 'FlightData'
continue
}
}
obj[key] = normalizeRouterState(value)
}
return obj
}
if (Array.isArray(val)) {
return val.map(normalizeRouterState)
}
return val
}
declare global {
interface Window {
__REDUX_DEVTOOLS_EXTENSION__: any
}
}
export interface ReduxDevToolsInstance {
send(action: any, state: any): void
init(initialState: any): void
}
export function useUnwrapState(state: ReducerState) {
// reducer actions can be async, so sometimes we need to suspend until the state is resolved
if (isThenable(state)) {
const result = use(state)
return result
}
return state
}
function useReducerWithReduxDevtoolsNoop(
initialState: AppRouterState
): [ReducerState, Dispatch<ReducerActions>, () => void] {
return [initialState, () => {}, () => {}]
}
function useReducerWithReduxDevtoolsImpl(
initialState: AppRouterState
): [ReducerState, Dispatch<ReducerActions>, () => void] {
const [state, setState] = React.useState<ReducerState>(initialState)
const actionQueue = useContext(ActionQueueContext)
if (!actionQueue) {
throw new Error('Invariant: Missing ActionQueueContext')
}
const devtoolsConnectionRef = useRef<ReduxDevToolsInstance>()
const enabledRef = useRef<boolean>()
useEffect(() => {
if (devtoolsConnectionRef.current || enabledRef.current === false) {
return
}
if (
enabledRef.current === undefined &&
typeof window.__REDUX_DEVTOOLS_EXTENSION__ === 'undefined'
) {
enabledRef.current = false
return
}
devtoolsConnectionRef.current = window.__REDUX_DEVTOOLS_EXTENSION__.connect(
{
instanceId: 8000, // Random number that is high to avoid conflicts
name: 'next-router',
}
)
if (devtoolsConnectionRef.current) {
devtoolsConnectionRef.current.init(normalizeRouterState(initialState))
if (actionQueue) {
actionQueue.devToolsInstance = devtoolsConnectionRef.current
}
}
return () => {
devtoolsConnectionRef.current = undefined
}
}, [initialState, actionQueue])
const dispatch = useCallback(
(action: ReducerActions) => {
if (!actionQueue.state) {
// we lazy initialize the mutable action queue state since the data needed
// to generate the state is not available when the actionQueue context is created
actionQueue.state = initialState
}
actionQueue.dispatch(action, setState)
},
[actionQueue, initialState]
)
const sync = useCallback(() => {
if (devtoolsConnectionRef.current) {
devtoolsConnectionRef.current.send(
{ type: 'RENDER_SYNC' },
normalizeRouterState(state)
)
}
}, [state])
return [state, dispatch, sync]
}
export const useReducerWithReduxDevtools =
typeof window !== 'undefined'
? useReducerWithReduxDevtoolsImpl
: useReducerWithReduxDevtoolsNoop