1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('jotai'), require('react')) :
|
3 | typeof define === 'function' && define.amd ? define(['exports', 'jotai', 'react'], factory) :
|
4 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.jotaiUtils = {}, global.jotai, global.React));
|
5 | })(this, (function (exports, jotai, react) { 'use strict';
|
6 |
|
7 | var RESET = Symbol();
|
8 |
|
9 | function atomWithReset(initialValue) {
|
10 | var anAtom = jotai.atom(initialValue, function (get, set, update) {
|
11 | var nextValue = typeof update === 'function' ? update(get(anAtom)) : update;
|
12 | set(anAtom, nextValue === RESET ? initialValue : nextValue);
|
13 | });
|
14 | return anAtom;
|
15 | }
|
16 |
|
17 | function _regeneratorRuntime() {
|
18 |
|
19 |
|
20 | _regeneratorRuntime = function () {
|
21 | return exports;
|
22 | };
|
23 |
|
24 | var exports = {},
|
25 | Op = Object.prototype,
|
26 | hasOwn = Op.hasOwnProperty,
|
27 | $Symbol = "function" == typeof Symbol ? Symbol : {},
|
28 | iteratorSymbol = $Symbol.iterator || "@@iterator",
|
29 | asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
|
30 | toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
|
31 |
|
32 | function define(obj, key, value) {
|
33 | return Object.defineProperty(obj, key, {
|
34 | value: value,
|
35 | enumerable: !0,
|
36 | configurable: !0,
|
37 | writable: !0
|
38 | }), obj[key];
|
39 | }
|
40 |
|
41 | try {
|
42 | define({}, "");
|
43 | } catch (err) {
|
44 | define = function (obj, key, value) {
|
45 | return obj[key] = value;
|
46 | };
|
47 | }
|
48 |
|
49 | function wrap(innerFn, outerFn, self, tryLocsList) {
|
50 | var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
|
51 | generator = Object.create(protoGenerator.prototype),
|
52 | context = new Context(tryLocsList || []);
|
53 | return generator._invoke = function (innerFn, self, context) {
|
54 | var state = "suspendedStart";
|
55 | return function (method, arg) {
|
56 | if ("executing" === state) throw new Error("Generator is already running");
|
57 |
|
58 | if ("completed" === state) {
|
59 | if ("throw" === method) throw arg;
|
60 | return doneResult();
|
61 | }
|
62 |
|
63 | for (context.method = method, context.arg = arg;;) {
|
64 | var delegate = context.delegate;
|
65 |
|
66 | if (delegate) {
|
67 | var delegateResult = maybeInvokeDelegate(delegate, context);
|
68 |
|
69 | if (delegateResult) {
|
70 | if (delegateResult === ContinueSentinel) continue;
|
71 | return delegateResult;
|
72 | }
|
73 | }
|
74 |
|
75 | if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
|
76 | if ("suspendedStart" === state) throw state = "completed", context.arg;
|
77 | context.dispatchException(context.arg);
|
78 | } else "return" === context.method && context.abrupt("return", context.arg);
|
79 | state = "executing";
|
80 | var record = tryCatch(innerFn, self, context);
|
81 |
|
82 | if ("normal" === record.type) {
|
83 | if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
|
84 | return {
|
85 | value: record.arg,
|
86 | done: context.done
|
87 | };
|
88 | }
|
89 |
|
90 | "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
|
91 | }
|
92 | };
|
93 | }(innerFn, self, context), generator;
|
94 | }
|
95 |
|
96 | function tryCatch(fn, obj, arg) {
|
97 | try {
|
98 | return {
|
99 | type: "normal",
|
100 | arg: fn.call(obj, arg)
|
101 | };
|
102 | } catch (err) {
|
103 | return {
|
104 | type: "throw",
|
105 | arg: err
|
106 | };
|
107 | }
|
108 | }
|
109 |
|
110 | exports.wrap = wrap;
|
111 | var ContinueSentinel = {};
|
112 |
|
113 | function Generator() {}
|
114 |
|
115 | function GeneratorFunction() {}
|
116 |
|
117 | function GeneratorFunctionPrototype() {}
|
118 |
|
119 | var IteratorPrototype = {};
|
120 | define(IteratorPrototype, iteratorSymbol, function () {
|
121 | return this;
|
122 | });
|
123 | var getProto = Object.getPrototypeOf,
|
124 | NativeIteratorPrototype = getProto && getProto(getProto(values([])));
|
125 | NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
|
126 | var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
|
127 |
|
128 | function defineIteratorMethods(prototype) {
|
129 | ["next", "throw", "return"].forEach(function (method) {
|
130 | define(prototype, method, function (arg) {
|
131 | return this._invoke(method, arg);
|
132 | });
|
133 | });
|
134 | }
|
135 |
|
136 | function AsyncIterator(generator, PromiseImpl) {
|
137 | function invoke(method, arg, resolve, reject) {
|
138 | var record = tryCatch(generator[method], generator, arg);
|
139 |
|
140 | if ("throw" !== record.type) {
|
141 | var result = record.arg,
|
142 | value = result.value;
|
143 | return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
|
144 | invoke("next", value, resolve, reject);
|
145 | }, function (err) {
|
146 | invoke("throw", err, resolve, reject);
|
147 | }) : PromiseImpl.resolve(value).then(function (unwrapped) {
|
148 | result.value = unwrapped, resolve(result);
|
149 | }, function (error) {
|
150 | return invoke("throw", error, resolve, reject);
|
151 | });
|
152 | }
|
153 |
|
154 | reject(record.arg);
|
155 | }
|
156 |
|
157 | var previousPromise;
|
158 |
|
159 | this._invoke = function (method, arg) {
|
160 | function callInvokeWithMethodAndArg() {
|
161 | return new PromiseImpl(function (resolve, reject) {
|
162 | invoke(method, arg, resolve, reject);
|
163 | });
|
164 | }
|
165 |
|
166 | return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
|
167 | };
|
168 | }
|
169 |
|
170 | function maybeInvokeDelegate(delegate, context) {
|
171 | var method = delegate.iterator[context.method];
|
172 |
|
173 | if (undefined === method) {
|
174 | if (context.delegate = null, "throw" === context.method) {
|
175 | if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel;
|
176 | context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method");
|
177 | }
|
178 |
|
179 | return ContinueSentinel;
|
180 | }
|
181 |
|
182 | var record = tryCatch(method, delegate.iterator, context.arg);
|
183 | if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
|
184 | var info = record.arg;
|
185 | return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel);
|
186 | }
|
187 |
|
188 | function pushTryEntry(locs) {
|
189 | var entry = {
|
190 | tryLoc: locs[0]
|
191 | };
|
192 | 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
|
193 | }
|
194 |
|
195 | function resetTryEntry(entry) {
|
196 | var record = entry.completion || {};
|
197 | record.type = "normal", delete record.arg, entry.completion = record;
|
198 | }
|
199 |
|
200 | function Context(tryLocsList) {
|
201 | this.tryEntries = [{
|
202 | tryLoc: "root"
|
203 | }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
|
204 | }
|
205 |
|
206 | function values(iterable) {
|
207 | if (iterable) {
|
208 | var iteratorMethod = iterable[iteratorSymbol];
|
209 | if (iteratorMethod) return iteratorMethod.call(iterable);
|
210 | if ("function" == typeof iterable.next) return iterable;
|
211 |
|
212 | if (!isNaN(iterable.length)) {
|
213 | var i = -1,
|
214 | next = function next() {
|
215 | for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
|
216 |
|
217 | return next.value = undefined, next.done = !0, next;
|
218 | };
|
219 |
|
220 | return next.next = next;
|
221 | }
|
222 | }
|
223 |
|
224 | return {
|
225 | next: doneResult
|
226 | };
|
227 | }
|
228 |
|
229 | function doneResult() {
|
230 | return {
|
231 | value: undefined,
|
232 | done: !0
|
233 | };
|
234 | }
|
235 |
|
236 | return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
|
237 | var ctor = "function" == typeof genFun && genFun.constructor;
|
238 | return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
|
239 | }, exports.mark = function (genFun) {
|
240 | return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
|
241 | }, exports.awrap = function (arg) {
|
242 | return {
|
243 | __await: arg
|
244 | };
|
245 | }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
|
246 | return this;
|
247 | }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
|
248 | void 0 === PromiseImpl && (PromiseImpl = Promise);
|
249 | var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
|
250 | return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
|
251 | return result.done ? result.value : iter.next();
|
252 | });
|
253 | }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
|
254 | return this;
|
255 | }), define(Gp, "toString", function () {
|
256 | return "[object Generator]";
|
257 | }), exports.keys = function (object) {
|
258 | var keys = [];
|
259 |
|
260 | for (var key in object) keys.push(key);
|
261 |
|
262 | return keys.reverse(), function next() {
|
263 | for (; keys.length;) {
|
264 | var key = keys.pop();
|
265 | if (key in object) return next.value = key, next.done = !1, next;
|
266 | }
|
267 |
|
268 | return next.done = !0, next;
|
269 | };
|
270 | }, exports.values = values, Context.prototype = {
|
271 | constructor: Context,
|
272 | reset: function (skipTempReset) {
|
273 | if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
|
274 | },
|
275 | stop: function () {
|
276 | this.done = !0;
|
277 | var rootRecord = this.tryEntries[0].completion;
|
278 | if ("throw" === rootRecord.type) throw rootRecord.arg;
|
279 | return this.rval;
|
280 | },
|
281 | dispatchException: function (exception) {
|
282 | if (this.done) throw exception;
|
283 | var context = this;
|
284 |
|
285 | function handle(loc, caught) {
|
286 | return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
|
287 | }
|
288 |
|
289 | for (var i = this.tryEntries.length - 1; i >= 0; --i) {
|
290 | var entry = this.tryEntries[i],
|
291 | record = entry.completion;
|
292 | if ("root" === entry.tryLoc) return handle("end");
|
293 |
|
294 | if (entry.tryLoc <= this.prev) {
|
295 | var hasCatch = hasOwn.call(entry, "catchLoc"),
|
296 | hasFinally = hasOwn.call(entry, "finallyLoc");
|
297 |
|
298 | if (hasCatch && hasFinally) {
|
299 | if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
|
300 | if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
|
301 | } else if (hasCatch) {
|
302 | if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
|
303 | } else {
|
304 | if (!hasFinally) throw new Error("try statement without catch or finally");
|
305 | if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
|
306 | }
|
307 | }
|
308 | }
|
309 | },
|
310 | abrupt: function (type, arg) {
|
311 | for (var i = this.tryEntries.length - 1; i >= 0; --i) {
|
312 | var entry = this.tryEntries[i];
|
313 |
|
314 | if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
|
315 | var finallyEntry = entry;
|
316 | break;
|
317 | }
|
318 | }
|
319 |
|
320 | finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
|
321 | var record = finallyEntry ? finallyEntry.completion : {};
|
322 | return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
|
323 | },
|
324 | complete: function (record, afterLoc) {
|
325 | if ("throw" === record.type) throw record.arg;
|
326 | return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel;
|
327 | },
|
328 | finish: function (finallyLoc) {
|
329 | for (var i = this.tryEntries.length - 1; i >= 0; --i) {
|
330 | var entry = this.tryEntries[i];
|
331 | if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
|
332 | }
|
333 | },
|
334 | catch: function (tryLoc) {
|
335 | for (var i = this.tryEntries.length - 1; i >= 0; --i) {
|
336 | var entry = this.tryEntries[i];
|
337 |
|
338 | if (entry.tryLoc === tryLoc) {
|
339 | var record = entry.completion;
|
340 |
|
341 | if ("throw" === record.type) {
|
342 | var thrown = record.arg;
|
343 | resetTryEntry(entry);
|
344 | }
|
345 |
|
346 | return thrown;
|
347 | }
|
348 | }
|
349 |
|
350 | throw new Error("illegal catch attempt");
|
351 | },
|
352 | delegateYield: function (iterable, resultName, nextLoc) {
|
353 | return this.delegate = {
|
354 | iterator: values(iterable),
|
355 | resultName: resultName,
|
356 | nextLoc: nextLoc
|
357 | }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
|
358 | }
|
359 | }, exports;
|
360 | }
|
361 |
|
362 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
363 | try {
|
364 | var info = gen[key](arg);
|
365 | var value = info.value;
|
366 | } catch (error) {
|
367 | reject(error);
|
368 | return;
|
369 | }
|
370 |
|
371 | if (info.done) {
|
372 | resolve(value);
|
373 | } else {
|
374 | Promise.resolve(value).then(_next, _throw);
|
375 | }
|
376 | }
|
377 |
|
378 | function _asyncToGenerator(fn) {
|
379 | return function () {
|
380 | var self = this,
|
381 | args = arguments;
|
382 | return new Promise(function (resolve, reject) {
|
383 | var gen = fn.apply(self, args);
|
384 |
|
385 | function _next(value) {
|
386 | asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
387 | }
|
388 |
|
389 | function _throw(err) {
|
390 | asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
391 | }
|
392 |
|
393 | _next(undefined);
|
394 | });
|
395 | };
|
396 | }
|
397 |
|
398 | function _extends() {
|
399 | _extends = Object.assign ? Object.assign.bind() : function (target) {
|
400 | for (var i = 1; i < arguments.length; i++) {
|
401 | var source = arguments[i];
|
402 |
|
403 | for (var key in source) {
|
404 | if (Object.prototype.hasOwnProperty.call(source, key)) {
|
405 | target[key] = source[key];
|
406 | }
|
407 | }
|
408 | }
|
409 |
|
410 | return target;
|
411 | };
|
412 | return _extends.apply(this, arguments);
|
413 | }
|
414 |
|
415 | function _unsupportedIterableToArray(o, minLen) {
|
416 | if (!o) return;
|
417 | if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
418 | var n = Object.prototype.toString.call(o).slice(8, -1);
|
419 | if (n === "Object" && o.constructor) n = o.constructor.name;
|
420 | if (n === "Map" || n === "Set") return Array.from(o);
|
421 | if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
422 | }
|
423 |
|
424 | function _arrayLikeToArray(arr, len) {
|
425 | if (len == null || len > arr.length) len = arr.length;
|
426 |
|
427 | for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
428 |
|
429 | return arr2;
|
430 | }
|
431 |
|
432 | function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
433 | var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
434 | if (it) return (it = it.call(o)).next.bind(it);
|
435 |
|
436 | if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
437 | if (it) o = it;
|
438 | var i = 0;
|
439 | return function () {
|
440 | if (i >= o.length) return {
|
441 | done: true
|
442 | };
|
443 | return {
|
444 | done: false,
|
445 | value: o[i++]
|
446 | };
|
447 | };
|
448 | }
|
449 |
|
450 | throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
451 | }
|
452 |
|
453 | var WRITE_ATOM = 'w';
|
454 | var RESTORE_ATOMS = 'h';
|
455 |
|
456 | function useResetAtom(anAtom, scope) {
|
457 | var ScopeContext = jotai.SECRET_INTERNAL_getScopeContext(scope);
|
458 | var store = react.useContext(ScopeContext).s;
|
459 | var setAtom = react.useCallback(function () {
|
460 | return store[WRITE_ATOM](anAtom, RESET);
|
461 | }, [store, anAtom]);
|
462 | return setAtom;
|
463 | }
|
464 |
|
465 | function useReducerAtom(anAtom, reducer, scope) {
|
466 | var _useAtom = jotai.useAtom(anAtom, scope),
|
467 | state = _useAtom[0],
|
468 | setState = _useAtom[1];
|
469 |
|
470 | var dispatch = react.useCallback(function (action) {
|
471 | setState(function (prev) {
|
472 | return reducer(prev, action);
|
473 | });
|
474 | }, [setState, reducer]);
|
475 | return [state, dispatch];
|
476 | }
|
477 |
|
478 | function atomWithReducer(initialValue, reducer) {
|
479 | var anAtom = jotai.atom(initialValue, function (get, set, action) {
|
480 | return set(anAtom, reducer(get(anAtom), action));
|
481 | });
|
482 | return anAtom;
|
483 | }
|
484 |
|
485 | function atomFamily(initializeAtom, areEqual) {
|
486 | var shouldRemove = null;
|
487 | var atoms = new Map();
|
488 |
|
489 | var createAtom = function createAtom(param) {
|
490 | var item;
|
491 |
|
492 | if (areEqual === undefined) {
|
493 | item = atoms.get(param);
|
494 | } else {
|
495 | for (var _iterator = _createForOfIteratorHelperLoose(atoms), _step; !(_step = _iterator()).done;) {
|
496 | var _step$value = _step.value,
|
497 | key = _step$value[0],
|
498 | value = _step$value[1];
|
499 |
|
500 | if (areEqual(key, param)) {
|
501 | item = value;
|
502 | break;
|
503 | }
|
504 | }
|
505 | }
|
506 |
|
507 | if (item !== undefined) {
|
508 | if (shouldRemove != null && shouldRemove(item[1], param)) {
|
509 | createAtom.remove(param);
|
510 | } else {
|
511 | return item[0];
|
512 | }
|
513 | }
|
514 |
|
515 | var newAtom = initializeAtom(param);
|
516 | atoms.set(param, [newAtom, Date.now()]);
|
517 | return newAtom;
|
518 | };
|
519 |
|
520 | createAtom.remove = function (param) {
|
521 | if (areEqual === undefined) {
|
522 | atoms.delete(param);
|
523 | } else {
|
524 | for (var _iterator2 = _createForOfIteratorHelperLoose(atoms), _step2; !(_step2 = _iterator2()).done;) {
|
525 | var _step2$value = _step2.value,
|
526 | key = _step2$value[0];
|
527 |
|
528 | if (areEqual(key, param)) {
|
529 | atoms.delete(key);
|
530 | break;
|
531 | }
|
532 | }
|
533 | }
|
534 | };
|
535 |
|
536 | createAtom.setShouldRemove = function (fn) {
|
537 | shouldRemove = fn;
|
538 | if (!shouldRemove) return;
|
539 |
|
540 | for (var _iterator3 = _createForOfIteratorHelperLoose(atoms), _step3; !(_step3 = _iterator3()).done;) {
|
541 | var _step3$value = _step3.value,
|
542 | key = _step3$value[0],
|
543 | value = _step3$value[1];
|
544 |
|
545 | if (shouldRemove(value[1], key)) {
|
546 | atoms.delete(key);
|
547 | }
|
548 | }
|
549 | };
|
550 |
|
551 | return createAtom;
|
552 | }
|
553 |
|
554 | var getWeakCacheItem = function getWeakCacheItem(cache, deps) {
|
555 | do {
|
556 | var _deps = deps,
|
557 | dep = _deps[0],
|
558 | rest = _deps.slice(1);
|
559 |
|
560 | var entry = cache.get(dep);
|
561 |
|
562 | if (!entry) {
|
563 | return;
|
564 | }
|
565 |
|
566 | if (!rest.length) {
|
567 | return entry[1];
|
568 | }
|
569 |
|
570 | cache = entry[0];
|
571 | deps = rest;
|
572 | } while (deps.length);
|
573 | };
|
574 |
|
575 | var setWeakCacheItem = function setWeakCacheItem(cache, deps, item) {
|
576 | do {
|
577 | var _deps2 = deps,
|
578 | dep = _deps2[0],
|
579 | rest = _deps2.slice(1);
|
580 |
|
581 | var entry = cache.get(dep);
|
582 |
|
583 | if (!entry) {
|
584 | entry = [new WeakMap()];
|
585 | cache.set(dep, entry);
|
586 | }
|
587 |
|
588 | if (!rest.length) {
|
589 | entry[1] = item;
|
590 | return;
|
591 | }
|
592 |
|
593 | cache = entry[0];
|
594 | deps = rest;
|
595 | } while (deps.length);
|
596 | };
|
597 |
|
598 | var createMemoizeAtom = function createMemoizeAtom() {
|
599 | var cache = new WeakMap();
|
600 |
|
601 | var memoizeAtom = function memoizeAtom(createAtom, deps) {
|
602 | var cachedAtom = getWeakCacheItem(cache, deps);
|
603 |
|
604 | if (cachedAtom) {
|
605 | return cachedAtom;
|
606 | }
|
607 |
|
608 | var createdAtom = createAtom();
|
609 | setWeakCacheItem(cache, deps, createdAtom);
|
610 | return createdAtom;
|
611 | };
|
612 |
|
613 | return memoizeAtom;
|
614 | };
|
615 |
|
616 | var memoizeAtom$4 = createMemoizeAtom();
|
617 | function selectAtom(anAtom, selector, equalityFn) {
|
618 | if (equalityFn === void 0) {
|
619 | equalityFn = Object.is;
|
620 | }
|
621 |
|
622 | return memoizeAtom$4(function () {
|
623 | var refAtom = jotai.atom(function () {
|
624 | return {};
|
625 | });
|
626 | var derivedAtom = jotai.atom(function (get) {
|
627 | var slice = selector(get(anAtom));
|
628 | var ref = get(refAtom);
|
629 |
|
630 | if ('prev' in ref && equalityFn(ref.prev, slice)) {
|
631 | return ref.prev;
|
632 | }
|
633 |
|
634 | ref.prev = slice;
|
635 | return slice;
|
636 | });
|
637 | return derivedAtom;
|
638 | }, [anAtom, selector, equalityFn]);
|
639 | }
|
640 |
|
641 | function useAtomCallback(callback, scope) {
|
642 | var anAtom = react.useMemo(function () {
|
643 | return jotai.atom(null, function (get, set, _ref) {
|
644 | var arg = _ref[0],
|
645 | resolve = _ref[1],
|
646 | reject = _ref[2];
|
647 |
|
648 | try {
|
649 | resolve(callback(get, set, arg));
|
650 | } catch (e) {
|
651 | reject(e);
|
652 | }
|
653 | });
|
654 | }, [callback]);
|
655 | var invoke = jotai.useSetAtom(anAtom, scope);
|
656 | return react.useCallback(function (arg) {
|
657 | var isSync = true;
|
658 | var settled = {};
|
659 | var promise = new Promise(function (resolve, reject) {
|
660 | invoke([arg, function (v) {
|
661 | if (isSync) {
|
662 | settled = {
|
663 | v: v
|
664 | };
|
665 | } else {
|
666 | resolve(v);
|
667 | }
|
668 | }, function (e) {
|
669 | if (isSync) {
|
670 | settled = {
|
671 | e: e
|
672 | };
|
673 | } else {
|
674 | reject(e);
|
675 | }
|
676 | }]);
|
677 | });
|
678 | isSync = false;
|
679 |
|
680 | if ('e' in settled) {
|
681 | throw settled.e;
|
682 | }
|
683 |
|
684 | if ('v' in settled) {
|
685 | return settled.v;
|
686 | }
|
687 |
|
688 | return promise;
|
689 | }, [invoke]);
|
690 | }
|
691 |
|
692 | var memoizeAtom$3 = createMemoizeAtom();
|
693 |
|
694 | var deepFreeze = function deepFreeze(obj) {
|
695 | if (typeof obj !== 'object' || obj === null) return;
|
696 | Object.freeze(obj);
|
697 | var propNames = Object.getOwnPropertyNames(obj);
|
698 |
|
699 | for (var _iterator = _createForOfIteratorHelperLoose(propNames), _step; !(_step = _iterator()).done;) {
|
700 | var name = _step.value;
|
701 | var value = obj[name];
|
702 | deepFreeze(value);
|
703 | }
|
704 |
|
705 | return obj;
|
706 | };
|
707 |
|
708 | function freezeAtom(anAtom) {
|
709 | return memoizeAtom$3(function () {
|
710 | var frozenAtom = jotai.atom(function (get) {
|
711 | return deepFreeze(get(anAtom));
|
712 | }, function (_get, set, arg) {
|
713 | return set(anAtom, arg);
|
714 | });
|
715 | return frozenAtom;
|
716 | }, [anAtom]);
|
717 | }
|
718 | function freezeAtomCreator(createAtom) {
|
719 | return function () {
|
720 | var anAtom = createAtom.apply(void 0, arguments);
|
721 | var origRead = anAtom.read;
|
722 |
|
723 | anAtom.read = function (get) {
|
724 | return deepFreeze(origRead(get));
|
725 | };
|
726 |
|
727 | return anAtom;
|
728 | };
|
729 | }
|
730 |
|
731 | var memoizeAtom$2 = createMemoizeAtom();
|
732 |
|
733 | var isWritable = function isWritable(atom) {
|
734 | return !!atom.write;
|
735 | };
|
736 |
|
737 | var isFunction = function isFunction(x) {
|
738 | return typeof x === 'function';
|
739 | };
|
740 |
|
741 | function splitAtom(arrAtom, keyExtractor) {
|
742 | return memoizeAtom$2(function () {
|
743 | var mappingCache = new WeakMap();
|
744 |
|
745 | var getMapping = function getMapping(arr, prev) {
|
746 | var mapping = mappingCache.get(arr);
|
747 |
|
748 | if (mapping) {
|
749 | return mapping;
|
750 | }
|
751 |
|
752 | var prevMapping = prev && mappingCache.get(prev);
|
753 | var atomList = [];
|
754 | var keyList = [];
|
755 | arr.forEach(function (item, index) {
|
756 | var key = keyExtractor ? keyExtractor(item) : index;
|
757 | keyList[index] = key;
|
758 | var cachedAtom = prevMapping && prevMapping.atomList[prevMapping.keyList.indexOf(key)];
|
759 |
|
760 | if (cachedAtom) {
|
761 | atomList[index] = cachedAtom;
|
762 | return;
|
763 | }
|
764 |
|
765 | var read = function read(get) {
|
766 | var ref = get(refAtom);
|
767 | var currArr = get(arrAtom);
|
768 | var mapping = getMapping(currArr, ref.prev);
|
769 | var index = mapping.keyList.indexOf(key);
|
770 |
|
771 | if (index < 0 || index >= currArr.length) {
|
772 | var prevItem = arr[getMapping(arr).keyList.indexOf(key)];
|
773 |
|
774 | if (prevItem) {
|
775 | return prevItem;
|
776 | }
|
777 |
|
778 | throw new Error('splitAtom: index out of bounds for read');
|
779 | }
|
780 |
|
781 | return currArr[index];
|
782 | };
|
783 |
|
784 | var write = function write(get, set, update) {
|
785 | var ref = get(refAtom);
|
786 | var arr = get(arrAtom);
|
787 | var mapping = getMapping(arr, ref.prev);
|
788 | var index = mapping.keyList.indexOf(key);
|
789 |
|
790 | if (index < 0 || index >= arr.length) {
|
791 | throw new Error('splitAtom: index out of bounds for write');
|
792 | }
|
793 |
|
794 | var nextItem = isFunction(update) ? update(arr[index]) : update;
|
795 | set(arrAtom, [].concat(arr.slice(0, index), [nextItem], arr.slice(index + 1)));
|
796 | };
|
797 |
|
798 | atomList[index] = isWritable(arrAtom) ? jotai.atom(read, write) : jotai.atom(read);
|
799 | });
|
800 |
|
801 | if (prevMapping && prevMapping.keyList.length === keyList.length && prevMapping.keyList.every(function (x, i) {
|
802 | return x === keyList[i];
|
803 | })) {
|
804 | mapping = prevMapping;
|
805 | } else {
|
806 | mapping = {
|
807 | atomList: atomList,
|
808 | keyList: keyList
|
809 | };
|
810 | }
|
811 |
|
812 | mappingCache.set(arr, mapping);
|
813 | return mapping;
|
814 | };
|
815 |
|
816 | var refAtom = jotai.atom(function () {
|
817 | return {};
|
818 | });
|
819 |
|
820 | var read = function read(get) {
|
821 | var ref = get(refAtom);
|
822 | var arr = get(arrAtom);
|
823 | var mapping = getMapping(arr, ref.prev);
|
824 | ref.prev = arr;
|
825 | return mapping.atomList;
|
826 | };
|
827 |
|
828 | var write = function write(get, set, action) {
|
829 | if ('read' in action) {
|
830 | console.warn('atomToRemove is deprecated. use action with type');
|
831 | action = {
|
832 | type: 'remove',
|
833 | atom: action
|
834 | };
|
835 | }
|
836 |
|
837 | switch (action.type) {
|
838 | case 'remove':
|
839 | {
|
840 | var index = get(splittedAtom).indexOf(action.atom);
|
841 |
|
842 | if (index >= 0) {
|
843 | var arr = get(arrAtom);
|
844 | set(arrAtom, [].concat(arr.slice(0, index), arr.slice(index + 1)));
|
845 | }
|
846 |
|
847 | break;
|
848 | }
|
849 |
|
850 | case 'insert':
|
851 | {
|
852 | var _index = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length;
|
853 |
|
854 | if (_index >= 0) {
|
855 | var _arr = get(arrAtom);
|
856 |
|
857 | set(arrAtom, [].concat(_arr.slice(0, _index), [action.value], _arr.slice(_index)));
|
858 | }
|
859 |
|
860 | break;
|
861 | }
|
862 |
|
863 | case 'move':
|
864 | {
|
865 | var index1 = get(splittedAtom).indexOf(action.atom);
|
866 | var index2 = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length;
|
867 |
|
868 | if (index1 >= 0 && index2 >= 0) {
|
869 | var _arr2 = get(arrAtom);
|
870 |
|
871 | if (index1 < index2) {
|
872 | set(arrAtom, [].concat(_arr2.slice(0, index1), _arr2.slice(index1 + 1, index2), [_arr2[index1]], _arr2.slice(index2)));
|
873 | } else {
|
874 | set(arrAtom, [].concat(_arr2.slice(0, index2), [_arr2[index1]], _arr2.slice(index2, index1), _arr2.slice(index1 + 1)));
|
875 | }
|
876 | }
|
877 |
|
878 | break;
|
879 | }
|
880 | }
|
881 | };
|
882 |
|
883 | var splittedAtom = isWritable(arrAtom) ? jotai.atom(read, write) : jotai.atom(read);
|
884 | return splittedAtom;
|
885 | }, keyExtractor ? [arrAtom, keyExtractor] : [arrAtom]);
|
886 | }
|
887 |
|
888 | function atomWithDefault(getDefault) {
|
889 | var EMPTY = Symbol();
|
890 | var overwrittenAtom = jotai.atom(EMPTY);
|
891 | var anAtom = jotai.atom(function (get) {
|
892 | var overwritten = get(overwrittenAtom);
|
893 |
|
894 | if (overwritten !== EMPTY) {
|
895 | return overwritten;
|
896 | }
|
897 |
|
898 | return getDefault(get);
|
899 | }, function (get, set, update) {
|
900 | if (update === RESET) {
|
901 | return set(overwrittenAtom, EMPTY);
|
902 | }
|
903 |
|
904 | return set(overwrittenAtom, typeof update === 'function' ? update(get(anAtom)) : update);
|
905 | });
|
906 | return anAtom;
|
907 | }
|
908 |
|
909 | var memoizeAtom$1 = createMemoizeAtom();
|
910 | var emptyArrayAtom = jotai.atom(function () {
|
911 | return [];
|
912 | });
|
913 | function waitForAll(atoms) {
|
914 | var createAtom = function createAtom() {
|
915 | var unwrappedAtoms = unwrapAtoms(atoms);
|
916 | var derivedAtom = jotai.atom(function (get) {
|
917 | var promises = [];
|
918 | var values = unwrappedAtoms.map(function (anAtom, index) {
|
919 | try {
|
920 | return get(anAtom);
|
921 | } catch (e) {
|
922 | if (e instanceof Promise) {
|
923 | promises[index] = e;
|
924 | } else {
|
925 | throw e;
|
926 | }
|
927 | }
|
928 | });
|
929 |
|
930 | if (promises.length) {
|
931 | throw Promise.all(promises);
|
932 | }
|
933 |
|
934 | return wrapResults(atoms, values);
|
935 | });
|
936 | return derivedAtom;
|
937 | };
|
938 |
|
939 | if (Array.isArray(atoms)) {
|
940 | if (atoms.length) {
|
941 | return memoizeAtom$1(createAtom, atoms);
|
942 | }
|
943 |
|
944 | return emptyArrayAtom;
|
945 | }
|
946 |
|
947 | return createAtom();
|
948 | }
|
949 |
|
950 | var unwrapAtoms = function unwrapAtoms(atoms) {
|
951 | return Array.isArray(atoms) ? atoms : Object.getOwnPropertyNames(atoms).map(function (key) {
|
952 | return atoms[key];
|
953 | });
|
954 | };
|
955 |
|
956 | var wrapResults = function wrapResults(atoms, results) {
|
957 | return Array.isArray(atoms) ? results : Object.getOwnPropertyNames(atoms).reduce(function (out, key, idx) {
|
958 | var _extends2;
|
959 |
|
960 | return _extends({}, out, (_extends2 = {}, _extends2[key] = results[idx], _extends2));
|
961 | }, {});
|
962 | };
|
963 |
|
964 | var NO_STORAGE_VALUE = Symbol();
|
965 | function createJSONStorage(getStringStorage) {
|
966 | var lastStr;
|
967 | var lastValue;
|
968 | var storage = {
|
969 | getItem: function getItem(key) {
|
970 | var _getStringStorage$get, _getStringStorage;
|
971 |
|
972 | var parse = function parse(str) {
|
973 | str = str || '';
|
974 |
|
975 | if (lastStr !== str) {
|
976 | try {
|
977 | lastValue = JSON.parse(str);
|
978 | } catch (_unused) {
|
979 | return NO_STORAGE_VALUE;
|
980 | }
|
981 |
|
982 | lastStr = str;
|
983 | }
|
984 |
|
985 | return lastValue;
|
986 | };
|
987 |
|
988 | var str = (_getStringStorage$get = (_getStringStorage = getStringStorage()) == null ? void 0 : _getStringStorage.getItem(key)) != null ? _getStringStorage$get : null;
|
989 |
|
990 | if (str instanceof Promise) {
|
991 | return str.then(parse);
|
992 | }
|
993 |
|
994 | return parse(str);
|
995 | },
|
996 | setItem: function setItem(key, newValue) {
|
997 | var _getStringStorage2;
|
998 |
|
999 | return (_getStringStorage2 = getStringStorage()) == null ? void 0 : _getStringStorage2.setItem(key, JSON.stringify(newValue));
|
1000 | },
|
1001 | removeItem: function removeItem(key) {
|
1002 | var _getStringStorage3;
|
1003 |
|
1004 | return (_getStringStorage3 = getStringStorage()) == null ? void 0 : _getStringStorage3.removeItem(key);
|
1005 | }
|
1006 | };
|
1007 |
|
1008 | if (typeof window !== 'undefined' && typeof window.addEventListener === 'function') {
|
1009 | storage.subscribe = function (key, callback) {
|
1010 | var storageEventCallback = function storageEventCallback(e) {
|
1011 | if (e.key === key && e.newValue) {
|
1012 | callback(JSON.parse(e.newValue));
|
1013 | }
|
1014 | };
|
1015 |
|
1016 | window.addEventListener('storage', storageEventCallback);
|
1017 | return function () {
|
1018 | window.removeEventListener('storage', storageEventCallback);
|
1019 | };
|
1020 | };
|
1021 | }
|
1022 |
|
1023 | return storage;
|
1024 | }
|
1025 | var defaultStorage = createJSONStorage(function () {
|
1026 | return typeof window !== 'undefined' ? window.localStorage : undefined;
|
1027 | });
|
1028 | function atomWithStorage(key, initialValue, storage) {
|
1029 | if (storage === void 0) {
|
1030 | storage = defaultStorage;
|
1031 | }
|
1032 |
|
1033 | var getInitialValue = function getInitialValue() {
|
1034 | var value = storage.getItem(key);
|
1035 |
|
1036 | if (value instanceof Promise) {
|
1037 | return value.then(function (v) {
|
1038 | return v === NO_STORAGE_VALUE ? initialValue : v;
|
1039 | });
|
1040 | }
|
1041 |
|
1042 | return value === NO_STORAGE_VALUE ? initialValue : value;
|
1043 | };
|
1044 |
|
1045 | var baseAtom = jotai.atom(storage.delayInit ? initialValue : getInitialValue());
|
1046 |
|
1047 | baseAtom.onMount = function (setAtom) {
|
1048 | var unsub;
|
1049 |
|
1050 | if (storage.subscribe) {
|
1051 | unsub = storage.subscribe(key, setAtom);
|
1052 | setAtom(getInitialValue());
|
1053 | }
|
1054 |
|
1055 | if (storage.delayInit) {
|
1056 | var _value = getInitialValue();
|
1057 |
|
1058 | if (_value instanceof Promise) {
|
1059 | _value.then(setAtom);
|
1060 | } else {
|
1061 | setAtom(_value);
|
1062 | }
|
1063 | }
|
1064 |
|
1065 | return unsub;
|
1066 | };
|
1067 |
|
1068 | var anAtom = jotai.atom(function (get) {
|
1069 | return get(baseAtom);
|
1070 | }, function (get, set, update) {
|
1071 | var nextValue = typeof update === 'function' ? update(get(baseAtom)) : update;
|
1072 |
|
1073 | if (nextValue === RESET) {
|
1074 | set(baseAtom, initialValue);
|
1075 | return storage.removeItem(key);
|
1076 | }
|
1077 |
|
1078 | set(baseAtom, nextValue);
|
1079 | return storage.setItem(key, nextValue);
|
1080 | });
|
1081 | return anAtom;
|
1082 | }
|
1083 | function atomWithHash(key, initialValue, options) {
|
1084 | var serialize = (options == null ? void 0 : options.serialize) || JSON.stringify;
|
1085 |
|
1086 | var deserialize = (options == null ? void 0 : options.deserialize) || function (str) {
|
1087 | try {
|
1088 | return JSON.parse(str || '');
|
1089 | } catch (_unused2) {
|
1090 | return NO_STORAGE_VALUE;
|
1091 | }
|
1092 | };
|
1093 |
|
1094 | var _subscribe = (options == null ? void 0 : options.subscribe) || function (callback) {
|
1095 | window.addEventListener('hashchange', callback);
|
1096 | return function () {
|
1097 | window.removeEventListener('hashchange', callback);
|
1098 | };
|
1099 | };
|
1100 |
|
1101 | var hashStorage = _extends({
|
1102 | getItem: function getItem(key) {
|
1103 | if (typeof location === 'undefined') {
|
1104 | return NO_STORAGE_VALUE;
|
1105 | }
|
1106 |
|
1107 | var searchParams = new URLSearchParams(location.hash.slice(1));
|
1108 | var storedValue = searchParams.get(key);
|
1109 | return deserialize(storedValue);
|
1110 | },
|
1111 | setItem: function setItem(key, newValue) {
|
1112 | var searchParams = new URLSearchParams(location.hash.slice(1));
|
1113 | searchParams.set(key, serialize(newValue));
|
1114 |
|
1115 | if (options != null && options.replaceState) {
|
1116 | history.replaceState(null, '', '#' + searchParams.toString());
|
1117 | } else {
|
1118 | location.hash = searchParams.toString();
|
1119 | }
|
1120 | },
|
1121 | removeItem: function removeItem(key) {
|
1122 | var searchParams = new URLSearchParams(location.hash.slice(1));
|
1123 | searchParams.delete(key);
|
1124 |
|
1125 | if (options != null && options.replaceState) {
|
1126 | history.replaceState(null, '', '#' + searchParams.toString());
|
1127 | } else {
|
1128 | location.hash = searchParams.toString();
|
1129 | }
|
1130 | }
|
1131 | }, (options == null ? void 0 : options.delayInit) && {
|
1132 | delayInit: true
|
1133 | }, {
|
1134 | subscribe: function subscribe(key, setValue) {
|
1135 | var callback = function callback() {
|
1136 | var searchParams = new URLSearchParams(location.hash.slice(1));
|
1137 | var str = searchParams.get(key);
|
1138 |
|
1139 | if (str !== null) {
|
1140 | setValue(deserialize(str));
|
1141 | } else {
|
1142 | setValue(initialValue);
|
1143 | }
|
1144 | };
|
1145 |
|
1146 | return _subscribe(callback);
|
1147 | }
|
1148 | });
|
1149 |
|
1150 | return atomWithStorage(key, initialValue, hashStorage);
|
1151 | }
|
1152 |
|
1153 | function atomWithObservable(createObservable, options) {
|
1154 | var observableResultAtom = jotai.atom(function (get) {
|
1155 | var _observable$Symbol$ob, _observable;
|
1156 |
|
1157 | var observable = createObservable(get);
|
1158 | var itself = (_observable$Symbol$ob = (_observable = observable)[Symbol.observable]) == null ? void 0 : _observable$Symbol$ob.call(_observable);
|
1159 |
|
1160 | if (itself) {
|
1161 | observable = itself;
|
1162 | }
|
1163 |
|
1164 | var EMPTY = Symbol();
|
1165 | var resolveEmittedInitialValue = null;
|
1166 | var initialEmittedValue = (options == null ? void 0 : options.initialValue) === undefined ? new Promise(function (resolve) {
|
1167 | resolveEmittedInitialValue = resolve;
|
1168 | }) : undefined;
|
1169 | var initialValueWasEmitted = false;
|
1170 | var emittedValueBeforeMount = EMPTY;
|
1171 | var isSync = true;
|
1172 |
|
1173 | var setData = function setData(data) {
|
1174 | if ((options == null ? void 0 : options.initialValue) === undefined && !initialValueWasEmitted) {
|
1175 | if (isSync) {
|
1176 | initialEmittedValue = data;
|
1177 | }
|
1178 |
|
1179 | resolveEmittedInitialValue == null ? void 0 : resolveEmittedInitialValue(data);
|
1180 | initialValueWasEmitted = true;
|
1181 | resolveEmittedInitialValue = null;
|
1182 | } else {
|
1183 | emittedValueBeforeMount = data;
|
1184 | }
|
1185 | };
|
1186 |
|
1187 | var dataListener = function dataListener(data) {
|
1188 | setData(data);
|
1189 | };
|
1190 |
|
1191 | var errorListener = function errorListener(error) {
|
1192 | setData(Promise.reject(error));
|
1193 | };
|
1194 |
|
1195 | var subscription = null;
|
1196 | var initialValue;
|
1197 |
|
1198 | if ((options == null ? void 0 : options.initialValue) !== undefined) {
|
1199 | initialValue = getInitialValue(options);
|
1200 | } else {
|
1201 | subscription = observable.subscribe(dataListener, errorListener);
|
1202 | initialValue = initialEmittedValue;
|
1203 | }
|
1204 |
|
1205 | isSync = false;
|
1206 | var dataAtom = jotai.atom(initialValue);
|
1207 |
|
1208 | dataAtom.onMount = function (update) {
|
1209 | setData = update;
|
1210 |
|
1211 | if (emittedValueBeforeMount !== EMPTY) {
|
1212 | update(emittedValueBeforeMount);
|
1213 | }
|
1214 |
|
1215 | if (!subscription) {
|
1216 | subscription = observable.subscribe(dataListener, errorListener);
|
1217 | }
|
1218 |
|
1219 | return function () {
|
1220 | var _subscription;
|
1221 |
|
1222 | (_subscription = subscription) == null ? void 0 : _subscription.unsubscribe();
|
1223 | subscription = null;
|
1224 | };
|
1225 | };
|
1226 |
|
1227 | return {
|
1228 | dataAtom: dataAtom,
|
1229 | observable: observable
|
1230 | };
|
1231 | });
|
1232 | var observableAtom = jotai.atom(function (get) {
|
1233 | var _get = get(observableResultAtom),
|
1234 | dataAtom = _get.dataAtom;
|
1235 |
|
1236 | return get(dataAtom);
|
1237 | }, function (get, set, data) {
|
1238 | var _get2 = get(observableResultAtom),
|
1239 | dataAtom = _get2.dataAtom,
|
1240 | observable = _get2.observable;
|
1241 |
|
1242 | if ('next' in observable) {
|
1243 | var subscription = null;
|
1244 |
|
1245 | var callback = function callback(data) {
|
1246 | var _subscription2;
|
1247 |
|
1248 | set(dataAtom, data);
|
1249 | (_subscription2 = subscription) == null ? void 0 : _subscription2.unsubscribe();
|
1250 | };
|
1251 |
|
1252 | subscription = observable.subscribe(callback);
|
1253 | observable.next(data);
|
1254 | } else {
|
1255 | throw new Error('observable is not subject');
|
1256 | }
|
1257 | });
|
1258 | return observableAtom;
|
1259 | }
|
1260 |
|
1261 | function getInitialValue(options) {
|
1262 | var initialValue = options.initialValue;
|
1263 | return initialValue instanceof Function ? initialValue() : initialValue;
|
1264 | }
|
1265 |
|
1266 | var hydratedMap = new WeakMap();
|
1267 | function useHydrateAtoms(values, scope) {
|
1268 | var ScopeContext = jotai.SECRET_INTERNAL_getScopeContext(scope);
|
1269 | var scopeContainer = react.useContext(ScopeContext);
|
1270 | var store = scopeContainer.s;
|
1271 | var hydratedSet = getHydratedSet(scopeContainer);
|
1272 | var tuplesToRestore = [];
|
1273 |
|
1274 | for (var _iterator = _createForOfIteratorHelperLoose(values), _step; !(_step = _iterator()).done;) {
|
1275 | var tuple = _step.value;
|
1276 | var atom = tuple[0];
|
1277 |
|
1278 | if (!hydratedSet.has(atom)) {
|
1279 | hydratedSet.add(atom);
|
1280 | tuplesToRestore.push(tuple);
|
1281 | }
|
1282 | }
|
1283 |
|
1284 | if (tuplesToRestore.length) {
|
1285 | store[RESTORE_ATOMS](tuplesToRestore);
|
1286 | }
|
1287 | }
|
1288 |
|
1289 | function getHydratedSet(scopeContainer) {
|
1290 | var hydratedSet = hydratedMap.get(scopeContainer);
|
1291 |
|
1292 | if (!hydratedSet) {
|
1293 | hydratedSet = new WeakSet();
|
1294 | hydratedMap.set(scopeContainer, hydratedSet);
|
1295 | }
|
1296 |
|
1297 | return hydratedSet;
|
1298 | }
|
1299 |
|
1300 | var memoizeAtom = createMemoizeAtom();
|
1301 | var LOADING = {
|
1302 | state: 'loading'
|
1303 | };
|
1304 | function loadable(anAtom) {
|
1305 | return memoizeAtom(function () {
|
1306 | var loadableAtomCache = new WeakMap();
|
1307 | var catchAtom = jotai.atom(function (get) {
|
1308 | var promise;
|
1309 |
|
1310 | try {
|
1311 | var data = get(anAtom);
|
1312 |
|
1313 | var _loadableAtom = jotai.atom({
|
1314 | state: 'hasData',
|
1315 | data: data
|
1316 | });
|
1317 |
|
1318 | return _loadableAtom;
|
1319 | } catch (error) {
|
1320 | if (error instanceof Promise) {
|
1321 | promise = error;
|
1322 | } else {
|
1323 | var _loadableAtom2 = jotai.atom({
|
1324 | state: 'hasError',
|
1325 | error: error
|
1326 | });
|
1327 |
|
1328 | return _loadableAtom2;
|
1329 | }
|
1330 | }
|
1331 |
|
1332 | var cached = loadableAtomCache.get(promise);
|
1333 |
|
1334 | if (cached) {
|
1335 | return cached;
|
1336 | }
|
1337 |
|
1338 | var loadableAtom = jotai.atom(LOADING, function () {
|
1339 | var _ref = _asyncToGenerator(_regeneratorRuntime().mark(function _callee(get, set) {
|
1340 | var _data;
|
1341 |
|
1342 | return _regeneratorRuntime().wrap(function _callee$(_context) {
|
1343 | while (1) {
|
1344 | switch (_context.prev = _context.next) {
|
1345 | case 0:
|
1346 | _context.prev = 0;
|
1347 | _context.next = 3;
|
1348 | return get(anAtom, {
|
1349 | unstable_promise: true
|
1350 | });
|
1351 |
|
1352 | case 3:
|
1353 | _data = _context.sent;
|
1354 | set(loadableAtom, {
|
1355 | state: 'hasData',
|
1356 | data: _data
|
1357 | });
|
1358 | _context.next = 10;
|
1359 | break;
|
1360 |
|
1361 | case 7:
|
1362 | _context.prev = 7;
|
1363 | _context.t0 = _context["catch"](0);
|
1364 | set(loadableAtom, {
|
1365 | state: 'hasError',
|
1366 | error: _context.t0
|
1367 | });
|
1368 |
|
1369 | case 10:
|
1370 | case "end":
|
1371 | return _context.stop();
|
1372 | }
|
1373 | }
|
1374 | }, _callee, null, [[0, 7]]);
|
1375 | }));
|
1376 |
|
1377 | return function (_x, _x2) {
|
1378 | return _ref.apply(this, arguments);
|
1379 | };
|
1380 | }());
|
1381 |
|
1382 | loadableAtom.onMount = function (init) {
|
1383 | init();
|
1384 | };
|
1385 |
|
1386 | loadableAtomCache.set(promise, loadableAtom);
|
1387 | return loadableAtom;
|
1388 | });
|
1389 | var derivedAtom = jotai.atom(function (get) {
|
1390 | var loadableAtom = get(catchAtom);
|
1391 | return get(loadableAtom);
|
1392 | });
|
1393 | return derivedAtom;
|
1394 | }, [anAtom]);
|
1395 | }
|
1396 |
|
1397 | function abortableAtom(read, write) {
|
1398 | return jotai.atom(function (get) {
|
1399 | var controller = new AbortController();
|
1400 | var promise = read(get, {
|
1401 | signal: controller.signal
|
1402 | });
|
1403 |
|
1404 | if (promise instanceof Promise) {
|
1405 | jotai.SECRET_INTERNAL_registerPromiseAbort(promise, function () {
|
1406 | return controller.abort();
|
1407 | });
|
1408 | }
|
1409 |
|
1410 | return promise;
|
1411 | }, write);
|
1412 | }
|
1413 |
|
1414 | Object.defineProperty(exports, 'useAtomValue', {
|
1415 | enumerable: true,
|
1416 | get: function () { return jotai.useAtomValue; }
|
1417 | });
|
1418 | Object.defineProperty(exports, 'useUpdateAtom', {
|
1419 | enumerable: true,
|
1420 | get: function () { return jotai.useSetAtom; }
|
1421 | });
|
1422 | exports.RESET = RESET;
|
1423 | exports.abortableAtom = abortableAtom;
|
1424 | exports.atomFamily = atomFamily;
|
1425 | exports.atomWithDefault = atomWithDefault;
|
1426 | exports.atomWithHash = atomWithHash;
|
1427 | exports.atomWithObservable = atomWithObservable;
|
1428 | exports.atomWithReducer = atomWithReducer;
|
1429 | exports.atomWithReset = atomWithReset;
|
1430 | exports.atomWithStorage = atomWithStorage;
|
1431 | exports.createJSONStorage = createJSONStorage;
|
1432 | exports.freezeAtom = freezeAtom;
|
1433 | exports.freezeAtomCreator = freezeAtomCreator;
|
1434 | exports.loadable = loadable;
|
1435 | exports.selectAtom = selectAtom;
|
1436 | exports.splitAtom = splitAtom;
|
1437 | exports.unstable_NO_STORAGE_VALUE = NO_STORAGE_VALUE;
|
1438 | exports.useAtomCallback = useAtomCallback;
|
1439 | exports.useHydrateAtoms = useHydrateAtoms;
|
1440 | exports.useReducerAtom = useReducerAtom;
|
1441 | exports.useResetAtom = useResetAtom;
|
1442 | exports.waitForAll = waitForAll;
|
1443 |
|
1444 | Object.defineProperty(exports, '__esModule', { value: true });
|
1445 |
|
1446 | }));
|