deno.land / x / jotai@v1.8.4 / src / devtools / useAtomsDevtools.ts

useAtomsDevtools.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
import { useEffect, useRef } from 'react'import type { Atom } from '../core/atom'import { Message } from './types'import { useAtomsSnapshot } from './useAtomsSnapshot'import { useGotoAtomsSnapshot } from './useGotoAtomsSnapshot'
type Scope = NonNullable<Parameters<typeof useAtomsSnapshot>[0]>
type AnyAtomValue = unknowntype AnyAtom = Atom<AnyAtomValue>type AtomsValues = Map<AnyAtom, AnyAtomValue> // immutabletype AtomsDependents = Map<AnyAtom, Set<AnyAtom>> // immutabletype AtomsSnapshot = Readonly<{ values: AtomsValues dependents: AtomsDependents}>
const atomToPrintable = (atom: AnyAtom) => atom.debugLabel ? `${atom}:${atom.debugLabel}` : `${atom}`
const getDevtoolsState = (atomsSnapshot: AtomsSnapshot) => { const values: Record<string, AnyAtomValue> = {} atomsSnapshot.values.forEach((v, atom) => { values[atomToPrintable(atom)] = v }) const dependents: Record<string, string[]> = {} atomsSnapshot.dependents.forEach((d, atom) => { dependents[atomToPrintable(atom)] = Array.from(d).map(atomToPrintable) }) return { values, dependents, }}
type DevtoolsOptions = { scope?: Scope enabled?: boolean}
export function useAtomsDevtools(name: string, options?: DevtoolsOptions): void
/* * @deprecated Please use object options (DevtoolsOptions) */export function useAtomsDevtools(name: string, scope?: Scope): void
export function useAtomsDevtools( name: string, options?: DevtoolsOptions | Scope) { if (typeof options !== 'undefined' && typeof options !== 'object') { console.warn('DEPRECATED [useAtomsDevtools] use DevtoolsOptions') options = { scope: options } } const { enabled, scope } = options || {}
let extension: typeof window['__REDUX_DEVTOOLS_EXTENSION__'] | false
try { extension = (enabled ?? __DEV__) && window.__REDUX_DEVTOOLS_EXTENSION__ } catch { // ignored }
if (!extension) { if (__DEV__ && enabled) { console.warn('Please install/enable Redux devtools extension') } }
// This an exception, we don't usually use utils in themselves! const atomsSnapshot = useAtomsSnapshot(scope) const goToSnapshot = useGotoAtomsSnapshot(scope)
const isTimeTraveling = useRef(false) const isRecording = useRef(true) const devtools = useRef< ReturnType< NonNullable<typeof window['__REDUX_DEVTOOLS_EXTENSION__']>['connect'] > & { shouldInit?: boolean } >()
const snapshots = useRef<AtomsSnapshot[]>([])
useEffect(() => { if (!extension) { return } const getSnapshotAt = (index = snapshots.current.length - 1) => { // index 0 is @@INIT, so we need to return the next action (0) const snapshot = snapshots.current[index >= 0 ? index : 0] if (!snapshot) { throw new Error('snaphost index out of bounds') } return snapshot } const connection = extension.connect({ name })
const devtoolsUnsubscribe = ( connection as unknown as { // FIXME https://github.com/reduxjs/redux-devtools/issues/1097 subscribe: ( listener: (message: Message) => void ) => (() => void) | undefined } ).subscribe((message) => { switch (message.type) { case 'DISPATCH': switch (message.payload?.type) { case 'RESET': // TODO break
case 'COMMIT': connection.init(getDevtoolsState(getSnapshotAt())) snapshots.current = [] break
case 'JUMP_TO_ACTION': case 'JUMP_TO_STATE': isTimeTraveling.current = true goToSnapshot(getSnapshotAt(message.payload.actionId - 1)) break
case 'PAUSE_RECORDING': isRecording.current = !isRecording.current break } } })
devtools.current = connection devtools.current.shouldInit = true return devtoolsUnsubscribe }, [extension, goToSnapshot, name])
useEffect(() => { if (!devtools.current) { return } if (devtools.current.shouldInit) { devtools.current.init(undefined) devtools.current.shouldInit = false return } if (isTimeTraveling.current) { isTimeTraveling.current = false } else if (isRecording.current) { snapshots.current.push(atomsSnapshot) devtools.current.send( { type: `${snapshots.current.length}`, updatedAt: new Date().toLocaleString(), } as any, getDevtoolsState(atomsSnapshot) ) } }, [atomsSnapshot])}
jotai

Version Info

Tagged at
2 years ago