1 | System.register(['react', 'jotai/react'], (function (exports) {
|
2 | 'use strict';
|
3 | var useState, useEffect, useDebugValue, useRef, useCallback, useStore, useAtom;
|
4 | return {
|
5 | setters: [function (module) {
|
6 | useState = module.useState;
|
7 | useEffect = module.useEffect;
|
8 | useDebugValue = module.useDebugValue;
|
9 | useRef = module.useRef;
|
10 | useCallback = module.useCallback;
|
11 | }, function (module) {
|
12 | useStore = module.useStore;
|
13 | useAtom = module.useAtom;
|
14 | }],
|
15 | execute: (function () {
|
16 |
|
17 | exports({
|
18 | useAtomDevtools: useAtomDevtools,
|
19 | useAtomsDevtools: useAtomsDevtools,
|
20 | useAtomsSnapshot: useAtomsSnapshot,
|
21 | useGotoAtomsSnapshot: useGotoAtomsSnapshot
|
22 | });
|
23 |
|
24 | const atomToPrintable$1 = (atom) => atom.debugLabel || atom.toString();
|
25 | const stateToPrintable = ([store, atoms]) => Object.fromEntries(
|
26 | atoms.flatMap((atom) => {
|
27 | var _a, _b;
|
28 | const mounted = (_a = store.dev_get_mounted) == null ? void 0 : _a.call(store, atom);
|
29 | if (!mounted) {
|
30 | return [];
|
31 | }
|
32 | const dependents = mounted.t;
|
33 | const atomState = ((_b = store.dev_get_atom_state) == null ? void 0 : _b.call(store, atom)) || {};
|
34 | return [
|
35 | [
|
36 | atomToPrintable$1(atom),
|
37 | {
|
38 | ..."e" in atomState && { error: atomState.e },
|
39 | ..."v" in atomState && { value: atomState.v },
|
40 | dependents: Array.from(dependents).map(atomToPrintable$1)
|
41 | }
|
42 | ]
|
43 | ];
|
44 | })
|
45 | );
|
46 | const useAtomsDebugValue = exports('useAtomsDebugValue', (options) => {
|
47 | var _a;
|
48 | const enabled = (_a = options == null ? void 0 : options.enabled) != null ? _a : true;
|
49 | const store = useStore(options);
|
50 | const [atoms, setAtoms] = useState([]);
|
51 | useEffect(() => {
|
52 | var _a2;
|
53 | if (!enabled) {
|
54 | return;
|
55 | }
|
56 | const callback = () => {
|
57 | var _a3;
|
58 | setAtoms(Array.from(((_a3 = store.dev_get_mounted_atoms) == null ? void 0 : _a3.call(store)) || []));
|
59 | };
|
60 | const unsubscribe = (_a2 = store.dev_subscribe_state) == null ? void 0 : _a2.call(store, callback);
|
61 | callback();
|
62 | return unsubscribe;
|
63 | }, [enabled, store]);
|
64 | useDebugValue([store, atoms], stateToPrintable);
|
65 | });
|
66 |
|
67 | function useAtomDevtools(anAtom, options) {
|
68 | const { enabled, name } = options || {};
|
69 | let extension;
|
70 | try {
|
71 | extension = (enabled != null ? enabled : true) && window.__REDUX_DEVTOOLS_EXTENSION__;
|
72 | } catch {
|
73 | }
|
74 | if (!extension) {
|
75 | if (enabled) {
|
76 | console.warn("Please install/enable Redux devtools extension");
|
77 | }
|
78 | }
|
79 | const [value, setValue] = useAtom(anAtom);
|
80 | const lastValue = useRef(value);
|
81 | const isTimeTraveling = useRef(false);
|
82 | const devtools = useRef();
|
83 | const atomName = name || anAtom.debugLabel || anAtom.toString();
|
84 | useEffect(() => {
|
85 | if (!extension) {
|
86 | return;
|
87 | }
|
88 | const setValueIfWritable = (value2) => {
|
89 | if (typeof setValue === "function") {
|
90 | setValue(value2);
|
91 | return;
|
92 | }
|
93 | console.warn(
|
94 | "[Warn] you cannot do write operations (Time-travelling, etc) in read-only atoms\n",
|
95 | anAtom
|
96 | );
|
97 | };
|
98 | devtools.current = extension.connect({ name: atomName });
|
99 | const unsubscribe = devtools.current.subscribe((message) => {
|
100 | var _a, _b, _c, _d, _e, _f;
|
101 | if (message.type === "ACTION" && message.payload) {
|
102 | try {
|
103 | setValueIfWritable(JSON.parse(message.payload));
|
104 | } catch (e) {
|
105 | console.error(
|
106 | "please dispatch a serializable value that JSON.parse() support\n",
|
107 | e
|
108 | );
|
109 | }
|
110 | } else if (message.type === "DISPATCH" && message.state) {
|
111 | if (((_a = message.payload) == null ? void 0 : _a.type) === "JUMP_TO_ACTION" || ((_b = message.payload) == null ? void 0 : _b.type) === "JUMP_TO_STATE") {
|
112 | isTimeTraveling.current = true;
|
113 | setValueIfWritable(JSON.parse(message.state));
|
114 | }
|
115 | } else if (message.type === "DISPATCH" && ((_c = message.payload) == null ? void 0 : _c.type) === "COMMIT") {
|
116 | (_d = devtools.current) == null ? void 0 : _d.init(lastValue.current);
|
117 | } else if (message.type === "DISPATCH" && ((_e = message.payload) == null ? void 0 : _e.type) === "IMPORT_STATE") {
|
118 | const computedStates = ((_f = message.payload.nextLiftedState) == null ? void 0 : _f.computedStates) || [];
|
119 | computedStates.forEach(({ state }, index) => {
|
120 | var _a2;
|
121 | if (index === 0) {
|
122 | (_a2 = devtools.current) == null ? void 0 : _a2.init(state);
|
123 | } else {
|
124 | setValueIfWritable(state);
|
125 | }
|
126 | });
|
127 | }
|
128 | });
|
129 | devtools.current.shouldInit = true;
|
130 | return unsubscribe;
|
131 | }, [anAtom, extension, atomName, setValue]);
|
132 | useEffect(() => {
|
133 | if (!devtools.current) {
|
134 | return;
|
135 | }
|
136 | lastValue.current = value;
|
137 | if (devtools.current.shouldInit) {
|
138 | devtools.current.init(value);
|
139 | devtools.current.shouldInit = false;
|
140 | } else if (isTimeTraveling.current) {
|
141 | isTimeTraveling.current = false;
|
142 | } else {
|
143 | devtools.current.send(
|
144 | `${atomName} - ${new Date().toLocaleString()}`,
|
145 | value
|
146 | );
|
147 | }
|
148 | }, [anAtom, extension, atomName, value]);
|
149 | }
|
150 |
|
151 | const isEqualAtomsValues = (left, right) => left.size === right.size && Array.from(left).every(([left2, v]) => Object.is(right.get(left2), v));
|
152 | const isEqualAtomsDependents = (left, right) => left.size === right.size && Array.from(left).every(([a, dLeft]) => {
|
153 | const dRight = right.get(a);
|
154 | return dRight && dLeft.size === dRight.size && Array.from(dLeft).every((d) => dRight.has(d));
|
155 | });
|
156 | function useAtomsSnapshot(options) {
|
157 | const store = useStore(options);
|
158 | const [atomsSnapshot, setAtomsSnapshot] = useState(() => ({
|
159 | values: new Map(),
|
160 | dependents: new Map()
|
161 | }));
|
162 | useEffect(() => {
|
163 | if (!store.dev_subscribe_state)
|
164 | return;
|
165 | let prevValues = new Map();
|
166 | let prevDependents = new Map();
|
167 | const callback = () => {
|
168 | const values = new Map();
|
169 | const dependents = new Map();
|
170 | for (const atom of store.dev_get_mounted_atoms() || []) {
|
171 | const atomState = store.dev_get_atom_state(atom);
|
172 | if (atomState) {
|
173 | if ("v" in atomState) {
|
174 | values.set(atom, atomState.v);
|
175 | }
|
176 | }
|
177 | const mounted = store.dev_get_mounted(atom);
|
178 | if (mounted) {
|
179 | dependents.set(atom, mounted.t);
|
180 | }
|
181 | }
|
182 | if (isEqualAtomsValues(prevValues, values) && isEqualAtomsDependents(prevDependents, dependents)) {
|
183 | return;
|
184 | }
|
185 | prevValues = values;
|
186 | prevDependents = dependents;
|
187 | setAtomsSnapshot({ values, dependents });
|
188 | };
|
189 | const unsubscribe = store.dev_subscribe_state(callback);
|
190 | callback();
|
191 | return unsubscribe;
|
192 | }, [store]);
|
193 | return atomsSnapshot;
|
194 | }
|
195 |
|
196 | function useGotoAtomsSnapshot(options) {
|
197 | const store = useStore(options);
|
198 | return useCallback(
|
199 | (snapshot) => {
|
200 | if (store.dev_subscribe_state) {
|
201 | store.res(snapshot.values);
|
202 | }
|
203 | },
|
204 | [store]
|
205 | );
|
206 | }
|
207 |
|
208 | const atomToPrintable = (atom) => atom.debugLabel ? `${atom}:${atom.debugLabel}` : `${atom}`;
|
209 | const getDevtoolsState = (atomsSnapshot) => {
|
210 | const values = {};
|
211 | atomsSnapshot.values.forEach((v, atom) => {
|
212 | values[atomToPrintable(atom)] = v;
|
213 | });
|
214 | const dependents = {};
|
215 | atomsSnapshot.dependents.forEach((d, atom) => {
|
216 | dependents[atomToPrintable(atom)] = Array.from(d).map(atomToPrintable);
|
217 | });
|
218 | return {
|
219 | values,
|
220 | dependents
|
221 | };
|
222 | };
|
223 | function useAtomsDevtools(name, options) {
|
224 | const { enabled } = options || {};
|
225 | let extension;
|
226 | try {
|
227 | extension = (enabled != null ? enabled : true) && window.__REDUX_DEVTOOLS_EXTENSION__;
|
228 | } catch {
|
229 | }
|
230 | if (!extension) {
|
231 | if (enabled) {
|
232 | console.warn("Please install/enable Redux devtools extension");
|
233 | }
|
234 | }
|
235 | const atomsSnapshot = useAtomsSnapshot();
|
236 | const goToSnapshot = useGotoAtomsSnapshot();
|
237 | const isTimeTraveling = useRef(false);
|
238 | const isRecording = useRef(true);
|
239 | const devtools = useRef();
|
240 | const snapshots = useRef([]);
|
241 | useEffect(() => {
|
242 | if (!extension) {
|
243 | return;
|
244 | }
|
245 | const getSnapshotAt = (index = snapshots.current.length - 1) => {
|
246 | const snapshot = snapshots.current[index >= 0 ? index : 0];
|
247 | if (!snapshot) {
|
248 | throw new Error("snaphost index out of bounds");
|
249 | }
|
250 | return snapshot;
|
251 | };
|
252 | const connection = extension.connect({ name });
|
253 | const devtoolsUnsubscribe = connection.subscribe((message) => {
|
254 | var _a;
|
255 | switch (message.type) {
|
256 | case "DISPATCH":
|
257 | switch ((_a = message.payload) == null ? void 0 : _a.type) {
|
258 | case "RESET":
|
259 | break;
|
260 | case "COMMIT":
|
261 | connection.init(getDevtoolsState(getSnapshotAt()));
|
262 | snapshots.current = [];
|
263 | break;
|
264 | case "JUMP_TO_ACTION":
|
265 | case "JUMP_TO_STATE":
|
266 | isTimeTraveling.current = true;
|
267 | goToSnapshot(getSnapshotAt(message.payload.actionId - 1));
|
268 | break;
|
269 | case "PAUSE_RECORDING":
|
270 | isRecording.current = !isRecording.current;
|
271 | break;
|
272 | }
|
273 | }
|
274 | });
|
275 | devtools.current = connection;
|
276 | devtools.current.shouldInit = true;
|
277 | return () => {
|
278 | extension.disconnect();
|
279 | devtoolsUnsubscribe == null ? void 0 : devtoolsUnsubscribe();
|
280 | };
|
281 | }, [extension, goToSnapshot, name]);
|
282 | useEffect(() => {
|
283 | if (!devtools.current) {
|
284 | return;
|
285 | }
|
286 | if (devtools.current.shouldInit) {
|
287 | devtools.current.init(void 0);
|
288 | devtools.current.shouldInit = false;
|
289 | return;
|
290 | }
|
291 | if (isTimeTraveling.current) {
|
292 | isTimeTraveling.current = false;
|
293 | } else if (isRecording.current) {
|
294 | snapshots.current.push(atomsSnapshot);
|
295 | devtools.current.send(
|
296 | {
|
297 | type: `${snapshots.current.length}`,
|
298 | updatedAt: new Date().toLocaleString()
|
299 | },
|
300 | getDevtoolsState(atomsSnapshot)
|
301 | );
|
302 | }
|
303 | }, [atomsSnapshot]);
|
304 | }
|
305 |
|
306 | })
|
307 | };
|
308 | }));
|