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