-
-
Notifications
You must be signed in to change notification settings - Fork 567
/
useAtomDevtools.ts
154 lines (138 loc) 路 4.35 KB
/
useAtomDevtools.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
import { useEffect, useRef } from 'react'
import { useAtom } from 'jotai'
import type { Atom, WritableAtom } from 'jotai'
import type { Scope, SetAtom } from '../core/atom'
import { Message } from './types'
type DevtoolOptions = {
name?: string
scope?: Scope
enabled?: boolean
}
export function useAtomDevtools<Value, Result extends void | Promise<void>>(
anAtom: WritableAtom<Value, Value, Result> | Atom<Value>,
options?: DevtoolOptions
): void
/*
* @deprecated Please use object options (DevtoolsOptions)
*/
export function useAtomDevtools<Value, Result extends void | Promise<void>>(
anAtom: WritableAtom<Value, Value, Result> | Atom<Value>,
name?: string,
scope?: Scope
): void
export function useAtomDevtools<Value, Result extends void | Promise<void>>(
anAtom: WritableAtom<Value, Value, Result> | Atom<Value>,
options?: DevtoolOptions | string,
deprecatedScope?: Scope
): void {
if (typeof options === 'string' || typeof deprecatedScope !== 'undefined') {
console.warn('DEPRECATED [useAtomDevtools] use DevtoolOptions')
options = {
name: options,
scope: deprecatedScope,
} as DevtoolOptions // we intentionally lie the type a little bit here
}
const { enabled, name, 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')
}
}
const [value, setValue] = useAtom(anAtom, scope)
const lastValue = useRef(value)
const isTimeTraveling = useRef(false)
const devtools = useRef<
ReturnType<
NonNullable<typeof window['__REDUX_DEVTOOLS_EXTENSION__']>['connect']
> & {
shouldInit?: boolean
}
>()
const atomName = name || anAtom.debugLabel || anAtom.toString()
useEffect(() => {
if (!extension) {
return
}
const setValueIfWritable = (value: Value) => {
if (typeof setValue === 'function') {
;(setValue as SetAtom<Value, void>)(value)
return
}
console.warn(
'[Warn] you cannot do write operations (Time-travelling, etc) in read-only atoms\n',
anAtom
)
}
devtools.current = extension.connect({ name: atomName })
const unsubscribe = (
devtools.current as unknown as {
// FIXME https://github.com/reduxjs/redux-devtools/issues/1097
subscribe: (
listener: (message: Message) => void
) => (() => void) | undefined
}
).subscribe((message) => {
if (message.type === 'ACTION' && message.payload) {
try {
setValueIfWritable(JSON.parse(message.payload))
} catch (e) {
console.error(
'please dispatch a serializable value that JSON.parse() support\n',
e
)
}
} else if (message.type === 'DISPATCH' && message.state) {
if (
message.payload?.type === 'JUMP_TO_ACTION' ||
message.payload?.type === 'JUMP_TO_STATE'
) {
isTimeTraveling.current = true
setValueIfWritable(JSON.parse(message.state))
}
} else if (
message.type === 'DISPATCH' &&
message.payload?.type === 'COMMIT'
) {
devtools.current?.init(lastValue.current)
} else if (
message.type === 'DISPATCH' &&
message.payload?.type === 'IMPORT_STATE'
) {
const computedStates =
message.payload.nextLiftedState?.computedStates || []
computedStates.forEach(({ state }: { state: Value }, index: number) => {
if (index === 0) {
devtools.current?.init(state)
} else {
setValueIfWritable(state)
}
})
}
})
devtools.current.shouldInit = true
return unsubscribe
}, [anAtom, extension, atomName, setValue])
useEffect(() => {
if (!devtools.current) {
return
}
lastValue.current = value
if (devtools.current.shouldInit) {
devtools.current.init(value)
devtools.current.shouldInit = false
} else if (isTimeTraveling.current) {
isTimeTraveling.current = false
} else {
devtools.current.send(
`${atomName} - ${new Date().toLocaleString()}` as any,
value
)
}
}, [anAtom, extension, atomName, value])
}