1 | 'use strict';
|
2 |
|
3 | var keyCount = 0;
|
4 | function atom(read, write) {
|
5 | var key = "atom" + ++keyCount;
|
6 | var config = {
|
7 | toString: function toString() {
|
8 | return key;
|
9 | }
|
10 | };
|
11 | if (typeof read === 'function') {
|
12 | config.read = read;
|
13 | } else {
|
14 | config.init = read;
|
15 | config.read = function (get) {
|
16 | return get(config);
|
17 | };
|
18 | config.write = function (get, set, arg) {
|
19 | return set(config, typeof arg === 'function' ? arg(get(config)) : arg);
|
20 | };
|
21 | }
|
22 | if (write) {
|
23 | config.write = write;
|
24 | }
|
25 | return config;
|
26 | }
|
27 |
|
28 | function _unsupportedIterableToArray(o, minLen) {
|
29 | if (!o) return;
|
30 | if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
31 | var n = Object.prototype.toString.call(o).slice(8, -1);
|
32 | if (n === "Object" && o.constructor) n = o.constructor.name;
|
33 | if (n === "Map" || n === "Set") return Array.from(o);
|
34 | if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
35 | }
|
36 | function _arrayLikeToArray(arr, len) {
|
37 | if (len == null || len > arr.length) len = arr.length;
|
38 | for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
39 | return arr2;
|
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 | if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
45 | if (it) o = it;
|
46 | var i = 0;
|
47 | return function () {
|
48 | if (i >= o.length) return {
|
49 | done: true
|
50 | };
|
51 | return {
|
52 | done: false,
|
53 | value: o[i++]
|
54 | };
|
55 | };
|
56 | }
|
57 | throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
58 | }
|
59 |
|
60 | var hasInitialValue = function hasInitialValue(atom) {
|
61 | return 'init' in atom;
|
62 | };
|
63 | var isActuallyWritableAtom = function isActuallyWritableAtom(atom) {
|
64 | return !!atom.write;
|
65 | };
|
66 | var cancelPromiseMap = new WeakMap();
|
67 | var registerCancelPromise = function registerCancelPromise(promise, cancel) {
|
68 | cancelPromiseMap.set(promise, cancel);
|
69 | promise.catch(function () {}).finally(function () {
|
70 | return cancelPromiseMap.delete(promise);
|
71 | });
|
72 | };
|
73 | var cancelPromise = function cancelPromise(promise, next) {
|
74 | var cancel = cancelPromiseMap.get(promise);
|
75 | if (cancel) {
|
76 | cancelPromiseMap.delete(promise);
|
77 | cancel(next);
|
78 | }
|
79 | };
|
80 | var resolvePromise = function resolvePromise(promise, value) {
|
81 | promise.status = 'fulfilled';
|
82 | promise.value = value;
|
83 | };
|
84 | var rejectPromise = function rejectPromise(promise, e) {
|
85 | promise.status = 'rejected';
|
86 | promise.reason = e;
|
87 | };
|
88 | var isPromiseLike = function isPromiseLike(x) {
|
89 | return typeof (x == null ? void 0 : x.then) === 'function';
|
90 | };
|
91 | var isEqualAtomValue = function isEqualAtomValue(a, b) {
|
92 | return 'v' in a && 'v' in b && Object.is(a.v, b.v);
|
93 | };
|
94 | var isEqualAtomError = function isEqualAtomError(a, b) {
|
95 | return 'e' in a && 'e' in b && Object.is(a.e, b.e);
|
96 | };
|
97 | var hasPromiseAtomValue = function hasPromiseAtomValue(a) {
|
98 | return 'v' in a && a.v instanceof Promise;
|
99 | };
|
100 | var isEqualPromiseAtomValue = function isEqualPromiseAtomValue(a, b) {
|
101 | return 'v' in a && 'v' in b && a.v.orig && a.v.orig === b.v.orig;
|
102 | };
|
103 | var returnAtomValue = function returnAtomValue(atomState) {
|
104 | if ('e' in atomState) {
|
105 | throw atomState.e;
|
106 | }
|
107 | return atomState.v;
|
108 | };
|
109 | var createStore = function createStore() {
|
110 | var atomStateMap = new WeakMap();
|
111 | var mountedMap = new WeakMap();
|
112 | var pendingMap = new Map();
|
113 | var storeListenersRev2;
|
114 | var mountedAtoms;
|
115 | if (process.env.NODE_ENV !== 'production') {
|
116 | storeListenersRev2 = new Set();
|
117 | mountedAtoms = new Set();
|
118 | }
|
119 | var getAtomState = function getAtomState(atom) {
|
120 | return atomStateMap.get(atom);
|
121 | };
|
122 | var setAtomState = function setAtomState(atom, atomState) {
|
123 | if (process.env.NODE_ENV !== 'production') {
|
124 | Object.freeze(atomState);
|
125 | }
|
126 | var prevAtomState = atomStateMap.get(atom);
|
127 | atomStateMap.set(atom, atomState);
|
128 | if (!pendingMap.has(atom)) {
|
129 | pendingMap.set(atom, prevAtomState);
|
130 | }
|
131 | if (prevAtomState && hasPromiseAtomValue(prevAtomState)) {
|
132 | var _next = 'v' in atomState ? atomState.v instanceof Promise ? atomState.v : Promise.resolve(atomState.v) : Promise.reject(atomState.e);
|
133 | if (prevAtomState.v !== _next) {
|
134 | cancelPromise(prevAtomState.v, _next);
|
135 | }
|
136 | }
|
137 | };
|
138 | var updateDependencies = function updateDependencies(atom, nextAtomState, nextDependencies) {
|
139 | var dependencies = new Map();
|
140 | var changed = false;
|
141 | nextDependencies.forEach(function (aState, a) {
|
142 | if (!aState && a === atom) {
|
143 | aState = nextAtomState;
|
144 | }
|
145 | if (aState) {
|
146 | dependencies.set(a, aState);
|
147 | if (nextAtomState.d.get(a) !== aState) {
|
148 | changed = true;
|
149 | }
|
150 | } else if (process.env.NODE_ENV !== 'production') {
|
151 | console.warn('[Bug] atom state not found');
|
152 | }
|
153 | });
|
154 | if (changed || nextAtomState.d.size !== dependencies.size) {
|
155 | nextAtomState.d = dependencies;
|
156 | }
|
157 | };
|
158 | var setAtomValue = function setAtomValue(atom, value, nextDependencies) {
|
159 | var prevAtomState = getAtomState(atom);
|
160 | var nextAtomState = {
|
161 | d: (prevAtomState == null ? void 0 : prevAtomState.d) || new Map(),
|
162 | v: value
|
163 | };
|
164 | if (nextDependencies) {
|
165 | updateDependencies(atom, nextAtomState, nextDependencies);
|
166 | }
|
167 | if (prevAtomState && isEqualAtomValue(prevAtomState, nextAtomState) && prevAtomState.d === nextAtomState.d) {
|
168 | return prevAtomState;
|
169 | }
|
170 | if (prevAtomState && hasPromiseAtomValue(prevAtomState) && hasPromiseAtomValue(nextAtomState) && isEqualPromiseAtomValue(prevAtomState, nextAtomState)) {
|
171 | if (prevAtomState.d === nextAtomState.d) {
|
172 | return prevAtomState;
|
173 | } else {
|
174 | nextAtomState.v = prevAtomState.v;
|
175 | }
|
176 | }
|
177 | setAtomState(atom, nextAtomState);
|
178 | return nextAtomState;
|
179 | };
|
180 | var setAtomValueOrPromise = function setAtomValueOrPromise(atom, valueOrPromise, nextDependencies, abortPromise) {
|
181 | if (isPromiseLike(valueOrPromise)) {
|
182 | var continuePromise;
|
183 | var promise = new Promise(function (resolve, reject) {
|
184 | var settled = false;
|
185 | valueOrPromise.then(function (v) {
|
186 | if (!settled) {
|
187 | settled = true;
|
188 | var prevAtomState = getAtomState(atom);
|
189 | var nextAtomState = setAtomValue(atom, promise, nextDependencies);
|
190 | resolvePromise(promise, v);
|
191 | resolve(v);
|
192 | if (mountedMap.has(atom) && (prevAtomState == null ? void 0 : prevAtomState.d) !== nextAtomState.d) {
|
193 | mountDependencies(atom, nextAtomState, prevAtomState == null ? void 0 : prevAtomState.d);
|
194 | }
|
195 | }
|
196 | }, function (e) {
|
197 | if (!settled) {
|
198 | settled = true;
|
199 | var prevAtomState = getAtomState(atom);
|
200 | var nextAtomState = setAtomValue(atom, promise, nextDependencies);
|
201 | rejectPromise(promise, e);
|
202 | reject(e);
|
203 | if (mountedMap.has(atom) && (prevAtomState == null ? void 0 : prevAtomState.d) !== nextAtomState.d) {
|
204 | mountDependencies(atom, nextAtomState, prevAtomState == null ? void 0 : prevAtomState.d);
|
205 | }
|
206 | }
|
207 | });
|
208 | continuePromise = function continuePromise(next) {
|
209 | if (!settled) {
|
210 | settled = true;
|
211 | next.then(function (v) {
|
212 | return resolvePromise(promise, v);
|
213 | }, function (e) {
|
214 | return rejectPromise(promise, e);
|
215 | });
|
216 | resolve(next);
|
217 | }
|
218 | };
|
219 | });
|
220 | promise.orig = valueOrPromise;
|
221 | promise.status = 'pending';
|
222 | registerCancelPromise(promise, function (next) {
|
223 | if (next) {
|
224 | continuePromise(next);
|
225 | }
|
226 | abortPromise == null || abortPromise();
|
227 | });
|
228 | return setAtomValue(atom, promise, nextDependencies);
|
229 | }
|
230 | return setAtomValue(atom, valueOrPromise, nextDependencies);
|
231 | };
|
232 | var setAtomError = function setAtomError(atom, error, nextDependencies) {
|
233 | var prevAtomState = getAtomState(atom);
|
234 | var nextAtomState = {
|
235 | d: (prevAtomState == null ? void 0 : prevAtomState.d) || new Map(),
|
236 | e: error
|
237 | };
|
238 | if (nextDependencies) {
|
239 | updateDependencies(atom, nextAtomState, nextDependencies);
|
240 | }
|
241 | if (prevAtomState && isEqualAtomError(prevAtomState, nextAtomState) && prevAtomState.d === nextAtomState.d) {
|
242 | return prevAtomState;
|
243 | }
|
244 | setAtomState(atom, nextAtomState);
|
245 | return nextAtomState;
|
246 | };
|
247 | var readAtomState = function readAtomState(atom, force) {
|
248 | var atomState = getAtomState(atom);
|
249 | if (!force && atomState) {
|
250 | if (mountedMap.has(atom)) {
|
251 | return atomState;
|
252 | }
|
253 | if (Array.from(atomState.d).every(function (_ref) {
|
254 | var a = _ref[0],
|
255 | s = _ref[1];
|
256 | return a === atom || readAtomState(a) === s;
|
257 | })) {
|
258 | return atomState;
|
259 | }
|
260 | }
|
261 | var nextDependencies = new Map();
|
262 | var isSync = true;
|
263 | var getter = function getter(a) {
|
264 | if (a === atom) {
|
265 | var _aState = getAtomState(a);
|
266 | if (_aState) {
|
267 | nextDependencies.set(a, _aState);
|
268 | return returnAtomValue(_aState);
|
269 | }
|
270 | if (hasInitialValue(a)) {
|
271 | nextDependencies.set(a, undefined);
|
272 | return a.init;
|
273 | }
|
274 | throw new Error('no atom init');
|
275 | }
|
276 | var aState = readAtomState(a);
|
277 | nextDependencies.set(a, aState);
|
278 | return returnAtomValue(aState);
|
279 | };
|
280 | var controller;
|
281 | var setSelf;
|
282 | var options = {
|
283 | get signal() {
|
284 | if (!controller) {
|
285 | controller = new AbortController();
|
286 | }
|
287 | return controller.signal;
|
288 | },
|
289 | get setSelf() {
|
290 | if (process.env.NODE_ENV !== 'production' && !isActuallyWritableAtom(atom)) {
|
291 | console.warn('setSelf function cannot be used with read-only atom');
|
292 | }
|
293 | if (!setSelf && isActuallyWritableAtom(atom)) {
|
294 | setSelf = function setSelf() {
|
295 | if (process.env.NODE_ENV !== 'production' && isSync) {
|
296 | console.warn('setSelf function cannot be called in sync');
|
297 | }
|
298 | if (!isSync) {
|
299 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
300 | args[_key] = arguments[_key];
|
301 | }
|
302 | return writeAtom.apply(void 0, [atom].concat(args));
|
303 | }
|
304 | };
|
305 | }
|
306 | return setSelf;
|
307 | }
|
308 | };
|
309 | try {
|
310 | var valueOrPromise = atom.read(getter, options);
|
311 | return setAtomValueOrPromise(atom, valueOrPromise, nextDependencies, function () {
|
312 | var _controller;
|
313 | return (_controller = controller) == null ? void 0 : _controller.abort();
|
314 | });
|
315 | } catch (error) {
|
316 | return setAtomError(atom, error, nextDependencies);
|
317 | } finally {
|
318 | isSync = false;
|
319 | }
|
320 | };
|
321 | var readAtom = function readAtom(atom) {
|
322 | return returnAtomValue(readAtomState(atom));
|
323 | };
|
324 | var addAtom = function addAtom(atom) {
|
325 | var mounted = mountedMap.get(atom);
|
326 | if (!mounted) {
|
327 | mounted = mountAtom(atom);
|
328 | }
|
329 | return mounted;
|
330 | };
|
331 | var canUnmountAtom = function canUnmountAtom(atom, mounted) {
|
332 | return !mounted.l.size && (!mounted.t.size || mounted.t.size === 1 && mounted.t.has(atom));
|
333 | };
|
334 | var delAtom = function delAtom(atom) {
|
335 | var mounted = mountedMap.get(atom);
|
336 | if (mounted && canUnmountAtom(atom, mounted)) {
|
337 | unmountAtom(atom);
|
338 | }
|
339 | };
|
340 | var recomputeDependents = function recomputeDependents(atom) {
|
341 | var dependencyMap = new Map();
|
342 | var dirtyMap = new WeakMap();
|
343 | var getDependents = function getDependents(a) {
|
344 | var _mountedMap$get;
|
345 | var dependents = new Set((_mountedMap$get = mountedMap.get(a)) == null ? void 0 : _mountedMap$get.t);
|
346 | pendingMap.forEach(function (_, pendingAtom) {
|
347 | var _getAtomState;
|
348 | if ((_getAtomState = getAtomState(pendingAtom)) != null && _getAtomState.d.has(a)) {
|
349 | dependents.add(pendingAtom);
|
350 | }
|
351 | });
|
352 | return dependents;
|
353 | };
|
354 | var loop1 = function loop1(a) {
|
355 | getDependents(a).forEach(function (dependent) {
|
356 | if (dependent !== a) {
|
357 | dependencyMap.set(dependent, (dependencyMap.get(dependent) || new Set()).add(a));
|
358 | dirtyMap.set(dependent, (dirtyMap.get(dependent) || 0) + 1);
|
359 | loop1(dependent);
|
360 | }
|
361 | });
|
362 | };
|
363 | loop1(atom);
|
364 | var loop2 = function loop2(a) {
|
365 | getDependents(a).forEach(function (dependent) {
|
366 | if (dependent !== a) {
|
367 | var dirtyCount = dirtyMap.get(dependent);
|
368 | if (dirtyCount) {
|
369 | dirtyMap.set(dependent, --dirtyCount);
|
370 | }
|
371 | if (!dirtyCount) {
|
372 | var _dependencyMap$get;
|
373 | var isChanged = !!((_dependencyMap$get = dependencyMap.get(dependent)) != null && _dependencyMap$get.size);
|
374 | if (isChanged) {
|
375 | var prevAtomState = getAtomState(dependent);
|
376 | var nextAtomState = readAtomState(dependent, true);
|
377 | isChanged = !prevAtomState || !isEqualAtomValue(prevAtomState, nextAtomState);
|
378 | }
|
379 | if (!isChanged) {
|
380 | dependencyMap.forEach(function (s) {
|
381 | return s.delete(dependent);
|
382 | });
|
383 | }
|
384 | }
|
385 | loop2(dependent);
|
386 | }
|
387 | });
|
388 | };
|
389 | loop2(atom);
|
390 | };
|
391 | var writeAtomState = function writeAtomState(atom) {
|
392 | var isSync = true;
|
393 | var getter = function getter(a) {
|
394 | return returnAtomValue(readAtomState(a));
|
395 | };
|
396 | var setter = function setter(a) {
|
397 | var r;
|
398 | for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
399 | args[_key3 - 1] = arguments[_key3];
|
400 | }
|
401 | if (a === atom) {
|
402 | if (!hasInitialValue(a)) {
|
403 | throw new Error('atom not writable');
|
404 | }
|
405 | var prevAtomState = getAtomState(a);
|
406 | var nextAtomState = setAtomValueOrPromise(a, args[0]);
|
407 | if (!prevAtomState || !isEqualAtomValue(prevAtomState, nextAtomState)) {
|
408 | recomputeDependents(a);
|
409 | }
|
410 | } else {
|
411 | r = writeAtomState.apply(void 0, [a].concat(args));
|
412 | }
|
413 | if (!isSync) {
|
414 | var flushed = flushPending();
|
415 | if (process.env.NODE_ENV !== 'production') {
|
416 | storeListenersRev2.forEach(function (l) {
|
417 | return l({
|
418 | type: 'async-write',
|
419 | flushed: flushed
|
420 | });
|
421 | });
|
422 | }
|
423 | }
|
424 | return r;
|
425 | };
|
426 | for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
427 | args[_key2 - 1] = arguments[_key2];
|
428 | }
|
429 | var result = atom.write.apply(atom, [getter, setter].concat(args));
|
430 | isSync = false;
|
431 | return result;
|
432 | };
|
433 | var writeAtom = function writeAtom(atom) {
|
434 | for (var _len4 = arguments.length, args = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
|
435 | args[_key4 - 1] = arguments[_key4];
|
436 | }
|
437 | var result = writeAtomState.apply(void 0, [atom].concat(args));
|
438 | var flushed = flushPending();
|
439 | if (process.env.NODE_ENV !== 'production') {
|
440 | storeListenersRev2.forEach(function (l) {
|
441 | return l({
|
442 | type: 'write',
|
443 | flushed: flushed
|
444 | });
|
445 | });
|
446 | }
|
447 | return result;
|
448 | };
|
449 | var mountAtom = function mountAtom(atom, initialDependent, onMountQueue) {
|
450 | var _getAtomState2;
|
451 | var queue = onMountQueue || [];
|
452 | (_getAtomState2 = getAtomState(atom)) == null || _getAtomState2.d.forEach(function (_, a) {
|
453 | var aMounted = mountedMap.get(a);
|
454 | if (aMounted) {
|
455 | aMounted.t.add(atom);
|
456 | } else {
|
457 | if (a !== atom) {
|
458 | mountAtom(a, atom, queue);
|
459 | }
|
460 | }
|
461 | });
|
462 | readAtomState(atom);
|
463 | var mounted = {
|
464 | t: new Set(initialDependent && [initialDependent]),
|
465 | l: new Set()
|
466 | };
|
467 | mountedMap.set(atom, mounted);
|
468 | if (process.env.NODE_ENV !== 'production') {
|
469 | mountedAtoms.add(atom);
|
470 | }
|
471 | if (isActuallyWritableAtom(atom) && atom.onMount) {
|
472 | var onMount = atom.onMount;
|
473 | queue.push(function () {
|
474 | var onUnmount = onMount(function () {
|
475 | for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
|
476 | args[_key5] = arguments[_key5];
|
477 | }
|
478 | return writeAtom.apply(void 0, [atom].concat(args));
|
479 | });
|
480 | if (onUnmount) {
|
481 | mounted.u = onUnmount;
|
482 | }
|
483 | });
|
484 | }
|
485 | if (!onMountQueue) {
|
486 | queue.forEach(function (f) {
|
487 | return f();
|
488 | });
|
489 | }
|
490 | return mounted;
|
491 | };
|
492 | var unmountAtom = function unmountAtom(atom) {
|
493 | var _mountedMap$get2;
|
494 | var onUnmount = (_mountedMap$get2 = mountedMap.get(atom)) == null ? void 0 : _mountedMap$get2.u;
|
495 | if (onUnmount) {
|
496 | onUnmount();
|
497 | }
|
498 | mountedMap.delete(atom);
|
499 | if (process.env.NODE_ENV !== 'production') {
|
500 | mountedAtoms.delete(atom);
|
501 | }
|
502 | var atomState = getAtomState(atom);
|
503 | if (atomState) {
|
504 | if (hasPromiseAtomValue(atomState)) {
|
505 | cancelPromise(atomState.v);
|
506 | }
|
507 | atomState.d.forEach(function (_, a) {
|
508 | if (a !== atom) {
|
509 | var mounted = mountedMap.get(a);
|
510 | if (mounted) {
|
511 | mounted.t.delete(atom);
|
512 | if (canUnmountAtom(a, mounted)) {
|
513 | unmountAtom(a);
|
514 | }
|
515 | }
|
516 | }
|
517 | });
|
518 | } else if (process.env.NODE_ENV !== 'production') {
|
519 | console.warn('[Bug] could not find atom state to unmount', atom);
|
520 | }
|
521 | };
|
522 | var mountDependencies = function mountDependencies(atom, atomState, prevDependencies) {
|
523 | var depSet = new Set(atomState.d.keys());
|
524 | prevDependencies == null || prevDependencies.forEach(function (_, a) {
|
525 | if (depSet.has(a)) {
|
526 | depSet.delete(a);
|
527 | return;
|
528 | }
|
529 | var mounted = mountedMap.get(a);
|
530 | if (mounted) {
|
531 | mounted.t.delete(atom);
|
532 | if (canUnmountAtom(a, mounted)) {
|
533 | unmountAtom(a);
|
534 | }
|
535 | }
|
536 | });
|
537 | depSet.forEach(function (a) {
|
538 | var mounted = mountedMap.get(a);
|
539 | if (mounted) {
|
540 | mounted.t.add(atom);
|
541 | } else if (mountedMap.has(atom)) {
|
542 | mountAtom(a, atom);
|
543 | }
|
544 | });
|
545 | };
|
546 | var flushPending = function flushPending() {
|
547 | var flushed;
|
548 | if (process.env.NODE_ENV !== 'production') {
|
549 | flushed = new Set();
|
550 | }
|
551 | while (pendingMap.size) {
|
552 | var pending = Array.from(pendingMap);
|
553 | pendingMap.clear();
|
554 | pending.forEach(function (_ref2) {
|
555 | var atom = _ref2[0],
|
556 | prevAtomState = _ref2[1];
|
557 | var atomState = getAtomState(atom);
|
558 | if (atomState) {
|
559 | var mounted = mountedMap.get(atom);
|
560 | if (mounted && atomState.d !== (prevAtomState == null ? void 0 : prevAtomState.d)) {
|
561 | mountDependencies(atom, atomState, prevAtomState == null ? void 0 : prevAtomState.d);
|
562 | }
|
563 | if (mounted && !(prevAtomState && !hasPromiseAtomValue(prevAtomState) && (isEqualAtomValue(prevAtomState, atomState) || isEqualAtomError(prevAtomState, atomState)))) {
|
564 | mounted.l.forEach(function (listener) {
|
565 | return listener();
|
566 | });
|
567 | if (process.env.NODE_ENV !== 'production') {
|
568 | flushed.add(atom);
|
569 | }
|
570 | }
|
571 | } else if (process.env.NODE_ENV !== 'production') {
|
572 | console.warn('[Bug] no atom state to flush');
|
573 | }
|
574 | });
|
575 | }
|
576 | if (process.env.NODE_ENV !== 'production') {
|
577 | return flushed;
|
578 | }
|
579 | };
|
580 | var subscribeAtom = function subscribeAtom(atom, listener) {
|
581 | var mounted = addAtom(atom);
|
582 | var flushed = flushPending();
|
583 | var listeners = mounted.l;
|
584 | listeners.add(listener);
|
585 | if (process.env.NODE_ENV !== 'production') {
|
586 | storeListenersRev2.forEach(function (l) {
|
587 | return l({
|
588 | type: 'sub',
|
589 | flushed: flushed
|
590 | });
|
591 | });
|
592 | }
|
593 | return function () {
|
594 | listeners.delete(listener);
|
595 | delAtom(atom);
|
596 | if (process.env.NODE_ENV !== 'production') {
|
597 | storeListenersRev2.forEach(function (l) {
|
598 | return l({
|
599 | type: 'unsub'
|
600 | });
|
601 | });
|
602 | }
|
603 | };
|
604 | };
|
605 | if (process.env.NODE_ENV !== 'production') {
|
606 | return {
|
607 | get: readAtom,
|
608 | set: writeAtom,
|
609 | sub: subscribeAtom,
|
610 | dev_subscribe_store: function dev_subscribe_store(l, rev) {
|
611 | if (rev !== 2) {
|
612 | throw new Error('The current StoreListener revision is 2.');
|
613 | }
|
614 | storeListenersRev2.add(l);
|
615 | return function () {
|
616 | storeListenersRev2.delete(l);
|
617 | };
|
618 | },
|
619 | dev_get_mounted_atoms: function dev_get_mounted_atoms() {
|
620 | return mountedAtoms.values();
|
621 | },
|
622 | dev_get_atom_state: function dev_get_atom_state(a) {
|
623 | return atomStateMap.get(a);
|
624 | },
|
625 | dev_get_mounted: function dev_get_mounted(a) {
|
626 | return mountedMap.get(a);
|
627 | },
|
628 | dev_restore_atoms: function dev_restore_atoms(values) {
|
629 | for (var _iterator = _createForOfIteratorHelperLoose(values), _step; !(_step = _iterator()).done;) {
|
630 | var _step$value = _step.value,
|
631 | atom = _step$value[0],
|
632 | valueOrPromise = _step$value[1];
|
633 | if (hasInitialValue(atom)) {
|
634 | setAtomValueOrPromise(atom, valueOrPromise);
|
635 | recomputeDependents(atom);
|
636 | }
|
637 | }
|
638 | var flushed = flushPending();
|
639 | storeListenersRev2.forEach(function (l) {
|
640 | return l({
|
641 | type: 'restore',
|
642 | flushed: flushed
|
643 | });
|
644 | });
|
645 | }
|
646 | };
|
647 | }
|
648 | return {
|
649 | get: readAtom,
|
650 | set: writeAtom,
|
651 | sub: subscribeAtom
|
652 | };
|
653 | };
|
654 | var defaultStore;
|
655 | if (process.env.NODE_ENV !== 'production') {
|
656 | if (typeof globalThis.__NUMBER_OF_JOTAI_INSTANCES__ === 'number') {
|
657 | ++globalThis.__NUMBER_OF_JOTAI_INSTANCES__;
|
658 | } else {
|
659 | globalThis.__NUMBER_OF_JOTAI_INSTANCES__ = 1;
|
660 | }
|
661 | }
|
662 | var getDefaultStore = function getDefaultStore() {
|
663 | if (!defaultStore) {
|
664 | if (process.env.NODE_ENV !== 'production' && globalThis.__NUMBER_OF_JOTAI_INSTANCES__ !== 1) {
|
665 | console.warn('Detected multiple Jotai instances. It may cause unexpected behavior with the default store. https://github.com/pmndrs/jotai/discussions/2044');
|
666 | }
|
667 | defaultStore = createStore();
|
668 | }
|
669 | return defaultStore;
|
670 | };
|
671 |
|
672 | exports.atom = atom;
|
673 | exports.createStore = createStore;
|
674 | exports.getDefaultStore = getDefaultStore;
|