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