forked from pmndrs/zustand
-
Notifications
You must be signed in to change notification settings - Fork 1
/
devtools.ts
439 lines (406 loc) · 13 KB
/
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import type {} from '@redux-devtools/extension'
import type { StateCreator, StoreApi, StoreMutatorIdentifier } from '../vanilla'
// Copy types to avoid import type { Config } from '@redux-devtools/extension'
// https://github.com/pmndrs/zustand/issues/1205
type Action<T = any> = {
type: T
}
type ActionCreator<A, P extends any[] = any[]> = {
(...args: P): A
}
type EnhancerOptions = {
name?: string
actionCreators?:
| ActionCreator<any>[]
| {
[key: string]: ActionCreator<any>
}
latency?: number
maxAge?: number
serialize?:
| boolean
| {
options?:
| undefined
| boolean
| {
date?: true
regex?: true
undefined?: true
error?: true
symbol?: true
map?: true
set?: true
function?: true | ((fn: (...args: any[]) => any) => string)
}
replacer?: (key: string, value: unknown) => any
reviver?: (key: string, value: unknown) => any
immutable?: any
refs?: any
}
actionSanitizer?: <A extends Action>(action: A, id: number) => A
stateSanitizer?: <S>(state: S, index: number) => S
actionsBlacklist?: string | string[]
actionsWhitelist?: string | string[]
actionsDenylist?: string | string[]
actionsAllowlist?: string | string[]
predicate?: <S, A extends Action>(state: S, action: A) => boolean
shouldRecordChanges?: boolean
pauseActionType?: string
autoPause?: boolean
shouldStartLocked?: boolean
shouldHotReload?: boolean
shouldCatchErrors?: boolean
features?: {
pause?: boolean
lock?: boolean
persist?: boolean
export?: boolean | 'custom'
import?: boolean | 'custom'
jump?: boolean
skip?: boolean
reorder?: boolean
dispatch?: boolean
test?: boolean
}
trace?: boolean | (<A extends Action>(action: A) => string)
traceLimit?: number
}
type Config = EnhancerOptions & {
type?: string
}
declare module '../vanilla' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface StoreMutators<S, A> {
'zustand/devtools': WithDevtools<S>
}
}
// FIXME https://github.com/reduxjs/redux-devtools/issues/1097
type Message = {
type: string
payload?: any
state?: any
}
type Cast<T, U> = T extends U ? T : U
type Write<T, U> = Omit<T, keyof U> & U
type TakeTwo<T> = T extends { length: 0 }
? [undefined, undefined]
: T extends { length: 1 }
? [...a0: Cast<T, unknown[]>, a1: undefined]
: T extends { length: 0 | 1 }
? [...a0: Cast<T, unknown[]>, a1: undefined]
: T extends { length: 2 }
? T
: T extends { length: 1 | 2 }
? T
: T extends { length: 0 | 1 | 2 }
? T
: T extends [infer A0, infer A1, ...unknown[]]
? [A0, A1]
: T extends [infer A0, (infer A1)?, ...unknown[]]
? [A0, A1?]
: T extends [(infer A0)?, (infer A1)?, ...unknown[]]
? [A0?, A1?]
: never
type WithDevtools<S> = Write<S, StoreDevtools<S>>
type StoreDevtools<S> = S extends {
setState: (...a: infer Sa) => infer Sr
}
? {
setState<A extends string | { type: unknown }>(
...a: [...a: TakeTwo<Sa>, action?: A]
): Sr
}
: never
export interface DevtoolsOptions extends Config {
enabled?: boolean
anonymousActionType?: string
store?: string
}
type Devtools = <
T,
Mps extends [StoreMutatorIdentifier, unknown][] = [],
Mcs extends [StoreMutatorIdentifier, unknown][] = []
>(
initializer: StateCreator<T, [...Mps, ['zustand/devtools', never]], Mcs>,
devtoolsOptions?: DevtoolsOptions
) => StateCreator<T, Mps, [['zustand/devtools', never], ...Mcs]>
declare module '../vanilla' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface StoreMutators<S, A> {
'zustand/devtools': WithDevtools<S>
}
}
type DevtoolsImpl = <T>(
storeInitializer: StateCreator<T, [], []>,
devtoolsOptions?: DevtoolsOptions
) => StateCreator<T, [], []>
export type NamedSet<T> = WithDevtools<StoreApi<T>>['setState']
type Connection = ReturnType<
NonNullable<Window['__REDUX_DEVTOOLS_EXTENSION__']>['connect']
>
type ConnectionName = string | undefined
type StoreName = string
type StoreInformation = StoreApi<unknown>
type ConnectionInformation = {
connection: Connection
stores: Record<StoreName, StoreInformation>
}
const trackedConnections: Map<ConnectionName, ConnectionInformation> = new Map()
const getTrackedConnectionState = (
name: string | undefined
): Record<string, any> => {
const api = trackedConnections.get(name)
if (!api) return {}
return Object.fromEntries(
Object.entries(api.stores).map(([key, api]) => [key, api.getState()])
)
}
const extractConnectionInformation = (
store: string | undefined,
extensionConnector: NonNullable<
typeof window['__REDUX_DEVTOOLS_EXTENSION__']
>,
options: Omit<DevtoolsOptions, 'enabled' | 'anonymousActionType' | 'store'>
) => {
if (store === undefined) {
return {
type: 'untracked' as const,
connection: extensionConnector.connect(options),
}
}
const existingConnection = trackedConnections.get(options.name)
if (existingConnection) {
return { type: 'tracked' as const, store, ...existingConnection }
}
const newConnection: ConnectionInformation = {
connection: extensionConnector.connect(options),
stores: {},
}
trackedConnections.set(options.name, newConnection)
return { type: 'tracked' as const, store, ...newConnection }
}
const devtoolsImpl: DevtoolsImpl =
(fn, devtoolsOptions = {}) =>
(set, get, api) => {
const { enabled, anonymousActionType, store, ...options } = devtoolsOptions
type S = ReturnType<typeof fn> & {
[store: string]: ReturnType<typeof fn>
}
type PartialState = Partial<S> | ((s: S) => Partial<S>)
let extensionConnector:
| typeof window['__REDUX_DEVTOOLS_EXTENSION__']
| false
try {
extensionConnector =
(enabled ?? __DEV__) && window.__REDUX_DEVTOOLS_EXTENSION__
} catch {
// ignored
}
if (!extensionConnector) {
if (__DEV__ && enabled) {
console.warn(
'[zustand devtools middleware] Please install/enable Redux devtools extension'
)
}
return fn(set, get, api)
}
const { connection, ...connectionInformation } =
extractConnectionInformation(store, extensionConnector, options)
let isRecording = true
;(api.setState as NamedSet<S>) = (state, replace, nameOrAction) => {
const r = set(state, replace)
if (!isRecording) return r
const action: Action<unknown> =
nameOrAction === undefined
? { type: anonymousActionType || 'anonymous' }
: typeof nameOrAction === 'string'
? { type: nameOrAction }
: nameOrAction
if (store === undefined) {
connection?.send(action, get())
return r
}
connection?.send(
{
...action,
type: `${store}/${action.type}`,
},
{
...getTrackedConnectionState(options.name),
[store]: api.getState(),
}
)
return r
}
const setStateFromDevtools: StoreApi<S>['setState'] = (...a) => {
const originalIsRecording = isRecording
isRecording = false
set(...a)
isRecording = originalIsRecording
}
const initialState = fn(api.setState, get, api)
if (connectionInformation.type === 'untracked') {
connection?.init(initialState)
} else {
connectionInformation.stores[connectionInformation.store] = api
connection?.init(
Object.fromEntries(
Object.entries(connectionInformation.stores).map(([key, store]) => [
key,
key === connectionInformation.store
? initialState
: store.getState(),
])
)
)
}
if (
(api as any).dispatchFromDevtools &&
typeof (api as any).dispatch === 'function'
) {
let didWarnAboutReservedActionType = false
const originalDispatch = (api as any).dispatch
;(api as any).dispatch = (...a: any[]) => {
if (
__DEV__ &&
a[0].type === '__setState' &&
!didWarnAboutReservedActionType
) {
console.warn(
'[zustand devtools middleware] "__setState" action type is reserved ' +
'to set state from the devtools. Avoid using it.'
)
didWarnAboutReservedActionType = true
}
;(originalDispatch as any)(...a)
}
}
;(
connection as unknown as {
// FIXME https://github.com/reduxjs/redux-devtools/issues/1097
subscribe: (
listener: (message: Message) => void
) => (() => void) | undefined
}
).subscribe((message: any) => {
switch (message.type) {
case 'ACTION':
if (typeof message.payload !== 'string') {
console.error(
'[zustand devtools middleware] Unsupported action format'
)
return
}
return parseJsonThen<{ type: unknown; state?: PartialState }>(
message.payload,
(action) => {
if (action.type === '__setState') {
if (store === undefined) {
setStateFromDevtools(action.state as PartialState)
return
}
if (Object.keys(action.state as S).length !== 1) {
console.error(
`
[zustand devtools middleware] Unsupported __setState action format.
When using 'store' option in devtools(), the 'state' should have only one key, which is a value of 'store' that was passed in devtools(),
and value of this only key should be a state object. Example: { "type": "__setState", "state": { "abc123Store": { "foo": "bar" } } }
`
)
}
const stateFromDevtools = (action.state as S)[store]
if (
stateFromDevtools === undefined ||
stateFromDevtools === null
) {
return
}
if (
JSON.stringify(api.getState()) !==
JSON.stringify(stateFromDevtools)
) {
setStateFromDevtools(stateFromDevtools)
}
return
}
if (!(api as any).dispatchFromDevtools) return
if (typeof (api as any).dispatch !== 'function') return
;(api as any).dispatch(action)
}
)
case 'DISPATCH':
switch (message.payload.type) {
case 'RESET':
setStateFromDevtools(initialState as S)
if (store === undefined) {
return connection?.init(api.getState())
}
return connection?.init(getTrackedConnectionState(options.name))
case 'COMMIT':
if (store === undefined) {
connection?.init(api.getState())
return
}
return connection?.init(getTrackedConnectionState(options.name))
case 'ROLLBACK':
return parseJsonThen<S>(message.state, (state) => {
if (store === undefined) {
setStateFromDevtools(state)
connection?.init(api.getState())
return
}
setStateFromDevtools(state[store] as S)
connection?.init(getTrackedConnectionState(options.name))
})
case 'JUMP_TO_STATE':
case 'JUMP_TO_ACTION':
return parseJsonThen<S>(message.state, (state) => {
if (store === undefined) {
setStateFromDevtools(state)
return
}
if (
JSON.stringify(api.getState()) !==
JSON.stringify(state[store])
) {
setStateFromDevtools(state[store] as S)
}
})
case 'IMPORT_STATE': {
const { nextLiftedState } = message.payload
const lastComputedState =
nextLiftedState.computedStates.slice(-1)[0]?.state
if (!lastComputedState) return
if (store === undefined) {
setStateFromDevtools(lastComputedState)
} else {
setStateFromDevtools(lastComputedState[store])
}
connection?.send(
null as any, // FIXME no-any
nextLiftedState
)
return
}
case 'PAUSE_RECORDING':
return (isRecording = !isRecording)
}
return
}
})
return initialState
}
export const devtools = devtoolsImpl as unknown as Devtools
const parseJsonThen = <T>(stringified: string, f: (parsed: T) => void) => {
let parsed: T | undefined
try {
parsed = JSON.parse(stringified)
} catch (e) {
console.error(
'[zustand devtools middleware] Could not parse the received json',
e
)
}
if (parsed !== undefined) f(parsed as T)
}