1 | 'use strict';
|
2 |
|
3 | var vanilla = require('jotai/vanilla');
|
4 |
|
5 | var RESET = Symbol();
|
6 |
|
7 | function atomWithReset(initialValue) {
|
8 | var anAtom = vanilla.atom(initialValue, function (get, set, update) {
|
9 | var nextValue = typeof update === 'function' ? update(get(anAtom)) : update;
|
10 | set(anAtom, nextValue === RESET ? initialValue : nextValue);
|
11 | });
|
12 | return anAtom;
|
13 | }
|
14 |
|
15 | function atomWithReducer(initialValue, reducer) {
|
16 | var anAtom = vanilla.atom(initialValue, function (get, set, action) {
|
17 | return set(anAtom, reducer(get(anAtom), action));
|
18 | });
|
19 | return anAtom;
|
20 | }
|
21 |
|
22 | function _unsupportedIterableToArray(o, minLen) {
|
23 | if (!o) return;
|
24 | if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
25 | var n = Object.prototype.toString.call(o).slice(8, -1);
|
26 | if (n === "Object" && o.constructor) n = o.constructor.name;
|
27 | if (n === "Map" || n === "Set") return Array.from(o);
|
28 | if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
29 | }
|
30 | function _arrayLikeToArray(arr, len) {
|
31 | if (len == null || len > arr.length) len = arr.length;
|
32 | for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
33 | return arr2;
|
34 | }
|
35 | function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
36 | var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
37 | if (it) return (it = it.call(o)).next.bind(it);
|
38 | if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
39 | if (it) o = it;
|
40 | var i = 0;
|
41 | return function () {
|
42 | if (i >= o.length) return {
|
43 | done: true
|
44 | };
|
45 | return {
|
46 | done: false,
|
47 | value: o[i++]
|
48 | };
|
49 | };
|
50 | }
|
51 | throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
52 | }
|
53 |
|
54 | function atomFamily(initializeAtom, areEqual) {
|
55 | var shouldRemove = null;
|
56 | var atoms = new Map();
|
57 | var createAtom = function createAtom(param) {
|
58 | var item;
|
59 | if (areEqual === undefined) {
|
60 | item = atoms.get(param);
|
61 | } else {
|
62 | for (var _iterator = _createForOfIteratorHelperLoose(atoms), _step; !(_step = _iterator()).done;) {
|
63 | var _step$value = _step.value,
|
64 | key = _step$value[0],
|
65 | value = _step$value[1];
|
66 | if (areEqual(key, param)) {
|
67 | item = value;
|
68 | break;
|
69 | }
|
70 | }
|
71 | }
|
72 | if (item !== undefined) {
|
73 | if (shouldRemove != null && shouldRemove(item[1], param)) {
|
74 | createAtom.remove(param);
|
75 | } else {
|
76 | return item[0];
|
77 | }
|
78 | }
|
79 | var newAtom = initializeAtom(param);
|
80 | atoms.set(param, [newAtom, Date.now()]);
|
81 | return newAtom;
|
82 | };
|
83 | createAtom.remove = function (param) {
|
84 | if (areEqual === undefined) {
|
85 | atoms.delete(param);
|
86 | } else {
|
87 | for (var _iterator2 = _createForOfIteratorHelperLoose(atoms), _step2; !(_step2 = _iterator2()).done;) {
|
88 | var _step2$value = _step2.value,
|
89 | key = _step2$value[0];
|
90 | if (areEqual(key, param)) {
|
91 | atoms.delete(key);
|
92 | break;
|
93 | }
|
94 | }
|
95 | }
|
96 | };
|
97 | createAtom.setShouldRemove = function (fn) {
|
98 | shouldRemove = fn;
|
99 | if (!shouldRemove) return;
|
100 | for (var _iterator3 = _createForOfIteratorHelperLoose(atoms), _step3; !(_step3 = _iterator3()).done;) {
|
101 | var _step3$value = _step3.value,
|
102 | key = _step3$value[0],
|
103 | value = _step3$value[1];
|
104 | if (shouldRemove(value[1], key)) {
|
105 | atoms.delete(key);
|
106 | }
|
107 | }
|
108 | };
|
109 | return createAtom;
|
110 | }
|
111 |
|
112 | var getCached$2 = function getCached(c, m, k) {
|
113 | return (m.has(k) ? m : m.set(k, c())).get(k);
|
114 | };
|
115 | var cache1$4 = new WeakMap();
|
116 | var memo3 = function memo3(create, dep1, dep2, dep3) {
|
117 | var cache2 = getCached$2(function () {
|
118 | return new WeakMap();
|
119 | }, cache1$4, dep1);
|
120 | var cache3 = getCached$2(function () {
|
121 | return new WeakMap();
|
122 | }, cache2, dep2);
|
123 | return getCached$2(create, cache3, dep3);
|
124 | };
|
125 | function selectAtom(anAtom, selector, equalityFn) {
|
126 | if (equalityFn === void 0) {
|
127 | equalityFn = Object.is;
|
128 | }
|
129 | return memo3(function () {
|
130 | var EMPTY = Symbol();
|
131 | var selectValue = function selectValue(_ref) {
|
132 | var value = _ref[0],
|
133 | prevSlice = _ref[1];
|
134 | var slice = selector(value);
|
135 | if (prevSlice !== EMPTY && equalityFn(prevSlice, slice)) {
|
136 | return prevSlice;
|
137 | }
|
138 | return slice;
|
139 | };
|
140 | var derivedAtom = vanilla.atom(function (get) {
|
141 | var prev = get(derivedAtom);
|
142 | var value = get(anAtom);
|
143 | if (value instanceof Promise || prev instanceof Promise) {
|
144 | return Promise.all([value, prev]).then(selectValue);
|
145 | }
|
146 | return selectValue([value, prev]);
|
147 | });
|
148 | derivedAtom.init = EMPTY;
|
149 | return derivedAtom;
|
150 | }, anAtom, selector, equalityFn);
|
151 | }
|
152 |
|
153 | var cache1$3 = new WeakMap();
|
154 | var memo1$1 = function memo1(create, dep1) {
|
155 | return (cache1$3.has(dep1) ? cache1$3 : cache1$3.set(dep1, create())).get(dep1);
|
156 | };
|
157 | var deepFreeze = function deepFreeze(obj) {
|
158 | if (typeof obj !== 'object' || obj === null) return;
|
159 | Object.freeze(obj);
|
160 | var propNames = Object.getOwnPropertyNames(obj);
|
161 | for (var _iterator = _createForOfIteratorHelperLoose(propNames), _step; !(_step = _iterator()).done;) {
|
162 | var name = _step.value;
|
163 | var value = obj[name];
|
164 | deepFreeze(value);
|
165 | }
|
166 | return obj;
|
167 | };
|
168 | function freezeAtom(anAtom) {
|
169 | return memo1$1(function () {
|
170 | var frozenAtom = vanilla.atom(function (get) {
|
171 | return deepFreeze(get(anAtom));
|
172 | }, function (_get, set, arg) {
|
173 | return set(anAtom, arg);
|
174 | });
|
175 | return frozenAtom;
|
176 | }, anAtom);
|
177 | }
|
178 | function freezeAtomCreator(createAtom) {
|
179 | return function () {
|
180 | var anAtom = createAtom.apply(void 0, arguments);
|
181 | var origRead = anAtom.read;
|
182 | anAtom.read = function (get, options) {
|
183 | return deepFreeze(origRead(get, options));
|
184 | };
|
185 | return anAtom;
|
186 | };
|
187 | }
|
188 |
|
189 | var getCached$1 = function getCached(c, m, k) {
|
190 | return (m.has(k) ? m : m.set(k, c())).get(k);
|
191 | };
|
192 | var cache1$2 = new WeakMap();
|
193 | var memo2$1 = function memo2(create, dep1, dep2) {
|
194 | var cache2 = getCached$1(function () {
|
195 | return new WeakMap();
|
196 | }, cache1$2, dep1);
|
197 | return getCached$1(create, cache2, dep2);
|
198 | };
|
199 | var cacheKeyForEmptyKeyExtractor = {};
|
200 | var isWritable = function isWritable(atom) {
|
201 | return !!atom.write;
|
202 | };
|
203 | var isFunction = function isFunction(x) {
|
204 | return typeof x === 'function';
|
205 | };
|
206 | function splitAtom(arrAtom, keyExtractor) {
|
207 | return memo2$1(function () {
|
208 | var mappingCache = new WeakMap();
|
209 | var getMapping = function getMapping(arr, prev) {
|
210 | var mapping = mappingCache.get(arr);
|
211 | if (mapping) {
|
212 | return mapping;
|
213 | }
|
214 | var prevMapping = prev && mappingCache.get(prev);
|
215 | var atomList = [];
|
216 | var keyList = [];
|
217 | arr.forEach(function (item, index) {
|
218 | var key = keyExtractor ? keyExtractor(item) : index;
|
219 | keyList[index] = key;
|
220 | var cachedAtom = prevMapping && prevMapping.atomList[prevMapping.keyList.indexOf(key)];
|
221 | if (cachedAtom) {
|
222 | atomList[index] = cachedAtom;
|
223 | return;
|
224 | }
|
225 | var read = function read(get) {
|
226 | var prev = get(mappingAtom);
|
227 | var currArr = get(arrAtom);
|
228 | var mapping = getMapping(currArr, prev == null ? void 0 : prev.arr);
|
229 | var index = mapping.keyList.indexOf(key);
|
230 | if (index < 0 || index >= currArr.length) {
|
231 | var prevItem = arr[getMapping(arr).keyList.indexOf(key)];
|
232 | if (prevItem) {
|
233 | return prevItem;
|
234 | }
|
235 | throw new Error('splitAtom: index out of bounds for read');
|
236 | }
|
237 | return currArr[index];
|
238 | };
|
239 | var write = function write(get, set, update) {
|
240 | var prev = get(mappingAtom);
|
241 | var arr = get(arrAtom);
|
242 | var mapping = getMapping(arr, prev == null ? void 0 : prev.arr);
|
243 | var index = mapping.keyList.indexOf(key);
|
244 | if (index < 0 || index >= arr.length) {
|
245 | throw new Error('splitAtom: index out of bounds for write');
|
246 | }
|
247 | var nextItem = isFunction(update) ? update(arr[index]) : update;
|
248 | set(arrAtom, [].concat(arr.slice(0, index), [nextItem], arr.slice(index + 1)));
|
249 | };
|
250 | atomList[index] = isWritable(arrAtom) ? vanilla.atom(read, write) : vanilla.atom(read);
|
251 | });
|
252 | if (prevMapping && prevMapping.keyList.length === keyList.length && prevMapping.keyList.every(function (x, i) {
|
253 | return x === keyList[i];
|
254 | })) {
|
255 | mapping = prevMapping;
|
256 | } else {
|
257 | mapping = {
|
258 | arr: arr,
|
259 | atomList: atomList,
|
260 | keyList: keyList
|
261 | };
|
262 | }
|
263 | mappingCache.set(arr, mapping);
|
264 | return mapping;
|
265 | };
|
266 | var mappingAtom = vanilla.atom(function (get) {
|
267 | var prev = get(mappingAtom);
|
268 | var arr = get(arrAtom);
|
269 | var mapping = getMapping(arr, prev == null ? void 0 : prev.arr);
|
270 | return mapping;
|
271 | });
|
272 | mappingAtom.init = undefined;
|
273 | var splittedAtom = isWritable(arrAtom) ? vanilla.atom(function (get) {
|
274 | return get(mappingAtom).atomList;
|
275 | }, function (get, set, action) {
|
276 | switch (action.type) {
|
277 | case 'remove':
|
278 | {
|
279 | var index = get(splittedAtom).indexOf(action.atom);
|
280 | if (index >= 0) {
|
281 | var arr = get(arrAtom);
|
282 | set(arrAtom, [].concat(arr.slice(0, index), arr.slice(index + 1)));
|
283 | }
|
284 | break;
|
285 | }
|
286 | case 'insert':
|
287 | {
|
288 | var _index = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length;
|
289 | if (_index >= 0) {
|
290 | var _arr = get(arrAtom);
|
291 | set(arrAtom, [].concat(_arr.slice(0, _index), [action.value], _arr.slice(_index)));
|
292 | }
|
293 | break;
|
294 | }
|
295 | case 'move':
|
296 | {
|
297 | var index1 = get(splittedAtom).indexOf(action.atom);
|
298 | var index2 = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length;
|
299 | if (index1 >= 0 && index2 >= 0) {
|
300 | var _arr2 = get(arrAtom);
|
301 | if (index1 < index2) {
|
302 | set(arrAtom, [].concat(_arr2.slice(0, index1), _arr2.slice(index1 + 1, index2), [_arr2[index1]], _arr2.slice(index2)));
|
303 | } else {
|
304 | set(arrAtom, [].concat(_arr2.slice(0, index2), [_arr2[index1]], _arr2.slice(index2, index1), _arr2.slice(index1 + 1)));
|
305 | }
|
306 | }
|
307 | break;
|
308 | }
|
309 | }
|
310 | }) : vanilla.atom(function (get) {
|
311 | return get(mappingAtom).atomList;
|
312 | });
|
313 | return splittedAtom;
|
314 | }, arrAtom, keyExtractor || cacheKeyForEmptyKeyExtractor);
|
315 | }
|
316 |
|
317 | var updateValue = function updateValue(prevValue, update) {
|
318 | return typeof update === 'function' ? update(prevValue) : update;
|
319 | };
|
320 | function atomWithDefault(getDefault) {
|
321 | var EMPTY = Symbol();
|
322 | var overwrittenAtom = vanilla.atom(EMPTY);
|
323 | var anAtom = vanilla.atom(function (get, options) {
|
324 | var overwritten = get(overwrittenAtom);
|
325 | if (overwritten !== EMPTY) {
|
326 | return overwritten;
|
327 | }
|
328 | return getDefault(get, options);
|
329 | }, function (get, set, update) {
|
330 | if (update === RESET) {
|
331 | return set(overwrittenAtom, EMPTY);
|
332 | }
|
333 | var prevValue = get(anAtom);
|
334 | if (prevValue instanceof Promise) {
|
335 | return prevValue.then(function (v) {
|
336 | return set(overwrittenAtom, updateValue(v, update));
|
337 | });
|
338 | }
|
339 | return set(overwrittenAtom, updateValue(prevValue, update));
|
340 | });
|
341 | return anAtom;
|
342 | }
|
343 |
|
344 | var NO_STORAGE_VALUE = Symbol();
|
345 | function createJSONStorage(getStringStorage) {
|
346 | var lastStr;
|
347 | var lastValue;
|
348 | var storage = {
|
349 | getItem: function getItem(key) {
|
350 | var _getStringStorage$get, _getStringStorage;
|
351 | var parse = function parse(str) {
|
352 | str = str || '';
|
353 | if (lastStr !== str) {
|
354 | try {
|
355 | lastValue = JSON.parse(str);
|
356 | } catch (_unused) {
|
357 | return NO_STORAGE_VALUE;
|
358 | }
|
359 | lastStr = str;
|
360 | }
|
361 | return lastValue;
|
362 | };
|
363 | var str = (_getStringStorage$get = (_getStringStorage = getStringStorage()) == null ? void 0 : _getStringStorage.getItem(key)) != null ? _getStringStorage$get : null;
|
364 | if (str instanceof Promise) {
|
365 | return str.then(parse);
|
366 | }
|
367 | return parse(str);
|
368 | },
|
369 | setItem: function setItem(key, newValue) {
|
370 | var _getStringStorage2;
|
371 | return (_getStringStorage2 = getStringStorage()) == null ? void 0 : _getStringStorage2.setItem(key, JSON.stringify(newValue));
|
372 | },
|
373 | removeItem: function removeItem(key) {
|
374 | var _getStringStorage3;
|
375 | return (_getStringStorage3 = getStringStorage()) == null ? void 0 : _getStringStorage3.removeItem(key);
|
376 | }
|
377 | };
|
378 | if (typeof window !== 'undefined' && typeof window.addEventListener === 'function') {
|
379 | storage.subscribe = function (key, callback) {
|
380 | var storageEventCallback = function storageEventCallback(e) {
|
381 | if (e.key === key && e.newValue) {
|
382 | callback(JSON.parse(e.newValue));
|
383 | }
|
384 | };
|
385 | window.addEventListener('storage', storageEventCallback);
|
386 | return function () {
|
387 | window.removeEventListener('storage', storageEventCallback);
|
388 | };
|
389 | };
|
390 | }
|
391 | return storage;
|
392 | }
|
393 | var defaultStorage = createJSONStorage(function () {
|
394 | return typeof window !== 'undefined' ? window.localStorage : undefined;
|
395 | });
|
396 | function atomWithStorage(key, initialValue, storage) {
|
397 | if (storage === void 0) {
|
398 | storage = defaultStorage;
|
399 | }
|
400 | var baseAtom = vanilla.atom(initialValue);
|
401 | baseAtom.onMount = function (setAtom) {
|
402 | var value = storage.getItem(key);
|
403 | if (value instanceof Promise) {
|
404 | value.then(function (v) {
|
405 | return setAtom(v === NO_STORAGE_VALUE ? initialValue : v);
|
406 | });
|
407 | } else {
|
408 | setAtom(value === NO_STORAGE_VALUE ? initialValue : value);
|
409 | }
|
410 | var unsub;
|
411 | if (storage.subscribe) {
|
412 | unsub = storage.subscribe(key, setAtom);
|
413 | }
|
414 | return unsub;
|
415 | };
|
416 | var anAtom = vanilla.atom(function (get) {
|
417 | return get(baseAtom);
|
418 | }, function (get, set, update) {
|
419 | var nextValue = typeof update === 'function' ? update(get(baseAtom)) : update;
|
420 | if (nextValue === RESET) {
|
421 | set(baseAtom, initialValue);
|
422 | return storage.removeItem(key);
|
423 | }
|
424 | set(baseAtom, nextValue);
|
425 | return storage.setItem(key, nextValue);
|
426 | });
|
427 | return anAtom;
|
428 | }
|
429 |
|
430 | function atomWithObservable(getObservable, options) {
|
431 | var returnResultData = function returnResultData(result) {
|
432 | if ('e' in result) {
|
433 | throw result.e;
|
434 | }
|
435 | return result.d;
|
436 | };
|
437 | var observableResultAtom = vanilla.atom(function (get) {
|
438 | var _observable$Symbol$ob, _observable;
|
439 | var observable = getObservable(get);
|
440 | var itself = (_observable$Symbol$ob = (_observable = observable)[Symbol.observable]) == null ? void 0 : _observable$Symbol$ob.call(_observable);
|
441 | if (itself) {
|
442 | observable = itself;
|
443 | }
|
444 | var resolve;
|
445 | var makePending = function makePending() {
|
446 | return new Promise(function (r) {
|
447 | resolve = r;
|
448 | });
|
449 | };
|
450 | var initialResult = options && 'initialValue' in options ? {
|
451 | d: typeof options.initialValue === 'function' ? options.initialValue() : options.initialValue
|
452 | } : makePending();
|
453 | var setResult;
|
454 | var lastResult;
|
455 | var listener = function listener(result) {
|
456 | lastResult = result;
|
457 | resolve == null ? void 0 : resolve(result);
|
458 | setResult == null ? void 0 : setResult(result);
|
459 | };
|
460 | var subscription;
|
461 | var timer;
|
462 | var isNotMounted = function isNotMounted() {
|
463 | return !setResult;
|
464 | };
|
465 | var start = function start() {
|
466 | if (subscription) {
|
467 | clearTimeout(timer);
|
468 | subscription.unsubscribe();
|
469 | }
|
470 | subscription = observable.subscribe({
|
471 | next: function next(d) {
|
472 | return listener({
|
473 | d: d
|
474 | });
|
475 | },
|
476 | error: function error(e) {
|
477 | return listener({
|
478 | e: e
|
479 | });
|
480 | },
|
481 | complete: function complete() {}
|
482 | });
|
483 | if (isNotMounted() && options != null && options.unstable_timeout) {
|
484 | timer = setTimeout(function () {
|
485 | if (subscription) {
|
486 | subscription.unsubscribe();
|
487 | subscription = undefined;
|
488 | }
|
489 | }, options.unstable_timeout);
|
490 | }
|
491 | };
|
492 | start();
|
493 | var resultAtom = vanilla.atom(lastResult || initialResult);
|
494 | resultAtom.onMount = function (update) {
|
495 | setResult = update;
|
496 | if (lastResult) {
|
497 | update(lastResult);
|
498 | }
|
499 | if (subscription) {
|
500 | clearTimeout(timer);
|
501 | } else {
|
502 | start();
|
503 | }
|
504 | return function () {
|
505 | setResult = undefined;
|
506 | if (subscription) {
|
507 | subscription.unsubscribe();
|
508 | subscription = undefined;
|
509 | }
|
510 | };
|
511 | };
|
512 | return [resultAtom, observable, makePending, start, isNotMounted];
|
513 | });
|
514 | var observableAtom = vanilla.atom(function (get) {
|
515 | var _get = get(observableResultAtom),
|
516 | resultAtom = _get[0];
|
517 | var result = get(resultAtom);
|
518 | if (result instanceof Promise) {
|
519 | return result.then(returnResultData);
|
520 | }
|
521 | return returnResultData(result);
|
522 | }, function (get, set, data) {
|
523 | var _get2 = get(observableResultAtom),
|
524 | resultAtom = _get2[0],
|
525 | observable = _get2[1],
|
526 | makePending = _get2[2],
|
527 | start = _get2[3],
|
528 | isNotMounted = _get2[4];
|
529 | if ('next' in observable) {
|
530 | if (isNotMounted()) {
|
531 | set(resultAtom, makePending());
|
532 | start();
|
533 | }
|
534 | observable.next(data);
|
535 | } else {
|
536 | throw new Error('observable is not subject');
|
537 | }
|
538 | });
|
539 | return observableAtom;
|
540 | }
|
541 |
|
542 | var cache1$1 = new WeakMap();
|
543 | var memo1 = function memo1(create, dep1) {
|
544 | return (cache1$1.has(dep1) ? cache1$1 : cache1$1.set(dep1, create())).get(dep1);
|
545 | };
|
546 | var LOADING = {
|
547 | state: 'loading'
|
548 | };
|
549 | function loadable(anAtom) {
|
550 | return memo1(function () {
|
551 | var loadableCache = new WeakMap();
|
552 | var refreshAtom = vanilla.atom(0);
|
553 | var derivedAtom = vanilla.atom(function (get, _ref) {
|
554 | var setSelf = _ref.setSelf;
|
555 | get(refreshAtom);
|
556 | var promise = get(anAtom);
|
557 | if (!(promise instanceof Promise)) {
|
558 | return {
|
559 | state: 'hasData',
|
560 | data: promise
|
561 | };
|
562 | }
|
563 | var cached = loadableCache.get(promise);
|
564 | if (cached) {
|
565 | return cached;
|
566 | }
|
567 | loadableCache.set(promise, LOADING);
|
568 | promise.then(function (data) {
|
569 | loadableCache.set(promise, {
|
570 | state: 'hasData',
|
571 | data: data
|
572 | });
|
573 | }, function (error) {
|
574 | loadableCache.set(promise, {
|
575 | state: 'hasError',
|
576 | error: error
|
577 | });
|
578 | }).finally(setSelf);
|
579 | return LOADING;
|
580 | }, function (_get, set) {
|
581 | set(refreshAtom, function (c) {
|
582 | return c + 1;
|
583 | });
|
584 | });
|
585 | return vanilla.atom(function (get) {
|
586 | return get(derivedAtom);
|
587 | });
|
588 | }, anAtom);
|
589 | }
|
590 |
|
591 | var getCached = function getCached(c, m, k) {
|
592 | return (m.has(k) ? m : m.set(k, c())).get(k);
|
593 | };
|
594 | var cache1 = new WeakMap();
|
595 | var memo2 = function memo2(create, dep1, dep2) {
|
596 | var cache2 = getCached(function () {
|
597 | return new WeakMap();
|
598 | }, cache1, dep1);
|
599 | return getCached(create, cache2, dep2);
|
600 | };
|
601 | var defaultFallback = function defaultFallback() {
|
602 | return undefined;
|
603 | };
|
604 | function unwrap(anAtom, fallback) {
|
605 | if (fallback === void 0) {
|
606 | fallback = defaultFallback;
|
607 | }
|
608 | return memo2(function () {
|
609 | var promiseErrorCache = new WeakMap();
|
610 | var promiseResultCache = new WeakMap();
|
611 | var refreshAtom = vanilla.atom(0);
|
612 | var promiseAndValueAtom = vanilla.atom(function (get, _ref) {
|
613 | var setSelf = _ref.setSelf;
|
614 | get(refreshAtom);
|
615 | var prev = get(promiseAndValueAtom);
|
616 | var promise = get(anAtom);
|
617 | if (promise === (prev == null ? void 0 : prev.p)) {
|
618 | if (promiseErrorCache.has(promise)) {
|
619 | throw promiseErrorCache.get(promise);
|
620 | }
|
621 | if (promiseResultCache.has(promise)) {
|
622 | return {
|
623 | p: promise,
|
624 | v: promiseResultCache.get(promise)
|
625 | };
|
626 | }
|
627 | }
|
628 | if (promise !== (prev == null ? void 0 : prev.p)) {
|
629 | promise.then(function (v) {
|
630 | return promiseResultCache.set(promise, v);
|
631 | }, function (e) {
|
632 | return promiseErrorCache.set(promise, e);
|
633 | }).finally(setSelf);
|
634 | }
|
635 | if (prev && 'v' in prev) {
|
636 | return {
|
637 | p: promise,
|
638 | f: fallback(prev.v)
|
639 | };
|
640 | }
|
641 | return {
|
642 | p: promise,
|
643 | f: fallback()
|
644 | };
|
645 | }, function (_get, set) {
|
646 | set(refreshAtom, function (c) {
|
647 | return c + 1;
|
648 | });
|
649 | });
|
650 | promiseAndValueAtom.init = undefined;
|
651 | return vanilla.atom(function (get) {
|
652 | var state = get(promiseAndValueAtom);
|
653 | if ('v' in state) {
|
654 | return state.v;
|
655 | }
|
656 | return state.f;
|
657 | });
|
658 | }, anAtom, fallback);
|
659 | }
|
660 |
|
661 | exports.RESET = RESET;
|
662 | exports.atomFamily = atomFamily;
|
663 | exports.atomWithDefault = atomWithDefault;
|
664 | exports.atomWithObservable = atomWithObservable;
|
665 | exports.atomWithReducer = atomWithReducer;
|
666 | exports.atomWithReset = atomWithReset;
|
667 | exports.atomWithStorage = atomWithStorage;
|
668 | exports.createJSONStorage = createJSONStorage;
|
669 | exports.freezeAtom = freezeAtom;
|
670 | exports.freezeAtomCreator = freezeAtomCreator;
|
671 | exports.loadable = loadable;
|
672 | exports.selectAtom = selectAtom;
|
673 | exports.splitAtom = splitAtom;
|
674 | exports.unstable_NO_STORAGE_VALUE = NO_STORAGE_VALUE;
|
675 | exports.unstable_unwrap = unwrap;
|