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 | var __defProp$1 = Object.defineProperty;
|
403 | var __defProps$1 = Object.defineProperties;
|
404 | var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
|
405 | var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
406 | var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
407 | var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
408 | var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
409 | var __spreadValues$1 = (a, b) => {
|
410 | for (var prop in b || (b = {}))
|
411 | if (__hasOwnProp$1.call(b, prop))
|
412 | __defNormalProp$1(a, prop, b[prop]);
|
413 | if (__getOwnPropSymbols$1)
|
414 | for (var prop of __getOwnPropSymbols$1(b)) {
|
415 | if (__propIsEnum$1.call(b, prop))
|
416 | __defNormalProp$1(a, prop, b[prop]);
|
417 | }
|
418 | return a;
|
419 | };
|
420 | var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
421 | const memoizeAtom$1 = createMemoizeAtom();
|
422 | function waitForAll(atoms) {
|
423 | const createAtom = () => {
|
424 | const unwrappedAtoms = unwrapAtoms(atoms);
|
425 | const derivedAtom = atom((get) => {
|
426 | const promises = [];
|
427 | const values = unwrappedAtoms.map((anAtom, index) => {
|
428 | try {
|
429 | return get(anAtom);
|
430 | } catch (e) {
|
431 | if (e instanceof Promise) {
|
432 | promises[index] = e;
|
433 | } else {
|
434 | throw e;
|
435 | }
|
436 | }
|
437 | });
|
438 | if (promises.length) {
|
439 | throw Promise.all(promises);
|
440 | }
|
441 | return wrapResults(atoms, values);
|
442 | });
|
443 | return derivedAtom;
|
444 | };
|
445 | if (Array.isArray(atoms)) {
|
446 | return memoizeAtom$1(createAtom, atoms);
|
447 | }
|
448 | return createAtom();
|
449 | }
|
450 | const unwrapAtoms = (atoms) => Array.isArray(atoms) ? atoms : Object.getOwnPropertyNames(atoms).map((key) => atoms[key]);
|
451 | const wrapResults = (atoms, results) => Array.isArray(atoms) ? results : Object.getOwnPropertyNames(atoms).reduce((out, key, idx) => __spreadProps$1(__spreadValues$1({}, out), { [key]: results[idx] }), {});
|
452 |
|
453 | var __defProp = Object.defineProperty;
|
454 | var __defProps = Object.defineProperties;
|
455 | var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
456 | var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
457 | var __hasOwnProp = Object.prototype.hasOwnProperty;
|
458 | var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
459 | var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
460 | var __spreadValues = (a, b) => {
|
461 | for (var prop in b || (b = {}))
|
462 | if (__hasOwnProp.call(b, prop))
|
463 | __defNormalProp(a, prop, b[prop]);
|
464 | if (__getOwnPropSymbols)
|
465 | for (var prop of __getOwnPropSymbols(b)) {
|
466 | if (__propIsEnum.call(b, prop))
|
467 | __defNormalProp(a, prop, b[prop]);
|
468 | }
|
469 | return a;
|
470 | };
|
471 | var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
472 | function createJSONStorage(getStringStorage) {
|
473 | let lastStr;
|
474 | let lastValue;
|
475 | return {
|
476 | getItem: (key) => {
|
477 | const parse = (str2) => {
|
478 | str2 = str2 || "";
|
479 | if (lastStr !== str2) {
|
480 | lastValue = JSON.parse(str2);
|
481 | lastStr = str2;
|
482 | }
|
483 | return lastValue;
|
484 | };
|
485 | const str = getStringStorage().getItem(key);
|
486 | if (str instanceof Promise) {
|
487 | return str.then(parse);
|
488 | }
|
489 | return parse(str);
|
490 | },
|
491 | setItem: (key, newValue) => getStringStorage().setItem(key, JSON.stringify(newValue)),
|
492 | removeItem: (key) => getStringStorage().removeItem(key)
|
493 | };
|
494 | }
|
495 | const defaultStorage = createJSONStorage(() => localStorage);
|
496 | defaultStorage.subscribe = (key, callback) => {
|
497 | const storageEventCallback = (e) => {
|
498 | if (e.key === key && e.newValue) {
|
499 | callback(JSON.parse(e.newValue));
|
500 | }
|
501 | };
|
502 | window.addEventListener("storage", storageEventCallback);
|
503 | return () => {
|
504 | window.removeEventListener("storage", storageEventCallback);
|
505 | };
|
506 | };
|
507 | function atomWithStorage(key, initialValue, storage = defaultStorage) {
|
508 | const getInitialValue = () => {
|
509 | try {
|
510 | const value = storage.getItem(key);
|
511 | if (value instanceof Promise) {
|
512 | return value.catch(() => initialValue);
|
513 | }
|
514 | return value;
|
515 | } catch {
|
516 | return initialValue;
|
517 | }
|
518 | };
|
519 | const baseAtom = atom(storage.delayInit ? initialValue : getInitialValue());
|
520 | baseAtom.onMount = (setAtom) => {
|
521 | let unsub;
|
522 | if (storage.subscribe) {
|
523 | unsub = storage.subscribe(key, setAtom);
|
524 | setAtom(getInitialValue());
|
525 | }
|
526 | if (storage.delayInit) {
|
527 | const value = getInitialValue();
|
528 | if (value instanceof Promise) {
|
529 | value.then(setAtom);
|
530 | } else {
|
531 | setAtom(value);
|
532 | }
|
533 | }
|
534 | return unsub;
|
535 | };
|
536 | const anAtom = atom((get) => get(baseAtom), (get, set, update) => {
|
537 | if (update === RESET) {
|
538 | set(baseAtom, initialValue);
|
539 | return storage.removeItem(key);
|
540 | }
|
541 | const newValue = typeof update === "function" ? update(get(baseAtom)) : update;
|
542 | set(baseAtom, newValue);
|
543 | return storage.setItem(key, newValue);
|
544 | });
|
545 | return anAtom;
|
546 | }
|
547 | function atomWithHash(key, initialValue, options) {
|
548 | const serialize = (options == null ? void 0 : options.serialize) || JSON.stringify;
|
549 | const deserialize = (options == null ? void 0 : options.deserialize) || JSON.parse;
|
550 | const subscribe = (options == null ? void 0 : options.subscribe) || ((callback) => {
|
551 | window.addEventListener("hashchange", callback);
|
552 | return () => {
|
553 | window.removeEventListener("hashchange", callback);
|
554 | };
|
555 | });
|
556 | const hashStorage = __spreadProps(__spreadValues({
|
557 | getItem: (key2) => {
|
558 | const searchParams = new URLSearchParams(location.hash.slice(1));
|
559 | const storedValue = searchParams.get(key2);
|
560 | if (storedValue === null) {
|
561 | throw new Error("no value stored");
|
562 | }
|
563 | return deserialize(storedValue);
|
564 | },
|
565 | setItem: (key2, newValue) => {
|
566 | const searchParams = new URLSearchParams(location.hash.slice(1));
|
567 | searchParams.set(key2, serialize(newValue));
|
568 | if (options == null ? void 0 : options.replaceState) {
|
569 | history.replaceState(null, "", "#" + searchParams.toString());
|
570 | } else {
|
571 | location.hash = searchParams.toString();
|
572 | }
|
573 | },
|
574 | removeItem: (key2) => {
|
575 | const searchParams = new URLSearchParams(location.hash.slice(1));
|
576 | searchParams.delete(key2);
|
577 | if (options == null ? void 0 : options.replaceState) {
|
578 | history.replaceState(null, "", "#" + searchParams.toString());
|
579 | } else {
|
580 | location.hash = searchParams.toString();
|
581 | }
|
582 | }
|
583 | }, (options == null ? void 0 : options.delayInit) && { delayInit: true }), {
|
584 | subscribe: (key2, setValue) => {
|
585 | const callback = () => {
|
586 | const searchParams = new URLSearchParams(location.hash.slice(1));
|
587 | const str = searchParams.get(key2);
|
588 | if (str !== null) {
|
589 | setValue(deserialize(str));
|
590 | } else {
|
591 | setValue(initialValue);
|
592 | }
|
593 | };
|
594 | return subscribe(callback);
|
595 | }
|
596 | });
|
597 | return atomWithStorage(key, initialValue, hashStorage);
|
598 | }
|
599 |
|
600 | function atomWithObservable(createObservable, options) {
|
601 | const observableResultAtom = atom((get) => {
|
602 | var _a;
|
603 | let observable = createObservable(get);
|
604 | const itself = (_a = observable[Symbol.observable]) == null ? void 0 : _a.call(observable);
|
605 | if (itself) {
|
606 | observable = itself;
|
607 | }
|
608 | const dataAtom = atom((options == null ? void 0 : options.initialValue) ? getInitialValue(options) : firstValueFrom(observable));
|
609 | let setData = () => {
|
610 | throw new Error("setting data without mount");
|
611 | };
|
612 | const dataListener = (data) => {
|
613 | setData(data);
|
614 | };
|
615 | const errorListener = (error) => {
|
616 | setData(Promise.reject(error));
|
617 | };
|
618 | let subscription = null;
|
619 | dataAtom.onMount = (update) => {
|
620 | setData = update;
|
621 | if (!subscription) {
|
622 | subscription = observable.subscribe(dataListener, errorListener);
|
623 | }
|
624 | return () => {
|
625 | subscription == null ? void 0 : subscription.unsubscribe();
|
626 | subscription = null;
|
627 | };
|
628 | };
|
629 | return { dataAtom, observable };
|
630 | });
|
631 | const observableAtom = atom((get) => {
|
632 | const { dataAtom } = get(observableResultAtom);
|
633 | return get(dataAtom);
|
634 | }, (get, _set, data) => {
|
635 | const { observable } = get(observableResultAtom);
|
636 | if ("next" in observable) {
|
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 | function firstValueFrom(source) {
|
649 | return new Promise((resolve, reject) => {
|
650 | let resolved = false;
|
651 | const subscription = source.subscribe({
|
652 | next: (value) => {
|
653 | resolve(value);
|
654 | resolved = true;
|
655 | if (subscription) {
|
656 | subscription.unsubscribe();
|
657 | }
|
658 | },
|
659 | error: reject,
|
660 | complete: () => {
|
661 | reject();
|
662 | }
|
663 | });
|
664 | if (resolved) {
|
665 | subscription.unsubscribe();
|
666 | }
|
667 | });
|
668 | }
|
669 |
|
670 | const hydratedMap = new WeakMap();
|
671 | function useHydrateAtoms(values, scope) {
|
672 | const ScopeContext = SECRET_INTERNAL_getScopeContext(scope);
|
673 | const scopeContainer = useContext(ScopeContext);
|
674 | const store = scopeContainer.s;
|
675 | const hydratedSet = getHydratedSet(scopeContainer);
|
676 | const tuplesToRestore = [];
|
677 | for (const tuple of values) {
|
678 | const atom = tuple[0];
|
679 | if (!hydratedSet.has(atom)) {
|
680 | hydratedSet.add(atom);
|
681 | tuplesToRestore.push(tuple);
|
682 | }
|
683 | }
|
684 | if (tuplesToRestore.length) {
|
685 | store[RESTORE_ATOMS](tuplesToRestore);
|
686 | }
|
687 | }
|
688 | function getHydratedSet(scopeContainer) {
|
689 | let hydratedSet = hydratedMap.get(scopeContainer);
|
690 | if (!hydratedSet) {
|
691 | hydratedSet = new WeakSet();
|
692 | hydratedMap.set(scopeContainer, hydratedSet);
|
693 | }
|
694 | return hydratedSet;
|
695 | }
|
696 |
|
697 | const memoizeAtom = createMemoizeAtom();
|
698 | const LOADING = { state: "loading" };
|
699 | function loadable(anAtom) {
|
700 | return memoizeAtom(() => {
|
701 | const loadableAtomCache = new WeakMap();
|
702 | const catchAtom = atom((get) => {
|
703 | let promise;
|
704 | try {
|
705 | const data = get(anAtom);
|
706 | const loadableAtom2 = atom({ state: "hasData", data });
|
707 | return loadableAtom2;
|
708 | } catch (error) {
|
709 | if (error instanceof Promise) {
|
710 | promise = error;
|
711 | } else {
|
712 | const loadableAtom2 = atom({
|
713 | state: "hasError",
|
714 | error
|
715 | });
|
716 | return loadableAtom2;
|
717 | }
|
718 | }
|
719 | const cached = loadableAtomCache.get(promise);
|
720 | if (cached) {
|
721 | return cached;
|
722 | }
|
723 | const loadableAtom = atom(LOADING, async (get2, set) => {
|
724 | try {
|
725 | const data = await get2(anAtom, { unstable_promise: true });
|
726 | set(loadableAtom, { state: "hasData", data });
|
727 | } catch (error) {
|
728 | set(loadableAtom, { state: "hasError", error });
|
729 | }
|
730 | });
|
731 | loadableAtom.onMount = (init) => {
|
732 | init();
|
733 | };
|
734 | loadableAtomCache.set(promise, loadableAtom);
|
735 | return loadableAtom;
|
736 | });
|
737 | const derivedAtom = atom((get) => {
|
738 | const loadableAtom = get(catchAtom);
|
739 | return get(loadableAtom);
|
740 | });
|
741 | return derivedAtom;
|
742 | }, [anAtom]);
|
743 | }
|
744 |
|
745 | })
|
746 | };
|
747 | }));
|