1 | System.register(['jotai', 'react'], (function (exports) {
|
2 | 'use strict';
|
3 | var atom, SECRET_INTERNAL_getScopeContext, useAtom, useSetAtom, useContext, useCallback, useMemo;
|
4 | return {
|
5 | setters: [function (module) {
|
6 | atom = module.atom;
|
7 | SECRET_INTERNAL_getScopeContext = module.SECRET_INTERNAL_getScopeContext;
|
8 | useAtom = module.useAtom;
|
9 | useSetAtom = module.useSetAtom;
|
10 | exports({ useAtomValue: module.useAtomValue, useUpdateAtom: module.useSetAtom });
|
11 | }, function (module) {
|
12 | useContext = module.useContext;
|
13 | useCallback = module.useCallback;
|
14 | useMemo = module.useMemo;
|
15 | }],
|
16 | execute: (function () {
|
17 |
|
18 | exports({
|
19 | atomFamily: atomFamily,
|
20 | atomWithDefault: atomWithDefault,
|
21 | atomWithHash: atomWithHash,
|
22 | atomWithObservable: atomWithObservable,
|
23 | atomWithReducer: atomWithReducer,
|
24 | atomWithReset: atomWithReset,
|
25 | atomWithStorage: atomWithStorage,
|
26 | createJSONStorage: createJSONStorage,
|
27 | freezeAtom: freezeAtom,
|
28 | freezeAtomCreator: freezeAtomCreator,
|
29 | loadable: loadable,
|
30 | selectAtom: selectAtom,
|
31 | splitAtom: splitAtom,
|
32 | useAtomCallback: useAtomCallback,
|
33 | useHydrateAtoms: useHydrateAtoms,
|
34 | useReducerAtom: useReducerAtom,
|
35 | useResetAtom: useResetAtom,
|
36 | waitForAll: waitForAll
|
37 | });
|
38 |
|
39 | const RESET = exports('RESET', Symbol());
|
40 |
|
41 | function atomWithReset(initialValue) {
|
42 | const anAtom = atom(initialValue, (get, set, update) => {
|
43 | if (update === RESET) {
|
44 | set(anAtom, initialValue);
|
45 | } else {
|
46 | set(anAtom, typeof update === "function" ? update(get(anAtom)) : update);
|
47 | }
|
48 | });
|
49 | return anAtom;
|
50 | }
|
51 |
|
52 | const WRITE_ATOM = "w";
|
53 | const RESTORE_ATOMS = "h";
|
54 |
|
55 | function useResetAtom(anAtom, scope) {
|
56 | const ScopeContext = SECRET_INTERNAL_getScopeContext(scope);
|
57 | const store = useContext(ScopeContext).s;
|
58 | const setAtom = useCallback(() => store[WRITE_ATOM](anAtom, RESET), [store, anAtom]);
|
59 | return setAtom;
|
60 | }
|
61 |
|
62 | function useReducerAtom(anAtom, reducer, scope) {
|
63 | const [state, setState] = useAtom(anAtom, scope);
|
64 | const dispatch = useCallback((action) => {
|
65 | setState((prev) => reducer(prev, action));
|
66 | }, [setState, reducer]);
|
67 | return [state, dispatch];
|
68 | }
|
69 |
|
70 | function atomWithReducer(initialValue, reducer) {
|
71 | const anAtom = atom(initialValue, (get, set, action) => set(anAtom, reducer(get(anAtom), action)));
|
72 | return anAtom;
|
73 | }
|
74 |
|
75 | function atomFamily(initializeAtom, areEqual) {
|
76 | let shouldRemove = null;
|
77 | const atoms = new Map();
|
78 | const createAtom = (param) => {
|
79 | let item;
|
80 | if (areEqual === void 0) {
|
81 | item = atoms.get(param);
|
82 | } else {
|
83 | for (const [key, value] of atoms) {
|
84 | if (areEqual(key, param)) {
|
85 | item = value;
|
86 | break;
|
87 | }
|
88 | }
|
89 | }
|
90 | if (item !== void 0) {
|
91 | if (shouldRemove == null ? void 0 : shouldRemove(item[1], param)) {
|
92 | atoms.delete(param);
|
93 | } else {
|
94 | return item[0];
|
95 | }
|
96 | }
|
97 | const newAtom = initializeAtom(param);
|
98 | atoms.set(param, [newAtom, Date.now()]);
|
99 | return newAtom;
|
100 | };
|
101 | createAtom.remove = (param) => {
|
102 | if (areEqual === void 0) {
|
103 | atoms.delete(param);
|
104 | } else {
|
105 | for (const [key] of atoms) {
|
106 | if (areEqual(key, param)) {
|
107 | atoms.delete(key);
|
108 | break;
|
109 | }
|
110 | }
|
111 | }
|
112 | };
|
113 | createAtom.setShouldRemove = (fn) => {
|
114 | shouldRemove = fn;
|
115 | if (!shouldRemove)
|
116 | return;
|
117 | for (const [key, value] of atoms) {
|
118 | if (shouldRemove(value[1], key)) {
|
119 | atoms.delete(key);
|
120 | }
|
121 | }
|
122 | };
|
123 | return createAtom;
|
124 | }
|
125 |
|
126 | const getWeakCacheItem = (cache, deps) => {
|
127 | do {
|
128 | const [dep, ...rest] = deps;
|
129 | const entry = cache.get(dep);
|
130 | if (!entry) {
|
131 | return;
|
132 | }
|
133 | if (!rest.length) {
|
134 | return entry[1];
|
135 | }
|
136 | cache = entry[0];
|
137 | deps = rest;
|
138 | } while (deps.length);
|
139 | };
|
140 | const setWeakCacheItem = (cache, deps, item) => {
|
141 | do {
|
142 | const [dep, ...rest] = deps;
|
143 | let entry = cache.get(dep);
|
144 | if (!entry) {
|
145 | entry = [ new WeakMap()];
|
146 | cache.set(dep, entry);
|
147 | }
|
148 | if (!rest.length) {
|
149 | entry[1] = item;
|
150 | return;
|
151 | }
|
152 | cache = entry[0];
|
153 | deps = rest;
|
154 | } while (deps.length);
|
155 | };
|
156 | const createMemoizeAtom = () => {
|
157 | const cache = new WeakMap();
|
158 | const memoizeAtom = (createAtom, deps) => {
|
159 | const cachedAtom = getWeakCacheItem(cache, deps);
|
160 | if (cachedAtom) {
|
161 | return cachedAtom;
|
162 | }
|
163 | const createdAtom = createAtom();
|
164 | setWeakCacheItem(cache, deps, createdAtom);
|
165 | return createdAtom;
|
166 | };
|
167 | return memoizeAtom;
|
168 | };
|
169 |
|
170 | const memoizeAtom$4 = createMemoizeAtom();
|
171 | function selectAtom(anAtom, selector, equalityFn = Object.is) {
|
172 | return memoizeAtom$4(() => {
|
173 | const refAtom = atom(() => ({}));
|
174 | const derivedAtom = atom((get) => {
|
175 | const slice = selector(get(anAtom));
|
176 | const ref = get(refAtom);
|
177 | if ("prev" in ref && equalityFn(ref.prev, slice)) {
|
178 | return ref.prev;
|
179 | }
|
180 | ref.prev = slice;
|
181 | return slice;
|
182 | });
|
183 | return derivedAtom;
|
184 | }, [anAtom, selector, equalityFn]);
|
185 | }
|
186 |
|
187 | function useAtomCallback(callback, scope) {
|
188 | const anAtom = useMemo(() => atom(null, (get, set, [arg, resolve, reject]) => {
|
189 | try {
|
190 | resolve(callback(get, set, arg));
|
191 | } catch (e) {
|
192 | reject(e);
|
193 | }
|
194 | }), [callback]);
|
195 | const invoke = useSetAtom(anAtom, scope);
|
196 | return useCallback((arg) => {
|
197 | let isSync = true;
|
198 | let settled = {};
|
199 | const promise = new Promise((resolve, reject) => {
|
200 | invoke([
|
201 | arg,
|
202 | (v) => {
|
203 | if (isSync) {
|
204 | settled = { v };
|
205 | } else {
|
206 | resolve(v);
|
207 | }
|
208 | },
|
209 | (e) => {
|
210 | if (isSync) {
|
211 | settled = { e };
|
212 | } else {
|
213 | reject(e);
|
214 | }
|
215 | }
|
216 | ]);
|
217 | });
|
218 | isSync = false;
|
219 | if ("e" in settled) {
|
220 | throw settled.e;
|
221 | }
|
222 | if ("v" in settled) {
|
223 | return settled.v;
|
224 | }
|
225 | return promise;
|
226 | }, [invoke]);
|
227 | }
|
228 |
|
229 | const memoizeAtom$3 = createMemoizeAtom();
|
230 | const deepFreeze = (obj) => {
|
231 | if (typeof obj !== "object" || obj === null)
|
232 | return;
|
233 | Object.freeze(obj);
|
234 | const propNames = Object.getOwnPropertyNames(obj);
|
235 | for (const name of propNames) {
|
236 | const value = obj[name];
|
237 | deepFreeze(value);
|
238 | }
|
239 | return obj;
|
240 | };
|
241 | function freezeAtom(anAtom) {
|
242 | return memoizeAtom$3(() => {
|
243 | const frozenAtom = atom((get) => deepFreeze(get(anAtom)), (_get, set, arg) => set(anAtom, arg));
|
244 | return frozenAtom;
|
245 | }, [anAtom]);
|
246 | }
|
247 | function freezeAtomCreator(createAtom) {
|
248 | return (...params) => {
|
249 | const anAtom = createAtom(...params);
|
250 | const origRead = anAtom.read;
|
251 | anAtom.read = (get) => deepFreeze(origRead(get));
|
252 | return anAtom;
|
253 | };
|
254 | }
|
255 |
|
256 | const memoizeAtom$2 = createMemoizeAtom();
|
257 | const isWritable = (atom2) => !!atom2.write;
|
258 | const isFunction = (x) => typeof x === "function";
|
259 | function splitAtom(arrAtom, keyExtractor) {
|
260 | return memoizeAtom$2(() => {
|
261 | const mappingCache = new WeakMap();
|
262 | const getMapping = (arr, prev) => {
|
263 | let mapping = mappingCache.get(arr);
|
264 | if (mapping) {
|
265 | return mapping;
|
266 | }
|
267 | const prevMapping = prev && mappingCache.get(prev);
|
268 | const atomList = [];
|
269 | const keyList = [];
|
270 | arr.forEach((item, index) => {
|
271 | const key = keyExtractor ? keyExtractor(item) : index;
|
272 | keyList[index] = key;
|
273 | const cachedAtom = prevMapping && prevMapping.atomList[prevMapping.keyList.indexOf(key)];
|
274 | if (cachedAtom) {
|
275 | atomList[index] = cachedAtom;
|
276 | return;
|
277 | }
|
278 | const read2 = (get) => {
|
279 | const ref = get(refAtom);
|
280 | const currArr = get(arrAtom);
|
281 | const mapping2 = getMapping(currArr, ref.prev);
|
282 | const index2 = mapping2.keyList.indexOf(key);
|
283 | if (index2 < 0 || index2 >= currArr.length) {
|
284 | const prevItem = arr[getMapping(arr).keyList.indexOf(key)];
|
285 | if (prevItem) {
|
286 | return prevItem;
|
287 | }
|
288 | throw new Error("splitAtom: index out of bounds for read");
|
289 | }
|
290 | return currArr[index2];
|
291 | };
|
292 | const write2 = (get, set, update) => {
|
293 | const ref = get(refAtom);
|
294 | const arr2 = get(arrAtom);
|
295 | const mapping2 = getMapping(arr2, ref.prev);
|
296 | const index2 = mapping2.keyList.indexOf(key);
|
297 | if (index2 < 0 || index2 >= arr2.length) {
|
298 | throw new Error("splitAtom: index out of bounds for write");
|
299 | }
|
300 | const nextItem = isFunction(update) ? update(arr2[index2]) : update;
|
301 | set(arrAtom, [
|
302 | ...arr2.slice(0, index2),
|
303 | nextItem,
|
304 | ...arr2.slice(index2 + 1)
|
305 | ]);
|
306 | };
|
307 | atomList[index] = isWritable(arrAtom) ? atom(read2, write2) : atom(read2);
|
308 | });
|
309 | if (prevMapping && prevMapping.keyList.length === keyList.length && prevMapping.keyList.every((x, i) => x === keyList[i])) {
|
310 | mapping = prevMapping;
|
311 | } else {
|
312 | mapping = { atomList, keyList };
|
313 | }
|
314 | mappingCache.set(arr, mapping);
|
315 | return mapping;
|
316 | };
|
317 | const refAtom = atom(() => ({}));
|
318 | const read = (get) => {
|
319 | const ref = get(refAtom);
|
320 | const arr = get(arrAtom);
|
321 | const mapping = getMapping(arr, ref.prev);
|
322 | ref.prev = arr;
|
323 | return mapping.atomList;
|
324 | };
|
325 | const write = (get, set, action) => {
|
326 | if ("read" in action) {
|
327 | console.warn("atomToRemove is deprecated. use action with type");
|
328 | action = { type: "remove", atom: action };
|
329 | }
|
330 | switch (action.type) {
|
331 | case "remove": {
|
332 | const index = get(splittedAtom).indexOf(action.atom);
|
333 | if (index >= 0) {
|
334 | const arr = get(arrAtom);
|
335 | set(arrAtom, [
|
336 | ...arr.slice(0, index),
|
337 | ...arr.slice(index + 1)
|
338 | ]);
|
339 | }
|
340 | break;
|
341 | }
|
342 | case "insert": {
|
343 | const index = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length;
|
344 | if (index >= 0) {
|
345 | const arr = get(arrAtom);
|
346 | set(arrAtom, [
|
347 | ...arr.slice(0, index),
|
348 | action.value,
|
349 | ...arr.slice(index)
|
350 | ]);
|
351 | }
|
352 | break;
|
353 | }
|
354 | case "move": {
|
355 | const index1 = get(splittedAtom).indexOf(action.atom);
|
356 | const index2 = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length;
|
357 | if (index1 >= 0 && index2 >= 0) {
|
358 | const arr = get(arrAtom);
|
359 | if (index1 < index2) {
|
360 | set(arrAtom, [
|
361 | ...arr.slice(0, index1),
|
362 | ...arr.slice(index1 + 1, index2),
|
363 | arr[index1],
|
364 | ...arr.slice(index2)
|
365 | ]);
|
366 | } else {
|
367 | set(arrAtom, [
|
368 | ...arr.slice(0, index2),
|
369 | arr[index1],
|
370 | ...arr.slice(index2, index1),
|
371 | ...arr.slice(index1 + 1)
|
372 | ]);
|
373 | }
|
374 | }
|
375 | break;
|
376 | }
|
377 | }
|
378 | };
|
379 | const splittedAtom = isWritable(arrAtom) ? atom(read, write) : atom(read);
|
380 | return splittedAtom;
|
381 | }, keyExtractor ? [arrAtom, keyExtractor] : [arrAtom]);
|
382 | }
|
383 |
|
384 | function atomWithDefault(getDefault) {
|
385 | const EMPTY = Symbol();
|
386 | const overwrittenAtom = atom(EMPTY);
|
387 | const anAtom = atom((get) => {
|
388 | const overwritten = get(overwrittenAtom);
|
389 | if (overwritten !== EMPTY) {
|
390 | return overwritten;
|
391 | }
|
392 | return getDefault(get);
|
393 | }, (get, set, update) => {
|
394 | if (update === RESET) {
|
395 | return set(overwrittenAtom, EMPTY);
|
396 | }
|
397 | return set(overwrittenAtom, typeof update === "function" ? update(get(anAtom)) : update);
|
398 | });
|
399 | return anAtom;
|
400 | }
|
401 |
|
402 | const memoizeAtom$1 = createMemoizeAtom();
|
403 | const emptyArrayAtom = atom(() => []);
|
404 | function waitForAll(atoms) {
|
405 | const createAtom = () => {
|
406 | const unwrappedAtoms = unwrapAtoms(atoms);
|
407 | const derivedAtom = atom((get) => {
|
408 | const promises = [];
|
409 | const values = unwrappedAtoms.map((anAtom, index) => {
|
410 | try {
|
411 | return get(anAtom);
|
412 | } catch (e) {
|
413 | if (e instanceof Promise) {
|
414 | promises[index] = e;
|
415 | } else {
|
416 | throw e;
|
417 | }
|
418 | }
|
419 | });
|
420 | if (promises.length) {
|
421 | throw Promise.all(promises);
|
422 | }
|
423 | return wrapResults(atoms, values);
|
424 | });
|
425 | return derivedAtom;
|
426 | };
|
427 | if (Array.isArray(atoms)) {
|
428 | if (atoms.length) {
|
429 | return memoizeAtom$1(createAtom, atoms);
|
430 | }
|
431 | return emptyArrayAtom;
|
432 | }
|
433 | return createAtom();
|
434 | }
|
435 | const unwrapAtoms = (atoms) => Array.isArray(atoms) ? atoms : Object.getOwnPropertyNames(atoms).map((key) => atoms[key]);
|
436 | const wrapResults = (atoms, results) => Array.isArray(atoms) ? results : Object.getOwnPropertyNames(atoms).reduce((out, key, idx) => ({ ...out, [key]: results[idx] }), {});
|
437 |
|
438 | function createJSONStorage(getStringStorage) {
|
439 | let lastStr;
|
440 | let lastValue;
|
441 | return {
|
442 | getItem: (key) => {
|
443 | const parse = (str2) => {
|
444 | str2 = str2 || "";
|
445 | if (lastStr !== str2) {
|
446 | lastValue = JSON.parse(str2);
|
447 | lastStr = str2;
|
448 | }
|
449 | return lastValue;
|
450 | };
|
451 | const str = getStringStorage().getItem(key);
|
452 | if (str instanceof Promise) {
|
453 | return str.then(parse);
|
454 | }
|
455 | return parse(str);
|
456 | },
|
457 | setItem: (key, newValue) => getStringStorage().setItem(key, JSON.stringify(newValue)),
|
458 | removeItem: (key) => getStringStorage().removeItem(key)
|
459 | };
|
460 | }
|
461 | const defaultStorage = createJSONStorage(() => localStorage);
|
462 | defaultStorage.subscribe = (key, callback) => {
|
463 | const storageEventCallback = (e) => {
|
464 | if (e.key === key && e.newValue) {
|
465 | callback(JSON.parse(e.newValue));
|
466 | }
|
467 | };
|
468 | window.addEventListener("storage", storageEventCallback);
|
469 | return () => {
|
470 | window.removeEventListener("storage", storageEventCallback);
|
471 | };
|
472 | };
|
473 | function atomWithStorage(key, initialValue, storage = defaultStorage) {
|
474 | const getInitialValue = () => {
|
475 | try {
|
476 | const value = storage.getItem(key);
|
477 | if (value instanceof Promise) {
|
478 | return value.catch(() => initialValue);
|
479 | }
|
480 | return value;
|
481 | } catch {
|
482 | return initialValue;
|
483 | }
|
484 | };
|
485 | const baseAtom = atom(storage.delayInit ? initialValue : getInitialValue());
|
486 | baseAtom.onMount = (setAtom) => {
|
487 | let unsub;
|
488 | if (storage.subscribe) {
|
489 | unsub = storage.subscribe(key, setAtom);
|
490 | setAtom(getInitialValue());
|
491 | }
|
492 | if (storage.delayInit) {
|
493 | const value = getInitialValue();
|
494 | if (value instanceof Promise) {
|
495 | value.then(setAtom);
|
496 | } else {
|
497 | setAtom(value);
|
498 | }
|
499 | }
|
500 | return unsub;
|
501 | };
|
502 | const anAtom = atom((get) => get(baseAtom), (get, set, update) => {
|
503 | if (update === RESET) {
|
504 | set(baseAtom, initialValue);
|
505 | return storage.removeItem(key);
|
506 | }
|
507 | const newValue = typeof update === "function" ? update(get(baseAtom)) : update;
|
508 | set(baseAtom, newValue);
|
509 | return storage.setItem(key, newValue);
|
510 | });
|
511 | return anAtom;
|
512 | }
|
513 | function atomWithHash(key, initialValue, options) {
|
514 | const serialize = (options == null ? void 0 : options.serialize) || JSON.stringify;
|
515 | const deserialize = (options == null ? void 0 : options.deserialize) || JSON.parse;
|
516 | const subscribe = (options == null ? void 0 : options.subscribe) || ((callback) => {
|
517 | window.addEventListener("hashchange", callback);
|
518 | return () => {
|
519 | window.removeEventListener("hashchange", callback);
|
520 | };
|
521 | });
|
522 | const hashStorage = {
|
523 | getItem: (key2) => {
|
524 | const searchParams = new URLSearchParams(location.hash.slice(1));
|
525 | const storedValue = searchParams.get(key2);
|
526 | if (storedValue === null) {
|
527 | throw new Error("no value stored");
|
528 | }
|
529 | return deserialize(storedValue);
|
530 | },
|
531 | setItem: (key2, newValue) => {
|
532 | const searchParams = new URLSearchParams(location.hash.slice(1));
|
533 | searchParams.set(key2, serialize(newValue));
|
534 | if (options == null ? void 0 : options.replaceState) {
|
535 | history.replaceState(null, "", "#" + searchParams.toString());
|
536 | } else {
|
537 | location.hash = searchParams.toString();
|
538 | }
|
539 | },
|
540 | removeItem: (key2) => {
|
541 | const searchParams = new URLSearchParams(location.hash.slice(1));
|
542 | searchParams.delete(key2);
|
543 | if (options == null ? void 0 : options.replaceState) {
|
544 | history.replaceState(null, "", "#" + searchParams.toString());
|
545 | } else {
|
546 | location.hash = searchParams.toString();
|
547 | }
|
548 | },
|
549 | ...(options == null ? void 0 : options.delayInit) && { delayInit: true },
|
550 | subscribe: (key2, setValue) => {
|
551 | const callback = () => {
|
552 | const searchParams = new URLSearchParams(location.hash.slice(1));
|
553 | const str = searchParams.get(key2);
|
554 | if (str !== null) {
|
555 | setValue(deserialize(str));
|
556 | } else {
|
557 | setValue(initialValue);
|
558 | }
|
559 | };
|
560 | return subscribe(callback);
|
561 | }
|
562 | };
|
563 | return atomWithStorage(key, initialValue, hashStorage);
|
564 | }
|
565 |
|
566 | function atomWithObservable(createObservable, options) {
|
567 | const observableResultAtom = atom((get) => {
|
568 | var _a;
|
569 | let observable = createObservable(get);
|
570 | const itself = (_a = observable[Symbol.observable]) == null ? void 0 : _a.call(observable);
|
571 | if (itself) {
|
572 | observable = itself;
|
573 | }
|
574 | const EMPTY = Symbol();
|
575 | let resolveEmittedInitialValue = null;
|
576 | let initialEmittedValue = (options == null ? void 0 : options.initialValue) === void 0 ? new Promise((resolve) => {
|
577 | resolveEmittedInitialValue = resolve;
|
578 | }) : void 0;
|
579 | let initialValueWasEmitted = false;
|
580 | let emittedValueBeforeMount = EMPTY;
|
581 | let isSync = true;
|
582 | let setData = (data) => {
|
583 | if ((options == null ? void 0 : options.initialValue) === void 0 && !initialValueWasEmitted) {
|
584 | if (isSync) {
|
585 | initialEmittedValue = data;
|
586 | }
|
587 | resolveEmittedInitialValue == null ? void 0 : resolveEmittedInitialValue(data);
|
588 | initialValueWasEmitted = true;
|
589 | resolveEmittedInitialValue = null;
|
590 | } else {
|
591 | emittedValueBeforeMount = data;
|
592 | }
|
593 | };
|
594 | const dataListener = (data) => {
|
595 | setData(data);
|
596 | };
|
597 | const errorListener = (error) => {
|
598 | setData(Promise.reject(error));
|
599 | };
|
600 | let subscription = null;
|
601 | let initialValue;
|
602 | if ((options == null ? void 0 : options.initialValue) !== void 0) {
|
603 | initialValue = getInitialValue(options);
|
604 | } else {
|
605 | subscription = observable.subscribe(dataListener, errorListener);
|
606 | initialValue = initialEmittedValue;
|
607 | }
|
608 | isSync = false;
|
609 | const dataAtom = atom(initialValue);
|
610 | dataAtom.onMount = (update) => {
|
611 | setData = update;
|
612 | if (emittedValueBeforeMount !== EMPTY) {
|
613 | update(emittedValueBeforeMount);
|
614 | }
|
615 | if (!subscription) {
|
616 | subscription = observable.subscribe(dataListener, errorListener);
|
617 | }
|
618 | return () => {
|
619 | subscription == null ? void 0 : subscription.unsubscribe();
|
620 | subscription = null;
|
621 | };
|
622 | };
|
623 | return { dataAtom, observable };
|
624 | });
|
625 | const observableAtom = atom((get) => {
|
626 | const { dataAtom } = get(observableResultAtom);
|
627 | return get(dataAtom);
|
628 | }, (get, set, data) => {
|
629 | const { dataAtom, observable } = get(observableResultAtom);
|
630 | if ("next" in observable) {
|
631 | let subscription = null;
|
632 | const callback = (data2) => {
|
633 | set(dataAtom, data2);
|
634 | subscription == null ? void 0 : subscription.unsubscribe();
|
635 | };
|
636 | subscription = observable.subscribe(callback);
|
637 | observable.next(data);
|
638 | } else {
|
639 | throw new Error("observable is not subject");
|
640 | }
|
641 | });
|
642 | return observableAtom;
|
643 | }
|
644 | function getInitialValue(options) {
|
645 | const initialValue = options.initialValue;
|
646 | return initialValue instanceof Function ? initialValue() : initialValue;
|
647 | }
|
648 |
|
649 | const hydratedMap = new WeakMap();
|
650 | function useHydrateAtoms(values, scope) {
|
651 | const ScopeContext = SECRET_INTERNAL_getScopeContext(scope);
|
652 | const scopeContainer = useContext(ScopeContext);
|
653 | const store = scopeContainer.s;
|
654 | const hydratedSet = getHydratedSet(scopeContainer);
|
655 | const tuplesToRestore = [];
|
656 | for (const tuple of values) {
|
657 | const atom = tuple[0];
|
658 | if (!hydratedSet.has(atom)) {
|
659 | hydratedSet.add(atom);
|
660 | tuplesToRestore.push(tuple);
|
661 | }
|
662 | }
|
663 | if (tuplesToRestore.length) {
|
664 | store[RESTORE_ATOMS](tuplesToRestore);
|
665 | }
|
666 | }
|
667 | function getHydratedSet(scopeContainer) {
|
668 | let hydratedSet = hydratedMap.get(scopeContainer);
|
669 | if (!hydratedSet) {
|
670 | hydratedSet = new WeakSet();
|
671 | hydratedMap.set(scopeContainer, hydratedSet);
|
672 | }
|
673 | return hydratedSet;
|
674 | }
|
675 |
|
676 | const memoizeAtom = createMemoizeAtom();
|
677 | const LOADING = { state: "loading" };
|
678 | function loadable(anAtom) {
|
679 | return memoizeAtom(() => {
|
680 | const loadableAtomCache = new WeakMap();
|
681 | const catchAtom = atom((get) => {
|
682 | let promise;
|
683 | try {
|
684 | const data = get(anAtom);
|
685 | const loadableAtom2 = atom({ state: "hasData", data });
|
686 | return loadableAtom2;
|
687 | } catch (error) {
|
688 | if (error instanceof Promise) {
|
689 | promise = error;
|
690 | } else {
|
691 | const loadableAtom2 = atom({
|
692 | state: "hasError",
|
693 | error
|
694 | });
|
695 | return loadableAtom2;
|
696 | }
|
697 | }
|
698 | const cached = loadableAtomCache.get(promise);
|
699 | if (cached) {
|
700 | return cached;
|
701 | }
|
702 | const loadableAtom = atom(LOADING, async (get2, set) => {
|
703 | try {
|
704 | const data = await get2(anAtom, { unstable_promise: true });
|
705 | set(loadableAtom, { state: "hasData", data });
|
706 | } catch (error) {
|
707 | set(loadableAtom, { state: "hasError", error });
|
708 | }
|
709 | });
|
710 | loadableAtom.onMount = (init) => {
|
711 | init();
|
712 | };
|
713 | loadableAtomCache.set(promise, loadableAtom);
|
714 | return loadableAtom;
|
715 | });
|
716 | const derivedAtom = atom((get) => {
|
717 | const loadableAtom = get(catchAtom);
|
718 | return get(loadableAtom);
|
719 | });
|
720 | return derivedAtom;
|
721 | }, [anAtom]);
|
722 | }
|
723 |
|
724 | })
|
725 | };
|
726 | }));
|