-
Notifications
You must be signed in to change notification settings - Fork 33
/
compose-with-devtools.ts
257 lines (211 loc) · 6.86 KB
/
compose-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
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
import { Atom } from 'jotai';
import {
AnyAtom,
AnyAtomError,
AnyAtomValue,
Store,
StoreV1,
StoreV2,
} from '../../types';
type DevSubscribeStoreListener = (action: {
type: 'get' | 'async-get' | 'set' | 'sub' | 'unsub' | 'restore';
}) => void;
type DevToolsStoreMethods = {
subscribeStore: (l: DevSubscribeStoreListener) => () => void;
getMountedAtoms: () => Iterable<Atom<unknown>>;
getAtomState: (
atom: Atom<unknown>,
) =>
| { readonly v: AnyAtomValue; readonly d: Iterable<AnyAtom> }
| { readonly e: AnyAtomError; readonly d: Iterable<AnyAtom> }
| undefined;
getMountedAtomState: (atom: Atom<unknown>) =>
| {
// Atom's listeners
readonly l: Set<() => void>;
// Atom's dependents. Atoms that depend on this atom
readonly t: Set<Atom<unknown>>;
}
| undefined;
restoreAtoms: (values: Iterable<readonly [AnyAtom, AnyAtomValue]>) => void;
};
type WithDevToolsStore<S extends Store> = S & DevToolsStoreMethods;
const isStoreV2 = (store: Store | undefined): store is StoreV2 => {
return store ? 'dev4_get_internal_weak_map' in store : false;
};
const isStoreV1 = (store: Store | undefined): store is StoreV1 => {
return !isStoreV2(store);
};
export const isDevToolsStore = (
store: Store | WithDevToolsStore<Store>,
): store is WithDevToolsStore<Store> => {
return 'subscribeStore' in store;
};
const __composeV2StoreWithDevTools = (
store: StoreV2,
): WithDevToolsStore<StoreV2> => {
const { sub, set, get } = store;
const storeListeners: Set<DevSubscribeStoreListener> = new Set();
// Map to keep track of how many times an atom was set recently
// We mostly use this to re-collect the values for history tracking for async atoms
// Async atoms call `get` once they finish fetching the value, so we verify if the atom was set recently and then trigger the history tracking
// I hope there is a better way to do this
const recentlySetAtomsMap = new WeakMap<Atom<unknown>, number>();
const reduceCountOrRemoveRecentlySetAtom = (
atom: Atom<unknown>,
onFound?: () => void,
) => {
const foundCount = recentlySetAtomsMap.get(atom);
if (typeof foundCount === 'number') {
if (foundCount > 1) {
recentlySetAtomsMap.set(atom, foundCount - 1);
} else {
recentlySetAtomsMap.delete(atom);
}
onFound?.();
}
};
const increaseCountRecentlySetAtom = (atom: Atom<unknown>) => {
const foundCount = recentlySetAtomsMap.get(atom);
recentlySetAtomsMap.set(atom, (foundCount || 0) + 1);
};
store.sub = (...args) => {
const unsub = sub(...args);
storeListeners.forEach((l) => l({ type: 'sub' }));
return () => {
unsub();
// We remove the atom from the recently set map if it was set recently when it is unsubscribed
reduceCountOrRemoveRecentlySetAtom(args[0]);
storeListeners.forEach((l) => l({ type: 'unsub' }));
};
};
store.get = (...args) => {
const value = get(...args);
reduceCountOrRemoveRecentlySetAtom(args[0], () => {
if (value instanceof Promise) {
value.then(() => {
// We wait for a tick to ensure that if there are any derived atoms then we wait for them to be flushed out as well
Promise.resolve().then(() => {
storeListeners.forEach((l) => l({ type: 'async-get' }));
});
});
}
});
storeListeners.forEach((l) => l({ type: 'get' }));
return value;
};
store.set = (...args) => {
const value = set(...args);
increaseCountRecentlySetAtom(args[0]);
storeListeners.forEach((l) => l({ type: 'set' }));
return value;
};
(store as WithDevToolsStore<typeof store>).subscribeStore = (l) => {
storeListeners.add(l);
return () => {
storeListeners.delete(l);
};
};
(store as WithDevToolsStore<typeof store>).getMountedAtoms = () => {
return store.dev4_get_mounted_atoms();
};
(store as WithDevToolsStore<typeof store>).getAtomState = (atom) => {
const aState = store.dev4_get_internal_weak_map().get(atom);
if (aState) {
return { v: aState.v, e: aState.e, d: new Set(aState.d.keys()) };
}
return undefined;
};
(store as WithDevToolsStore<typeof store>).getMountedAtomState = (atom) => {
const aState = store.dev4_get_internal_weak_map().get(atom);
if (aState && aState.m) {
return {
l: aState.m.l,
t: aState.m.t,
};
}
return undefined;
};
(store as WithDevToolsStore<typeof store>).restoreAtoms = (values) => {
store.dev4_restore_atoms(values);
storeListeners.forEach((l) => l({ type: 'restore' }));
};
return store as typeof store & DevToolsStoreMethods;
};
const __composeV1StoreWithDevTools = (
store: StoreV1,
): StoreV1 | WithDevToolsStore<StoreV1> => {
if (
'dev_subscribe_store' in store &&
'dev_get_mounted_atoms' in store &&
'dev_get_atom_state' in store &&
'dev_get_mounted' in store &&
'dev_restore_atoms' in store
) {
const {
dev_subscribe_store,
dev_get_mounted_atoms,
dev_get_atom_state,
dev_get_mounted,
dev_restore_atoms,
} = store;
(store as WithDevToolsStore<typeof store>).subscribeStore = (l) => {
const cb: Parameters<typeof dev_subscribe_store>[0] = (action) => {
if (action.type === 'write' || action.type === 'async-write') {
l({ type: 'set' });
}
if (action.type === 'sub') {
l({ type: 'sub' });
}
if (action.type === 'unsub') {
l({ type: 'unsub' });
}
if (action.type === 'restore') {
l({ type: 'restore' });
}
};
return dev_subscribe_store(cb, 2);
};
(store as WithDevToolsStore<typeof store>).getMountedAtoms = () => {
return dev_get_mounted_atoms();
};
(store as WithDevToolsStore<typeof store>).getAtomState = (atom) => {
const aState = dev_get_atom_state(atom);
if (aState) {
const d = new Set(aState.d.keys());
d.delete(atom);
if ('v' in aState) {
return { v: aState.v, d };
}
if ('e' in aState) {
return { e: aState.e, d };
}
return undefined;
}
};
(store as WithDevToolsStore<typeof store>).getMountedAtomState = (atom) => {
const mounted = dev_get_mounted(atom);
return mounted;
};
(store as WithDevToolsStore<typeof store>).restoreAtoms = (values) => {
dev_restore_atoms(values);
};
return store as typeof store & DevToolsStoreMethods;
}
return store;
};
export const composeWithDevTools = (
store: Store,
): typeof store | WithDevToolsStore<typeof store> => {
// Check if the store is already composed with DevTools
if (isDevToolsStore(store)) {
return store;
}
if (isStoreV2(store)) {
return __composeV2StoreWithDevTools(store);
}
if (isStoreV1(store)) {
return __composeV1StoreWithDevTools(store);
}
return store;
};