1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var react = require('react');
|
6 |
|
7 | function _extends() {
|
8 | _extends = Object.assign ? Object.assign.bind() : function (target) {
|
9 | for (var i = 1; i < arguments.length; i++) {
|
10 | var source = arguments[i];
|
11 |
|
12 | for (var key in source) {
|
13 | if (Object.prototype.hasOwnProperty.call(source, key)) {
|
14 | target[key] = source[key];
|
15 | }
|
16 | }
|
17 | }
|
18 |
|
19 | return target;
|
20 | };
|
21 | return _extends.apply(this, arguments);
|
22 | }
|
23 |
|
24 | function _unsupportedIterableToArray(o, minLen) {
|
25 | if (!o) return;
|
26 | if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
27 | var n = Object.prototype.toString.call(o).slice(8, -1);
|
28 | if (n === "Object" && o.constructor) n = o.constructor.name;
|
29 | if (n === "Map" || n === "Set") return Array.from(o);
|
30 | if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
31 | }
|
32 |
|
33 | function _arrayLikeToArray(arr, len) {
|
34 | if (len == null || len > arr.length) len = arr.length;
|
35 |
|
36 | for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
37 |
|
38 | return arr2;
|
39 | }
|
40 |
|
41 | function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
42 | var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
43 | if (it) return (it = it.call(o)).next.bind(it);
|
44 |
|
45 | if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
46 | if (it) o = it;
|
47 | var i = 0;
|
48 | return function () {
|
49 | if (i >= o.length) return {
|
50 | done: true
|
51 | };
|
52 | return {
|
53 | done: false,
|
54 | value: o[i++]
|
55 | };
|
56 | };
|
57 | }
|
58 |
|
59 | throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
60 | }
|
61 |
|
62 | var SUSPENSE_PROMISE = Symbol();
|
63 | var isSuspensePromise = function isSuspensePromise(promise) {
|
64 | return !!promise[SUSPENSE_PROMISE];
|
65 | };
|
66 | var isSuspensePromiseAlreadyCancelled = function isSuspensePromiseAlreadyCancelled(suspensePromise) {
|
67 | return !suspensePromise[SUSPENSE_PROMISE].c;
|
68 | };
|
69 | var cancelSuspensePromise = function cancelSuspensePromise(suspensePromise) {
|
70 | var _suspensePromise$SUSP = suspensePromise[SUSPENSE_PROMISE],
|
71 | basePromise = _suspensePromise$SUSP.b,
|
72 | cancelPromise = _suspensePromise$SUSP.c;
|
73 |
|
74 | if (cancelPromise) {
|
75 | var _promiseAbortMap$get;
|
76 |
|
77 | cancelPromise();
|
78 | (_promiseAbortMap$get = promiseAbortMap.get(basePromise)) == null ? void 0 : _promiseAbortMap$get();
|
79 | }
|
80 | };
|
81 | var isEqualSuspensePromise = function isEqualSuspensePromise(oldSuspensePromise, newSuspensePromise) {
|
82 | var oldOriginalPromise = oldSuspensePromise[SUSPENSE_PROMISE].o;
|
83 | var newOriginalPromise = newSuspensePromise[SUSPENSE_PROMISE].o;
|
84 | return oldOriginalPromise === newOriginalPromise || oldSuspensePromise === newOriginalPromise || isSuspensePromise(oldOriginalPromise) && isEqualSuspensePromise(oldOriginalPromise, newSuspensePromise);
|
85 | };
|
86 | var createSuspensePromise = function createSuspensePromise(basePromise, promise) {
|
87 | var suspensePromiseExtra = {
|
88 | b: basePromise,
|
89 | o: promise,
|
90 | c: null
|
91 | };
|
92 | var suspensePromise = new Promise(function (resolve) {
|
93 | suspensePromiseExtra.c = function () {
|
94 | suspensePromiseExtra.c = null;
|
95 | resolve();
|
96 | };
|
97 |
|
98 | promise.finally(suspensePromiseExtra.c);
|
99 | });
|
100 | suspensePromise[SUSPENSE_PROMISE] = suspensePromiseExtra;
|
101 | return suspensePromise;
|
102 | };
|
103 | var promiseAbortMap = new WeakMap();
|
104 | var registerPromiseAbort = function registerPromiseAbort(basePromise, abort) {
|
105 | promiseAbortMap.set(basePromise, abort);
|
106 | };
|
107 |
|
108 | var hasInitialValue = function hasInitialValue(atom) {
|
109 | return 'init' in atom;
|
110 | };
|
111 |
|
112 | var READ_ATOM = 'r';
|
113 | var WRITE_ATOM = 'w';
|
114 | var COMMIT_ATOM = 'c';
|
115 | var SUBSCRIBE_ATOM = 's';
|
116 | var RESTORE_ATOMS = 'h';
|
117 | var DEV_SUBSCRIBE_STATE = 'n';
|
118 | var DEV_GET_MOUNTED_ATOMS = 'l';
|
119 | var DEV_GET_ATOM_STATE = 'a';
|
120 | var DEV_GET_MOUNTED = 'm';
|
121 | var createStore = function createStore(initialValues) {
|
122 | var _ref4;
|
123 |
|
124 | var committedAtomStateMap = new WeakMap();
|
125 | var mountedMap = new WeakMap();
|
126 | var pendingMap = new Map();
|
127 | var stateListeners;
|
128 | var mountedAtoms;
|
129 |
|
130 | if (process.env.NODE_ENV !== "production") {
|
131 | stateListeners = new Set();
|
132 | mountedAtoms = new Set();
|
133 | }
|
134 |
|
135 | if (initialValues) {
|
136 | for (var _iterator = _createForOfIteratorHelperLoose(initialValues), _step; !(_step = _iterator()).done;) {
|
137 | var _step$value = _step.value,
|
138 | atom = _step$value[0],
|
139 | value = _step$value[1];
|
140 | var atomState = {
|
141 | v: value,
|
142 | r: 0,
|
143 | y: true,
|
144 | d: new Map()
|
145 | };
|
146 |
|
147 | if (process.env.NODE_ENV !== "production") {
|
148 | Object.freeze(atomState);
|
149 |
|
150 | if (!hasInitialValue(atom)) {
|
151 | console.warn('Found initial value for derived atom which can cause unexpected behavior', atom);
|
152 | }
|
153 | }
|
154 |
|
155 | committedAtomStateMap.set(atom, atomState);
|
156 | }
|
157 | }
|
158 |
|
159 | var suspensePromiseCacheMap = new WeakMap();
|
160 |
|
161 | var addSuspensePromiseToCache = function addSuspensePromiseToCache(version, atom, suspensePromise) {
|
162 | var cache = suspensePromiseCacheMap.get(atom);
|
163 |
|
164 | if (!cache) {
|
165 | cache = new Map();
|
166 | suspensePromiseCacheMap.set(atom, cache);
|
167 | }
|
168 |
|
169 | suspensePromise.then(function () {
|
170 | if (cache.get(version) === suspensePromise) {
|
171 | cache.delete(version);
|
172 |
|
173 | if (!cache.size) {
|
174 | suspensePromiseCacheMap.delete(atom);
|
175 | }
|
176 | }
|
177 | });
|
178 | cache.set(version, suspensePromise);
|
179 | };
|
180 |
|
181 | var cancelAllSuspensePromiseInCache = function cancelAllSuspensePromiseInCache(atom) {
|
182 | var versionSet = new Set();
|
183 | var cache = suspensePromiseCacheMap.get(atom);
|
184 |
|
185 | if (cache) {
|
186 | suspensePromiseCacheMap.delete(atom);
|
187 | cache.forEach(function (suspensePromise, version) {
|
188 | cancelSuspensePromise(suspensePromise);
|
189 | versionSet.add(version);
|
190 | });
|
191 | }
|
192 |
|
193 | return versionSet;
|
194 | };
|
195 |
|
196 | var versionedAtomStateMapMap = new WeakMap();
|
197 |
|
198 | var getVersionedAtomStateMap = function getVersionedAtomStateMap(version) {
|
199 | var versionedAtomStateMap = versionedAtomStateMapMap.get(version);
|
200 |
|
201 | if (!versionedAtomStateMap) {
|
202 | versionedAtomStateMap = new Map();
|
203 | versionedAtomStateMapMap.set(version, versionedAtomStateMap);
|
204 | }
|
205 |
|
206 | return versionedAtomStateMap;
|
207 | };
|
208 |
|
209 | var getAtomState = function getAtomState(version, atom) {
|
210 | if (version) {
|
211 | var versionedAtomStateMap = getVersionedAtomStateMap(version);
|
212 |
|
213 | var _atomState = versionedAtomStateMap.get(atom);
|
214 |
|
215 | if (!_atomState) {
|
216 | _atomState = getAtomState(version.p, atom);
|
217 |
|
218 | if (_atomState) {
|
219 | versionedAtomStateMap.set(atom, _atomState);
|
220 | }
|
221 | }
|
222 |
|
223 | return _atomState;
|
224 | }
|
225 |
|
226 | return committedAtomStateMap.get(atom);
|
227 | };
|
228 |
|
229 | var setAtomState = function setAtomState(version, atom, atomState) {
|
230 | if (process.env.NODE_ENV !== "production") {
|
231 | Object.freeze(atomState);
|
232 | }
|
233 |
|
234 | if (version) {
|
235 | var versionedAtomStateMap = getVersionedAtomStateMap(version);
|
236 | versionedAtomStateMap.set(atom, atomState);
|
237 | } else {
|
238 | var prevAtomState = committedAtomStateMap.get(atom);
|
239 | committedAtomStateMap.set(atom, atomState);
|
240 |
|
241 | if (!pendingMap.has(atom)) {
|
242 | pendingMap.set(atom, prevAtomState);
|
243 | }
|
244 | }
|
245 | };
|
246 |
|
247 | var createReadDependencies = function createReadDependencies(version, prevReadDependencies, dependencies) {
|
248 | if (prevReadDependencies === void 0) {
|
249 | prevReadDependencies = new Map();
|
250 | }
|
251 |
|
252 | if (!dependencies) {
|
253 | return prevReadDependencies;
|
254 | }
|
255 |
|
256 | var readDependencies = new Map();
|
257 | var changed = false;
|
258 | dependencies.forEach(function (atom) {
|
259 | var _getAtomState;
|
260 |
|
261 | var revision = ((_getAtomState = getAtomState(version, atom)) == null ? void 0 : _getAtomState.r) || 0;
|
262 | readDependencies.set(atom, revision);
|
263 |
|
264 | if (prevReadDependencies.get(atom) !== revision) {
|
265 | changed = true;
|
266 | }
|
267 | });
|
268 |
|
269 | if (prevReadDependencies.size === readDependencies.size && !changed) {
|
270 | return prevReadDependencies;
|
271 | }
|
272 |
|
273 | return readDependencies;
|
274 | };
|
275 |
|
276 | var setAtomValue = function setAtomValue(version, atom, value, dependencies, suspensePromise) {
|
277 | var atomState = getAtomState(version, atom);
|
278 |
|
279 | if (atomState) {
|
280 | if (suspensePromise && (!('p' in atomState) || !isEqualSuspensePromise(atomState.p, suspensePromise))) {
|
281 | return atomState;
|
282 | }
|
283 |
|
284 | if ('p' in atomState) {
|
285 | cancelSuspensePromise(atomState.p);
|
286 | }
|
287 | }
|
288 |
|
289 | var nextAtomState = {
|
290 | v: value,
|
291 | r: (atomState == null ? void 0 : atomState.r) || 0,
|
292 | y: true,
|
293 | d: createReadDependencies(version, atomState == null ? void 0 : atomState.d, dependencies)
|
294 | };
|
295 | var changed = !(atomState != null && atomState.y);
|
296 |
|
297 | if (!atomState || !('v' in atomState) || !Object.is(atomState.v, value)) {
|
298 | changed = true;
|
299 | ++nextAtomState.r;
|
300 |
|
301 | if (nextAtomState.d.has(atom)) {
|
302 | nextAtomState.d = new Map(nextAtomState.d).set(atom, nextAtomState.r);
|
303 | }
|
304 | } else if (nextAtomState.d !== atomState.d && (nextAtomState.d.size !== atomState.d.size || !Array.from(nextAtomState.d.keys()).every(function (a) {
|
305 | return atomState.d.has(a);
|
306 | }))) {
|
307 | changed = true;
|
308 | Promise.resolve().then(function () {
|
309 | flushPending(version);
|
310 | });
|
311 | }
|
312 |
|
313 | if (atomState && !changed) {
|
314 | return atomState;
|
315 | }
|
316 |
|
317 | setAtomState(version, atom, nextAtomState);
|
318 | return nextAtomState;
|
319 | };
|
320 |
|
321 | var setAtomReadError = function setAtomReadError(version, atom, error, dependencies, suspensePromise) {
|
322 | var atomState = getAtomState(version, atom);
|
323 |
|
324 | if (atomState) {
|
325 | if (suspensePromise && (!('p' in atomState) || !isEqualSuspensePromise(atomState.p, suspensePromise))) {
|
326 | return atomState;
|
327 | }
|
328 |
|
329 | if ('p' in atomState) {
|
330 | cancelSuspensePromise(atomState.p);
|
331 | }
|
332 | }
|
333 |
|
334 | var nextAtomState = {
|
335 | e: error,
|
336 | r: ((atomState == null ? void 0 : atomState.r) || 0) + 1,
|
337 | y: true,
|
338 | d: createReadDependencies(version, atomState == null ? void 0 : atomState.d, dependencies)
|
339 | };
|
340 | setAtomState(version, atom, nextAtomState);
|
341 | return nextAtomState;
|
342 | };
|
343 |
|
344 | var setAtomSuspensePromise = function setAtomSuspensePromise(version, atom, suspensePromise, dependencies) {
|
345 | var atomState = getAtomState(version, atom);
|
346 |
|
347 | if (atomState && 'p' in atomState) {
|
348 | if (isEqualSuspensePromise(atomState.p, suspensePromise)) {
|
349 | if (!atomState.y) {
|
350 | return _extends({}, atomState, {
|
351 | y: true
|
352 | });
|
353 | }
|
354 |
|
355 | return atomState;
|
356 | }
|
357 |
|
358 | cancelSuspensePromise(atomState.p);
|
359 | }
|
360 |
|
361 | addSuspensePromiseToCache(version, atom, suspensePromise);
|
362 | var nextAtomState = {
|
363 | p: suspensePromise,
|
364 | r: ((atomState == null ? void 0 : atomState.r) || 0) + 1,
|
365 | y: true,
|
366 | d: createReadDependencies(version, atomState == null ? void 0 : atomState.d, dependencies)
|
367 | };
|
368 | setAtomState(version, atom, nextAtomState);
|
369 | return nextAtomState;
|
370 | };
|
371 |
|
372 | var setAtomPromiseOrValue = function setAtomPromiseOrValue(version, atom, promiseOrValue, dependencies) {
|
373 | if (promiseOrValue instanceof Promise) {
|
374 | var suspensePromise = createSuspensePromise(promiseOrValue, promiseOrValue.then(function (value) {
|
375 | setAtomValue(version, atom, value, dependencies, suspensePromise);
|
376 | }).catch(function (e) {
|
377 | if (e instanceof Promise) {
|
378 | if (isSuspensePromise(e)) {
|
379 | return e.then(function () {
|
380 | readAtomState(version, atom, true);
|
381 | });
|
382 | }
|
383 |
|
384 | return e;
|
385 | }
|
386 |
|
387 | setAtomReadError(version, atom, e, dependencies, suspensePromise);
|
388 | }));
|
389 | return setAtomSuspensePromise(version, atom, suspensePromise, dependencies);
|
390 | }
|
391 |
|
392 | return setAtomValue(version, atom, promiseOrValue, dependencies);
|
393 | };
|
394 |
|
395 | var setAtomInvalidated = function setAtomInvalidated(version, atom) {
|
396 | var atomState = getAtomState(version, atom);
|
397 |
|
398 | if (atomState) {
|
399 | var nextAtomState = _extends({}, atomState, {
|
400 | y: false
|
401 | });
|
402 |
|
403 | setAtomState(version, atom, nextAtomState);
|
404 | } else if (process.env.NODE_ENV !== "production") {
|
405 | console.warn('[Bug] could not invalidate non existing atom', atom);
|
406 | }
|
407 | };
|
408 |
|
409 | var readAtomState = function readAtomState(version, atom, force) {
|
410 | if (!force) {
|
411 | var _atomState2 = getAtomState(version, atom);
|
412 |
|
413 | if (_atomState2) {
|
414 | if (_atomState2.y && 'p' in _atomState2 && !isSuspensePromiseAlreadyCancelled(_atomState2.p)) {
|
415 | return _atomState2;
|
416 | }
|
417 |
|
418 | _atomState2.d.forEach(function (_, a) {
|
419 | if (a !== atom) {
|
420 | if (!mountedMap.has(a)) {
|
421 | readAtomState(version, a);
|
422 | } else {
|
423 | var aState = getAtomState(version, a);
|
424 |
|
425 | if (aState && !aState.y) {
|
426 | readAtomState(version, a);
|
427 | }
|
428 | }
|
429 | }
|
430 | });
|
431 |
|
432 | if (Array.from(_atomState2.d).every(function (_ref) {
|
433 | var a = _ref[0],
|
434 | r = _ref[1];
|
435 | var aState = getAtomState(version, a);
|
436 | return aState && !('p' in aState) && aState.r === r;
|
437 | })) {
|
438 | if (!_atomState2.y) {
|
439 | return _extends({}, _atomState2, {
|
440 | y: true
|
441 | });
|
442 | }
|
443 |
|
444 | return _atomState2;
|
445 | }
|
446 | }
|
447 | }
|
448 |
|
449 | var dependencies = new Set();
|
450 |
|
451 | try {
|
452 | var promiseOrValue = atom.read(function (a) {
|
453 | dependencies.add(a);
|
454 | var aState = a === atom ? getAtomState(version, a) : readAtomState(version, a);
|
455 |
|
456 | if (aState) {
|
457 | if ('e' in aState) {
|
458 | throw aState.e;
|
459 | }
|
460 |
|
461 | if ('p' in aState) {
|
462 | throw aState.p;
|
463 | }
|
464 |
|
465 | return aState.v;
|
466 | }
|
467 |
|
468 | if (hasInitialValue(a)) {
|
469 | return a.init;
|
470 | }
|
471 |
|
472 | throw new Error('no atom init');
|
473 | });
|
474 | return setAtomPromiseOrValue(version, atom, promiseOrValue, dependencies);
|
475 | } catch (errorOrPromise) {
|
476 | if (errorOrPromise instanceof Promise) {
|
477 | var suspensePromise = createSuspensePromise(errorOrPromise, errorOrPromise);
|
478 | return setAtomSuspensePromise(version, atom, suspensePromise, dependencies);
|
479 | }
|
480 |
|
481 | return setAtomReadError(version, atom, errorOrPromise, dependencies);
|
482 | }
|
483 | };
|
484 |
|
485 | var readAtom = function readAtom(readingAtom, version) {
|
486 | var atomState = readAtomState(version, readingAtom);
|
487 | return atomState;
|
488 | };
|
489 |
|
490 | var addAtom = function addAtom(version, addingAtom) {
|
491 | var mounted = mountedMap.get(addingAtom);
|
492 |
|
493 | if (!mounted) {
|
494 | mounted = mountAtom(version, addingAtom);
|
495 | }
|
496 |
|
497 | return mounted;
|
498 | };
|
499 |
|
500 | var canUnmountAtom = function canUnmountAtom(atom, mounted) {
|
501 | return !mounted.l.size && (!mounted.t.size || mounted.t.size === 1 && mounted.t.has(atom));
|
502 | };
|
503 |
|
504 | var delAtom = function delAtom(version, deletingAtom) {
|
505 | var mounted = mountedMap.get(deletingAtom);
|
506 |
|
507 | if (mounted && canUnmountAtom(deletingAtom, mounted)) {
|
508 | unmountAtom(version, deletingAtom);
|
509 | }
|
510 | };
|
511 |
|
512 | var invalidateDependents = function invalidateDependents(version, atom) {
|
513 | var mounted = mountedMap.get(atom);
|
514 | mounted == null ? void 0 : mounted.t.forEach(function (dependent) {
|
515 | if (dependent !== atom) {
|
516 | setAtomInvalidated(version, dependent);
|
517 | invalidateDependents(version, dependent);
|
518 | }
|
519 | });
|
520 | };
|
521 |
|
522 | var writeAtomState = function writeAtomState(version, atom, update) {
|
523 | var isSync = true;
|
524 |
|
525 | var writeGetter = function writeGetter(a, options) {
|
526 | var aState = readAtomState(version, a);
|
527 |
|
528 | if ('e' in aState) {
|
529 | throw aState.e;
|
530 | }
|
531 |
|
532 | if ('p' in aState) {
|
533 | if (options != null && options.unstable_promise) {
|
534 | return aState.p.then(function () {
|
535 | var s = getAtomState(version, a);
|
536 |
|
537 | if (s && 'p' in s && s.p === aState.p) {
|
538 | return new Promise(function (resolve) {
|
539 | return setTimeout(resolve);
|
540 | }).then(function () {
|
541 | return writeGetter(a, options);
|
542 | });
|
543 | }
|
544 |
|
545 | return writeGetter(a, options);
|
546 | });
|
547 | }
|
548 |
|
549 | if (process.env.NODE_ENV !== "production") {
|
550 | console.info('Reading pending atom state in write operation. We throw a promise for now.', a);
|
551 | }
|
552 |
|
553 | throw aState.p;
|
554 | }
|
555 |
|
556 | if ('v' in aState) {
|
557 | return aState.v;
|
558 | }
|
559 |
|
560 | if (process.env.NODE_ENV !== "production") {
|
561 | console.warn('[Bug] no value found while reading atom in write operation. This is probably a bug.', a);
|
562 | }
|
563 |
|
564 | throw new Error('no value found');
|
565 | };
|
566 |
|
567 | var setter = function setter(a, v) {
|
568 | var promiseOrVoid;
|
569 |
|
570 | if (a === atom) {
|
571 | if (!hasInitialValue(a)) {
|
572 | throw new Error('atom not writable');
|
573 | }
|
574 |
|
575 | var versionSet = cancelAllSuspensePromiseInCache(a);
|
576 | versionSet.forEach(function (cancelledVersion) {
|
577 | if (cancelledVersion !== version) {
|
578 | setAtomPromiseOrValue(cancelledVersion, a, v);
|
579 | }
|
580 | });
|
581 | var prevAtomState = getAtomState(version, a);
|
582 | var nextAtomState = setAtomPromiseOrValue(version, a, v);
|
583 |
|
584 | if (prevAtomState !== nextAtomState) {
|
585 | invalidateDependents(version, a);
|
586 | }
|
587 | } else {
|
588 | promiseOrVoid = writeAtomState(version, a, v);
|
589 | }
|
590 |
|
591 | if (!isSync) {
|
592 | flushPending(version);
|
593 | }
|
594 |
|
595 | return promiseOrVoid;
|
596 | };
|
597 |
|
598 | var promiseOrVoid = atom.write(writeGetter, setter, update);
|
599 | isSync = false;
|
600 | return promiseOrVoid;
|
601 | };
|
602 |
|
603 | var writeAtom = function writeAtom(writingAtom, update, version) {
|
604 | var promiseOrVoid = writeAtomState(version, writingAtom, update);
|
605 | flushPending(version);
|
606 | return promiseOrVoid;
|
607 | };
|
608 |
|
609 | var isActuallyWritableAtom = function isActuallyWritableAtom(atom) {
|
610 | return !!atom.write;
|
611 | };
|
612 |
|
613 | var mountAtom = function mountAtom(version, atom, initialDependent) {
|
614 | var mounted = {
|
615 | t: new Set(initialDependent && [initialDependent]),
|
616 | l: new Set()
|
617 | };
|
618 | mountedMap.set(atom, mounted);
|
619 |
|
620 | if (process.env.NODE_ENV !== "production") {
|
621 | mountedAtoms.add(atom);
|
622 | }
|
623 |
|
624 | var atomState = readAtomState(undefined, atom);
|
625 | atomState.d.forEach(function (_, a) {
|
626 | var aMounted = mountedMap.get(a);
|
627 |
|
628 | if (aMounted) {
|
629 | aMounted.t.add(atom);
|
630 | } else {
|
631 | if (a !== atom) {
|
632 | mountAtom(version, a, atom);
|
633 | }
|
634 | }
|
635 | });
|
636 |
|
637 | if (isActuallyWritableAtom(atom) && atom.onMount) {
|
638 | var setAtom = function setAtom(update) {
|
639 | return writeAtom(atom, update, version);
|
640 | };
|
641 |
|
642 | var onUnmount = atom.onMount(setAtom);
|
643 | version = undefined;
|
644 |
|
645 | if (onUnmount) {
|
646 | mounted.u = onUnmount;
|
647 | }
|
648 | }
|
649 |
|
650 | return mounted;
|
651 | };
|
652 |
|
653 | var unmountAtom = function unmountAtom(version, atom) {
|
654 | var _mountedMap$get;
|
655 |
|
656 | var onUnmount = (_mountedMap$get = mountedMap.get(atom)) == null ? void 0 : _mountedMap$get.u;
|
657 |
|
658 | if (onUnmount) {
|
659 | onUnmount();
|
660 | }
|
661 |
|
662 | mountedMap.delete(atom);
|
663 |
|
664 | if (process.env.NODE_ENV !== "production") {
|
665 | mountedAtoms.delete(atom);
|
666 | }
|
667 |
|
668 | var atomState = getAtomState(version, atom);
|
669 |
|
670 | if (atomState) {
|
671 | if ('p' in atomState) {
|
672 | cancelSuspensePromise(atomState.p);
|
673 | }
|
674 |
|
675 | atomState.d.forEach(function (_, a) {
|
676 | if (a !== atom) {
|
677 | var mounted = mountedMap.get(a);
|
678 |
|
679 | if (mounted) {
|
680 | mounted.t.delete(atom);
|
681 |
|
682 | if (canUnmountAtom(a, mounted)) {
|
683 | unmountAtom(version, a);
|
684 | }
|
685 | }
|
686 | }
|
687 | });
|
688 | } else if (process.env.NODE_ENV !== "production") {
|
689 | console.warn('[Bug] could not find atom state to unmount', atom);
|
690 | }
|
691 | };
|
692 |
|
693 | var mountDependencies = function mountDependencies(version, atom, atomState, prevReadDependencies) {
|
694 | var dependencies = new Set(atomState.d.keys());
|
695 | prevReadDependencies == null ? void 0 : prevReadDependencies.forEach(function (_, a) {
|
696 | if (dependencies.has(a)) {
|
697 | dependencies.delete(a);
|
698 | return;
|
699 | }
|
700 |
|
701 | var mounted = mountedMap.get(a);
|
702 |
|
703 | if (mounted) {
|
704 | mounted.t.delete(atom);
|
705 |
|
706 | if (canUnmountAtom(a, mounted)) {
|
707 | unmountAtom(version, a);
|
708 | }
|
709 | }
|
710 | });
|
711 | dependencies.forEach(function (a) {
|
712 | var mounted = mountedMap.get(a);
|
713 |
|
714 | if (mounted) {
|
715 | mounted.t.add(atom);
|
716 | } else if (mountedMap.has(atom)) {
|
717 | mountAtom(version, a, atom);
|
718 | }
|
719 | });
|
720 | };
|
721 |
|
722 | var flushPending = function flushPending(version) {
|
723 | if (version) {
|
724 | var versionedAtomStateMap = getVersionedAtomStateMap(version);
|
725 | versionedAtomStateMap.forEach(function (atomState, atom) {
|
726 | var committedAtomState = committedAtomStateMap.get(atom);
|
727 |
|
728 | if (atomState !== committedAtomState) {
|
729 | var mounted = mountedMap.get(atom);
|
730 | mounted == null ? void 0 : mounted.l.forEach(function (listener) {
|
731 | return listener(version);
|
732 | });
|
733 | }
|
734 | });
|
735 | return;
|
736 | }
|
737 |
|
738 | while (pendingMap.size) {
|
739 | var pending = Array.from(pendingMap);
|
740 | pendingMap.clear();
|
741 | pending.forEach(function (_ref2) {
|
742 | var atom = _ref2[0],
|
743 | prevAtomState = _ref2[1];
|
744 | var atomState = getAtomState(undefined, atom);
|
745 |
|
746 | if (atomState && atomState.d !== (prevAtomState == null ? void 0 : prevAtomState.d)) {
|
747 | mountDependencies(undefined, atom, atomState, prevAtomState == null ? void 0 : prevAtomState.d);
|
748 | }
|
749 |
|
750 | if (prevAtomState && !prevAtomState.y && atomState != null && atomState.y) {
|
751 | return;
|
752 | }
|
753 |
|
754 | var mounted = mountedMap.get(atom);
|
755 | mounted == null ? void 0 : mounted.l.forEach(function (listener) {
|
756 | return listener();
|
757 | });
|
758 | });
|
759 | }
|
760 |
|
761 | if (process.env.NODE_ENV !== "production") {
|
762 | stateListeners.forEach(function (l) {
|
763 | return l();
|
764 | });
|
765 | }
|
766 | };
|
767 |
|
768 | var commitVersionedAtomStateMap = function commitVersionedAtomStateMap(version) {
|
769 | var versionedAtomStateMap = getVersionedAtomStateMap(version);
|
770 | versionedAtomStateMap.forEach(function (atomState, atom) {
|
771 | var prevAtomState = committedAtomStateMap.get(atom);
|
772 |
|
773 | if (!prevAtomState || atomState.r > prevAtomState.r || atomState.y !== prevAtomState.y || atomState.r === prevAtomState.r && atomState.d !== prevAtomState.d) {
|
774 | committedAtomStateMap.set(atom, atomState);
|
775 |
|
776 | if (atomState.d !== (prevAtomState == null ? void 0 : prevAtomState.d)) {
|
777 | mountDependencies(version, atom, atomState, prevAtomState == null ? void 0 : prevAtomState.d);
|
778 | }
|
779 | }
|
780 | });
|
781 | };
|
782 |
|
783 | var commitAtom = function commitAtom(_atom, version) {
|
784 | if (version) {
|
785 | commitVersionedAtomStateMap(version);
|
786 | }
|
787 |
|
788 | flushPending(undefined);
|
789 | };
|
790 |
|
791 | var subscribeAtom = function subscribeAtom(atom, callback, version) {
|
792 | var mounted = addAtom(version, atom);
|
793 | var listeners = mounted.l;
|
794 | listeners.add(callback);
|
795 | return function () {
|
796 | listeners.delete(callback);
|
797 | delAtom(version, atom);
|
798 | };
|
799 | };
|
800 |
|
801 | var restoreAtoms = function restoreAtoms(values, version) {
|
802 | for (var _iterator2 = _createForOfIteratorHelperLoose(values), _step2; !(_step2 = _iterator2()).done;) {
|
803 | var _step2$value = _step2.value,
|
804 | _atom2 = _step2$value[0],
|
805 | _value = _step2$value[1];
|
806 |
|
807 | if (hasInitialValue(_atom2)) {
|
808 | setAtomPromiseOrValue(version, _atom2, _value);
|
809 | invalidateDependents(version, _atom2);
|
810 | }
|
811 | }
|
812 |
|
813 | flushPending(version);
|
814 | };
|
815 |
|
816 | if (process.env.NODE_ENV !== "production") {
|
817 | var _ref3;
|
818 |
|
819 | return _ref3 = {}, _ref3[READ_ATOM] = readAtom, _ref3[WRITE_ATOM] = writeAtom, _ref3[COMMIT_ATOM] = commitAtom, _ref3[SUBSCRIBE_ATOM] = subscribeAtom, _ref3[RESTORE_ATOMS] = restoreAtoms, _ref3[DEV_SUBSCRIBE_STATE] = function (l) {
|
820 | stateListeners.add(l);
|
821 | return function () {
|
822 | stateListeners.delete(l);
|
823 | };
|
824 | }, _ref3[DEV_GET_MOUNTED_ATOMS] = function () {
|
825 | return mountedAtoms.values();
|
826 | }, _ref3[DEV_GET_ATOM_STATE] = function (a) {
|
827 | return committedAtomStateMap.get(a);
|
828 | }, _ref3[DEV_GET_MOUNTED] = function (a) {
|
829 | return mountedMap.get(a);
|
830 | }, _ref3;
|
831 | }
|
832 |
|
833 | return _ref4 = {}, _ref4[READ_ATOM] = readAtom, _ref4[WRITE_ATOM] = writeAtom, _ref4[COMMIT_ATOM] = commitAtom, _ref4[SUBSCRIBE_ATOM] = subscribeAtom, _ref4[RESTORE_ATOMS] = restoreAtoms, _ref4;
|
834 | };
|
835 | var createStoreForExport = function createStoreForExport(initialValues) {
|
836 | var store = createStore(initialValues);
|
837 |
|
838 | var get = function get(atom) {
|
839 | var atomState = store[READ_ATOM](atom);
|
840 |
|
841 | if ('e' in atomState) {
|
842 | throw atomState.e;
|
843 | }
|
844 |
|
845 | if ('p' in atomState) {
|
846 | return undefined;
|
847 | }
|
848 |
|
849 | return atomState.v;
|
850 | };
|
851 |
|
852 | var asyncGet = function asyncGet(atom) {
|
853 | return new Promise(function (resolve, reject) {
|
854 | var atomState = store[READ_ATOM](atom);
|
855 |
|
856 | if ('e' in atomState) {
|
857 | reject(atomState.e);
|
858 | } else if ('p' in atomState) {
|
859 | resolve(atomState.p.then(function () {
|
860 | return asyncGet(atom);
|
861 | }));
|
862 | } else {
|
863 | resolve(atomState.v);
|
864 | }
|
865 | });
|
866 | };
|
867 |
|
868 | var set = function set(atom, update) {
|
869 | return store[WRITE_ATOM](atom, update);
|
870 | };
|
871 |
|
872 | var sub = function sub(atom, callback) {
|
873 | return store[SUBSCRIBE_ATOM](atom, callback);
|
874 | };
|
875 |
|
876 | return {
|
877 | get: get,
|
878 | asyncGet: asyncGet,
|
879 | set: set,
|
880 | sub: sub,
|
881 | SECRET_INTERNAL_store: store
|
882 | };
|
883 | };
|
884 |
|
885 | var createScopeContainer = function createScopeContainer(initialValues, unstable_createStore) {
|
886 | var store = unstable_createStore ? unstable_createStore(initialValues).SECRET_INTERNAL_store : createStore(initialValues);
|
887 | return {
|
888 | s: store
|
889 | };
|
890 | };
|
891 | var ScopeContextMap = new Map();
|
892 | var getScopeContext = function getScopeContext(scope) {
|
893 | if (!ScopeContextMap.has(scope)) {
|
894 | ScopeContextMap.set(scope, react.createContext(createScopeContainer()));
|
895 | }
|
896 |
|
897 | return ScopeContextMap.get(scope);
|
898 | };
|
899 |
|
900 | var Provider = function Provider(_ref) {
|
901 | var children = _ref.children,
|
902 | initialValues = _ref.initialValues,
|
903 | scope = _ref.scope,
|
904 | unstable_createStore = _ref.unstable_createStore,
|
905 | unstable_enableVersionedWrite = _ref.unstable_enableVersionedWrite;
|
906 |
|
907 | var _useState = react.useState({}),
|
908 | version = _useState[0],
|
909 | setVersion = _useState[1];
|
910 |
|
911 | react.useEffect(function () {
|
912 | var scopeContainer = scopeContainerRef.current;
|
913 |
|
914 | if (scopeContainer.w) {
|
915 | scopeContainer.s[COMMIT_ATOM](null, version);
|
916 | delete version.p;
|
917 | scopeContainer.v = version;
|
918 | }
|
919 | }, [version]);
|
920 | var scopeContainerRef = react.useRef();
|
921 |
|
922 | if (!scopeContainerRef.current) {
|
923 | var scopeContainer = createScopeContainer(initialValues, unstable_createStore);
|
924 |
|
925 | if (unstable_enableVersionedWrite) {
|
926 | var retrying = 0;
|
927 |
|
928 | scopeContainer.w = function (write) {
|
929 | setVersion(function (parentVersion) {
|
930 | var nextVersion = retrying ? parentVersion : {
|
931 | p: parentVersion
|
932 | };
|
933 | write(nextVersion);
|
934 | return nextVersion;
|
935 | });
|
936 | };
|
937 |
|
938 | scopeContainer.v = version;
|
939 |
|
940 | scopeContainer.r = function (fn) {
|
941 | ++retrying;
|
942 | fn();
|
943 | --retrying;
|
944 | };
|
945 | }
|
946 |
|
947 | scopeContainerRef.current = scopeContainer;
|
948 | }
|
949 |
|
950 | var ScopeContainerContext = getScopeContext(scope);
|
951 | return react.createElement(ScopeContainerContext.Provider, {
|
952 | value: scopeContainerRef.current
|
953 | }, children);
|
954 | };
|
955 |
|
956 | var keyCount = 0;
|
957 | function atom(read, write) {
|
958 | var key = "atom" + ++keyCount;
|
959 | var config = {
|
960 | toString: function toString() {
|
961 | return key;
|
962 | }
|
963 | };
|
964 |
|
965 | if (typeof read === 'function') {
|
966 | config.read = read;
|
967 | } else {
|
968 | config.init = read;
|
969 |
|
970 | config.read = function (get) {
|
971 | return get(config);
|
972 | };
|
973 |
|
974 | config.write = function (get, set, update) {
|
975 | return set(config, typeof update === 'function' ? update(get(config)) : update);
|
976 | };
|
977 | }
|
978 |
|
979 | if (write) {
|
980 | config.write = write;
|
981 | }
|
982 |
|
983 | return config;
|
984 | }
|
985 |
|
986 | function useAtomValue(atom, scope) {
|
987 | var ScopeContext = getScopeContext(scope);
|
988 | var scopeContainer = react.useContext(ScopeContext);
|
989 | var store = scopeContainer.s,
|
990 | versionFromProvider = scopeContainer.v;
|
991 |
|
992 | var getAtomValue = function getAtomValue(version) {
|
993 | var atomState = store[READ_ATOM](atom, version);
|
994 |
|
995 | if (process.env.NODE_ENV !== "production" && !atomState.y) {
|
996 | throw new Error('should not be invalidated');
|
997 | }
|
998 |
|
999 | if ('e' in atomState) {
|
1000 | throw atomState.e;
|
1001 | }
|
1002 |
|
1003 | if ('p' in atomState) {
|
1004 | throw atomState.p;
|
1005 | }
|
1006 |
|
1007 | if ('v' in atomState) {
|
1008 | return atomState.v;
|
1009 | }
|
1010 |
|
1011 | throw new Error('no atom value');
|
1012 | };
|
1013 |
|
1014 | var _useReducer = react.useReducer(function (prev, nextVersion) {
|
1015 | var nextValue = getAtomValue(nextVersion);
|
1016 |
|
1017 | if (Object.is(prev[1], nextValue) && prev[2] === atom) {
|
1018 | return prev;
|
1019 | }
|
1020 |
|
1021 | return [nextVersion, nextValue, atom];
|
1022 | }, versionFromProvider, function (initialVersion) {
|
1023 | var initialValue = getAtomValue(initialVersion);
|
1024 | return [initialVersion, initialValue, atom];
|
1025 | }),
|
1026 | _useReducer$ = _useReducer[0],
|
1027 | version = _useReducer$[0],
|
1028 | valueFromReducer = _useReducer$[1],
|
1029 | atomFromReducer = _useReducer$[2],
|
1030 | rerenderIfChanged = _useReducer[1];
|
1031 |
|
1032 | var value = valueFromReducer;
|
1033 |
|
1034 | if (atomFromReducer !== atom) {
|
1035 | rerenderIfChanged(version);
|
1036 | value = getAtomValue(version);
|
1037 | }
|
1038 |
|
1039 | react.useEffect(function () {
|
1040 | var versionFromProvider = scopeContainer.v;
|
1041 |
|
1042 | if (versionFromProvider) {
|
1043 | store[COMMIT_ATOM](atom, versionFromProvider);
|
1044 | }
|
1045 |
|
1046 | var unsubscribe = store[SUBSCRIBE_ATOM](atom, rerenderIfChanged, versionFromProvider);
|
1047 | rerenderIfChanged(versionFromProvider);
|
1048 | return unsubscribe;
|
1049 | }, [store, atom, scopeContainer]);
|
1050 | react.useEffect(function () {
|
1051 | store[COMMIT_ATOM](atom, version);
|
1052 | });
|
1053 | react.useDebugValue(value);
|
1054 | return value;
|
1055 | }
|
1056 |
|
1057 | function useSetAtom(atom, scope) {
|
1058 | var ScopeContext = getScopeContext(scope);
|
1059 |
|
1060 | var _useContext = react.useContext(ScopeContext),
|
1061 | store = _useContext.s,
|
1062 | versionedWrite = _useContext.w;
|
1063 |
|
1064 | var setAtom = react.useCallback(function (update) {
|
1065 | if (process.env.NODE_ENV !== "production" && !('write' in atom)) {
|
1066 | throw new Error('not writable atom');
|
1067 | }
|
1068 |
|
1069 | var write = function write(version) {
|
1070 | return store[WRITE_ATOM](atom, update, version);
|
1071 | };
|
1072 |
|
1073 | return versionedWrite ? versionedWrite(write) : write();
|
1074 | }, [store, versionedWrite, atom]);
|
1075 | return setAtom;
|
1076 | }
|
1077 |
|
1078 | function useAtom(atom, scope) {
|
1079 | if ('scope' in atom) {
|
1080 | console.warn('atom.scope is deprecated. Please do useAtom(atom, scope) instead.');
|
1081 | scope = atom.scope;
|
1082 | }
|
1083 |
|
1084 | return [useAtomValue(atom, scope), useSetAtom(atom, scope)];
|
1085 | }
|
1086 |
|
1087 | exports.Provider = Provider;
|
1088 | exports.SECRET_INTERNAL_getScopeContext = getScopeContext;
|
1089 | exports.SECRET_INTERNAL_registerPromiseAbort = registerPromiseAbort;
|
1090 | exports.atom = atom;
|
1091 | exports.unstable_createStore = createStoreForExport;
|
1092 | exports.useAtom = useAtom;
|
1093 | exports.useAtomValue = useAtomValue;
|
1094 | exports.useSetAtom = useSetAtom;
|