ccstate 4.0.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/debug/index.cjs CHANGED
@@ -163,307 +163,6 @@ function _possibleConstructorReturn(t, e) {
163
163
  if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
164
164
  return _assertThisInitialized(t);
165
165
  }
166
- function _regeneratorRuntime() {
167
- _regeneratorRuntime = function () {
168
- return e;
169
- };
170
- var t,
171
- e = {},
172
- r = Object.prototype,
173
- n = r.hasOwnProperty,
174
- o = Object.defineProperty || function (t, e, r) {
175
- t[e] = r.value;
176
- },
177
- i = "function" == typeof Symbol ? Symbol : {},
178
- a = i.iterator || "@@iterator",
179
- c = i.asyncIterator || "@@asyncIterator",
180
- u = i.toStringTag || "@@toStringTag";
181
- function define(t, e, r) {
182
- return Object.defineProperty(t, e, {
183
- value: r,
184
- enumerable: !0,
185
- configurable: !0,
186
- writable: !0
187
- }), t[e];
188
- }
189
- try {
190
- define({}, "");
191
- } catch (t) {
192
- define = function (t, e, r) {
193
- return t[e] = r;
194
- };
195
- }
196
- function wrap(t, e, r, n) {
197
- var i = e && e.prototype instanceof Generator ? e : Generator,
198
- a = Object.create(i.prototype),
199
- c = new Context(n || []);
200
- return o(a, "_invoke", {
201
- value: makeInvokeMethod(t, r, c)
202
- }), a;
203
- }
204
- function tryCatch(t, e, r) {
205
- try {
206
- return {
207
- type: "normal",
208
- arg: t.call(e, r)
209
- };
210
- } catch (t) {
211
- return {
212
- type: "throw",
213
- arg: t
214
- };
215
- }
216
- }
217
- e.wrap = wrap;
218
- var h = "suspendedStart",
219
- l = "suspendedYield",
220
- f = "executing",
221
- s = "completed",
222
- y = {};
223
- function Generator() {}
224
- function GeneratorFunction() {}
225
- function GeneratorFunctionPrototype() {}
226
- var p = {};
227
- define(p, a, function () {
228
- return this;
229
- });
230
- var d = Object.getPrototypeOf,
231
- v = d && d(d(values([])));
232
- v && v !== r && n.call(v, a) && (p = v);
233
- var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
234
- function defineIteratorMethods(t) {
235
- ["next", "throw", "return"].forEach(function (e) {
236
- define(t, e, function (t) {
237
- return this._invoke(e, t);
238
- });
239
- });
240
- }
241
- function AsyncIterator(t, e) {
242
- function invoke(r, o, i, a) {
243
- var c = tryCatch(t[r], t, o);
244
- if ("throw" !== c.type) {
245
- var u = c.arg,
246
- h = u.value;
247
- return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
248
- invoke("next", t, i, a);
249
- }, function (t) {
250
- invoke("throw", t, i, a);
251
- }) : e.resolve(h).then(function (t) {
252
- u.value = t, i(u);
253
- }, function (t) {
254
- return invoke("throw", t, i, a);
255
- });
256
- }
257
- a(c.arg);
258
- }
259
- var r;
260
- o(this, "_invoke", {
261
- value: function (t, n) {
262
- function callInvokeWithMethodAndArg() {
263
- return new e(function (e, r) {
264
- invoke(t, n, e, r);
265
- });
266
- }
267
- return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
268
- }
269
- });
270
- }
271
- function makeInvokeMethod(e, r, n) {
272
- var o = h;
273
- return function (i, a) {
274
- if (o === f) throw Error("Generator is already running");
275
- if (o === s) {
276
- if ("throw" === i) throw a;
277
- return {
278
- value: t,
279
- done: !0
280
- };
281
- }
282
- for (n.method = i, n.arg = a;;) {
283
- var c = n.delegate;
284
- if (c) {
285
- var u = maybeInvokeDelegate(c, n);
286
- if (u) {
287
- if (u === y) continue;
288
- return u;
289
- }
290
- }
291
- if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
292
- if (o === h) throw o = s, n.arg;
293
- n.dispatchException(n.arg);
294
- } else "return" === n.method && n.abrupt("return", n.arg);
295
- o = f;
296
- var p = tryCatch(e, r, n);
297
- if ("normal" === p.type) {
298
- if (o = n.done ? s : l, p.arg === y) continue;
299
- return {
300
- value: p.arg,
301
- done: n.done
302
- };
303
- }
304
- "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
305
- }
306
- };
307
- }
308
- function maybeInvokeDelegate(e, r) {
309
- var n = r.method,
310
- o = e.iterator[n];
311
- if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
312
- var i = tryCatch(o, e.iterator, r.arg);
313
- if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
314
- var a = i.arg;
315
- return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
316
- }
317
- function pushTryEntry(t) {
318
- var e = {
319
- tryLoc: t[0]
320
- };
321
- 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
322
- }
323
- function resetTryEntry(t) {
324
- var e = t.completion || {};
325
- e.type = "normal", delete e.arg, t.completion = e;
326
- }
327
- function Context(t) {
328
- this.tryEntries = [{
329
- tryLoc: "root"
330
- }], t.forEach(pushTryEntry, this), this.reset(!0);
331
- }
332
- function values(e) {
333
- if (e || "" === e) {
334
- var r = e[a];
335
- if (r) return r.call(e);
336
- if ("function" == typeof e.next) return e;
337
- if (!isNaN(e.length)) {
338
- var o = -1,
339
- i = function next() {
340
- for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
341
- return next.value = t, next.done = !0, next;
342
- };
343
- return i.next = i;
344
- }
345
- }
346
- throw new TypeError(typeof e + " is not iterable");
347
- }
348
- return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
349
- value: GeneratorFunctionPrototype,
350
- configurable: !0
351
- }), o(GeneratorFunctionPrototype, "constructor", {
352
- value: GeneratorFunction,
353
- configurable: !0
354
- }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
355
- var e = "function" == typeof t && t.constructor;
356
- return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
357
- }, e.mark = function (t) {
358
- return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
359
- }, e.awrap = function (t) {
360
- return {
361
- __await: t
362
- };
363
- }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
364
- return this;
365
- }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
366
- void 0 === i && (i = Promise);
367
- var a = new AsyncIterator(wrap(t, r, n, o), i);
368
- return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
369
- return t.done ? t.value : a.next();
370
- });
371
- }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
372
- return this;
373
- }), define(g, "toString", function () {
374
- return "[object Generator]";
375
- }), e.keys = function (t) {
376
- var e = Object(t),
377
- r = [];
378
- for (var n in e) r.push(n);
379
- return r.reverse(), function next() {
380
- for (; r.length;) {
381
- var t = r.pop();
382
- if (t in e) return next.value = t, next.done = !1, next;
383
- }
384
- return next.done = !0, next;
385
- };
386
- }, e.values = values, Context.prototype = {
387
- constructor: Context,
388
- reset: function (e) {
389
- if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
390
- },
391
- stop: function () {
392
- this.done = !0;
393
- var t = this.tryEntries[0].completion;
394
- if ("throw" === t.type) throw t.arg;
395
- return this.rval;
396
- },
397
- dispatchException: function (e) {
398
- if (this.done) throw e;
399
- var r = this;
400
- function handle(n, o) {
401
- return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
402
- }
403
- for (var o = this.tryEntries.length - 1; o >= 0; --o) {
404
- var i = this.tryEntries[o],
405
- a = i.completion;
406
- if ("root" === i.tryLoc) return handle("end");
407
- if (i.tryLoc <= this.prev) {
408
- var c = n.call(i, "catchLoc"),
409
- u = n.call(i, "finallyLoc");
410
- if (c && u) {
411
- if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
412
- if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
413
- } else if (c) {
414
- if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
415
- } else {
416
- if (!u) throw Error("try statement without catch or finally");
417
- if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
418
- }
419
- }
420
- }
421
- },
422
- abrupt: function (t, e) {
423
- for (var r = this.tryEntries.length - 1; r >= 0; --r) {
424
- var o = this.tryEntries[r];
425
- if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
426
- var i = o;
427
- break;
428
- }
429
- }
430
- i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
431
- var a = i ? i.completion : {};
432
- return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
433
- },
434
- complete: function (t, e) {
435
- if ("throw" === t.type) throw t.arg;
436
- return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
437
- },
438
- finish: function (t) {
439
- for (var e = this.tryEntries.length - 1; e >= 0; --e) {
440
- var r = this.tryEntries[e];
441
- if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
442
- }
443
- },
444
- catch: function (t) {
445
- for (var e = this.tryEntries.length - 1; e >= 0; --e) {
446
- var r = this.tryEntries[e];
447
- if (r.tryLoc === t) {
448
- var n = r.completion;
449
- if ("throw" === n.type) {
450
- var o = n.arg;
451
- resetTryEntry(r);
452
- }
453
- return o;
454
- }
455
- }
456
- throw Error("illegal catch attempt");
457
- },
458
- delegateYield: function (e, r, n) {
459
- return this.delegate = {
460
- iterator: values(e),
461
- resultName: r,
462
- nextLoc: n
463
- }, "next" === this.method && (this.arg = t), y;
464
- }
465
- }, e;
466
- }
467
166
  function _setPrototypeOf(t, e) {
468
167
  return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
469
168
  return t.__proto__ = e, t;
@@ -507,528 +206,614 @@ function _unsupportedIterableToArray(r, a) {
507
206
  }
508
207
  }
509
208
 
209
+ function withSetInterceptor(fn, writable$, interceptor) {
210
+ if (!interceptor) {
211
+ return fn();
212
+ }
213
+ var result = {
214
+ called: false
215
+ };
216
+ var wrappedFn = function wrappedFn() {
217
+ result = {
218
+ called: true,
219
+ data: fn()
220
+ };
221
+ return result.data;
222
+ };
223
+ for (var _len = arguments.length, args = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
224
+ args[_key - 3] = arguments[_key];
225
+ }
226
+ if ('write' in writable$) {
227
+ interceptor.apply(void 0, [writable$, wrappedFn].concat(_toConsumableArray(args)));
228
+ } else {
229
+ interceptor(writable$, wrappedFn, args[0]);
230
+ }
231
+ if (!result.called) {
232
+ throw new Error('interceptor must call fn sync');
233
+ }
234
+ return result.data;
235
+ }
236
+ function withGetInterceptor(fn, signal, interceptor) {
237
+ if (!interceptor) {
238
+ return fn();
239
+ }
240
+ var result = {
241
+ called: false
242
+ };
243
+ interceptor(signal, function () {
244
+ result = {
245
+ called: true,
246
+ data: fn()
247
+ };
248
+ return result.data;
249
+ });
250
+ if (!result.called) {
251
+ throw new Error('interceptor must call fn sync');
252
+ }
253
+ return result.data;
254
+ }
255
+ function withComputedInterceptor(fn, signal, interceptor) {
256
+ if (!interceptor) {
257
+ return fn();
258
+ }
259
+ var result = {
260
+ called: false
261
+ };
262
+ interceptor(signal, function () {
263
+ result = {
264
+ called: true,
265
+ data: fn()
266
+ };
267
+ return result.data.val;
268
+ });
269
+ if (!result.called) {
270
+ throw new Error('interceptor must call fn sync');
271
+ }
272
+ return result.data;
273
+ }
274
+ function withGeValInterceptor(fn, signal, interceptor) {
275
+ if (!interceptor) {
276
+ return fn();
277
+ }
278
+ var result = {
279
+ called: false
280
+ };
281
+ interceptor(signal, function () {
282
+ result = {
283
+ called: true,
284
+ data: fn()
285
+ };
286
+ return result.data;
287
+ });
288
+ if (!result.called) {
289
+ throw new Error('interceptor must call fn sync');
290
+ }
291
+ return result.data;
292
+ }
293
+ function withSubInterceptor(fn, signal, callback$, interceptor) {
294
+ if (!interceptor) {
295
+ fn();
296
+ return;
297
+ }
298
+ var result = {
299
+ called: false
300
+ };
301
+ interceptor(signal, callback$, function () {
302
+ result = {
303
+ called: true,
304
+ data: undefined
305
+ };
306
+ fn();
307
+ });
308
+ if (!result.called) {
309
+ throw new Error('interceptor must call fn sync');
310
+ }
311
+ }
312
+ function withUnsubInterceptor(fn, signal, callback$, interceptor) {
313
+ if (!interceptor) {
314
+ fn();
315
+ return;
316
+ }
317
+ var result = {
318
+ called: false
319
+ };
320
+ interceptor(signal, callback$, function () {
321
+ result = {
322
+ called: true,
323
+ data: undefined
324
+ };
325
+ fn();
326
+ });
327
+ if (!result.called) {
328
+ throw new Error('interceptor must call fn sync');
329
+ }
330
+ }
331
+ function withNotifyInterceptor(fn, callback$, interceptor) {
332
+ if (!interceptor) {
333
+ return fn();
334
+ }
335
+ var result = {
336
+ called: false
337
+ };
338
+ interceptor(callback$, function () {
339
+ result = {
340
+ called: true,
341
+ data: fn()
342
+ };
343
+ return result.data;
344
+ });
345
+ if (!result.called) {
346
+ throw new Error('interceptor must call fn sync');
347
+ }
348
+ return result.data;
349
+ }
350
+
510
351
  function canReadAsCompute(atom) {
511
352
  return 'read' in atom;
512
353
  }
513
354
  function isComputedState(state) {
514
355
  return 'dependencies' in state;
515
356
  }
516
- var AtomManager = /*#__PURE__*/function () {
517
- function AtomManager(options) {
518
- var _this = this;
519
- _classCallCheck(this, AtomManager);
520
- _defineProperty(this, "atomStateMap", new WeakMap());
521
- _defineProperty(this, "tryGetCachedState", function (atom, ignoreMounted) {
522
- var atomState = _this.atomStateMap.get(atom);
523
- if (!atomState) {
524
- return undefined;
525
- }
526
- if (atomState.mounted && !ignoreMounted) {
527
- return atomState;
528
- }
529
- var _iterator = _createForOfIteratorHelper(atomState.dependencies.entries()),
530
- _step;
531
- try {
532
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
533
- var _step$value = _slicedToArray(_step.value, 2),
534
- dep = _step$value[0],
535
- epoch = _step$value[1];
536
- var depState = _this.readAtomState(dep);
537
- if (depState.epoch !== epoch) {
538
- return undefined;
539
- }
540
- }
541
- } catch (err) {
542
- _iterator.e(err);
543
- } finally {
544
- _iterator.f();
545
- }
546
- return atomState;
547
- });
548
- this.options = options;
549
- }
550
- return _createClass(AtomManager, [{
551
- key: "readComputedAtom",
552
- value: function readComputedAtom(atom) {
553
- var _this$options,
554
- _this2 = this;
555
- var ignoreMounted = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
556
- var cachedState = this.tryGetCachedState(atom, ignoreMounted);
557
- if (cachedState) {
558
- return cachedState;
559
- }
560
- var computedInterceptor = (_this$options = this.options) === null || _this$options === void 0 || (_this$options = _this$options.interceptor) === null || _this$options === void 0 ? void 0 : _this$options.computed;
561
- if (!computedInterceptor) {
562
- return this.computeComputedAtom(atom);
563
- }
564
- var result = {
565
- called: false
566
- };
567
- computedInterceptor(atom, function () {
568
- result = {
569
- called: true,
570
- data: _this2.computeComputedAtom(atom)
571
- };
572
- return result.data.val;
573
- });
574
- if (!result.called) {
575
- throw new Error('interceptor must call fn sync');
357
+
358
+ function checkEpoch(readComputed, computedState, context, mutation) {
359
+ var _iterator = _createForOfIteratorHelper(computedState.dependencies.entries()),
360
+ _step;
361
+ try {
362
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
363
+ var _context$stateMap$get;
364
+ var _step$value = _slicedToArray(_step.value, 2),
365
+ dep = _step$value[0],
366
+ epoch = _step$value[1];
367
+ var depEpoch = canReadAsCompute(dep) ? readComputed(dep, context, mutation).epoch : (_context$stateMap$get = context.stateMap.get(dep)) === null || _context$stateMap$get === void 0 ? void 0 : _context$stateMap$get.epoch;
368
+ if (depEpoch !== epoch) {
369
+ return false;
576
370
  }
577
- return result.data;
578
371
  }
372
+ } catch (err) {
373
+ _iterator.e(err);
374
+ } finally {
375
+ _iterator.f();
376
+ }
377
+ return true;
378
+ }
379
+ function tryGetCached(readComputed, computed$, context, mutation) {
380
+ var signalState = context.stateMap.get(computed$);
381
+ if (!signalState) {
382
+ return undefined;
383
+ }
384
+ if (mutation !== null && mutation !== void 0 && mutation.dirtyMarkers.has(computed$.id)) {
385
+ return undefined;
386
+ }
387
+ if (signalState.mounted) {
388
+ return signalState;
389
+ }
390
+ if (checkEpoch(readComputed, signalState, context, mutation)) {
391
+ return signalState;
392
+ }
393
+ return undefined;
394
+ }
395
+ function wrapGet(readSignal, mount, callerComputed$, callerState, context, mutation) {
396
+ var readDeps = new Map();
397
+ return [function (dep$) {
398
+ var depState = readSignal(dep$, context, mutation);
399
+ if (callerState.dependencies === readDeps) {
400
+ readDeps.set(dep$, depState.epoch);
401
+ var callerMounted = !!callerState.mounted;
402
+ if (callerMounted && !depState.mounted) {
403
+ mount(dep$, context, mutation).readDepts.add(callerComputed$);
404
+ } else if (callerMounted && depState.mounted) {
405
+ depState.mounted.readDepts.add(callerComputed$);
406
+ }
407
+ }
408
+ return depState.val;
409
+ }, readDeps];
410
+ }
411
+ function getOrInitComputedState(computed$, context) {
412
+ var computedState = context.stateMap.get(computed$);
413
+ if (!computedState) {
414
+ computedState = {
415
+ dependencies: new Map(),
416
+ epoch: -1
417
+ };
418
+ context.stateMap.set(computed$, computedState);
419
+ }
420
+ return computedState;
421
+ }
422
+ function cleanupMissingDependencies(unmount, computed$, lastDeps, currDeps, context, mutation) {
423
+ var _iterator2 = _createForOfIteratorHelper(lastDeps.keys()),
424
+ _step2;
425
+ try {
426
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
427
+ var key = _step2.value;
428
+ if (!currDeps.has(key)) {
429
+ var _depState$mounted;
430
+ var depState = context.stateMap.get(key);
431
+ depState === null || depState === void 0 || (_depState$mounted = depState.mounted) === null || _depState$mounted === void 0 || _depState$mounted.readDepts["delete"](computed$);
432
+ unmount(key, context, mutation);
433
+ }
434
+ }
435
+ } catch (err) {
436
+ _iterator2.e(err);
437
+ } finally {
438
+ _iterator2.f();
439
+ }
440
+ }
441
+ function evaluateComputed(readSignal, mount, unmount, computed$, context, mutation) {
442
+ var computedState = getOrInitComputedState(computed$, context);
443
+ var lastDeps = computedState.dependencies;
444
+ var _wrapGet = wrapGet(readSignal, mount, computed$, computedState, context, mutation),
445
+ _wrapGet2 = _slicedToArray(_wrapGet, 2),
446
+ _get = _wrapGet2[0],
447
+ dependencies = _wrapGet2[1];
448
+ computedState.dependencies = dependencies;
449
+ var evalVal = computed$.read(function (depAtom) {
450
+ var _context$interceptor;
451
+ return withGeValInterceptor(function () {
452
+ return _get(depAtom);
453
+ }, depAtom, (_context$interceptor = context.interceptor) === null || _context$interceptor === void 0 ? void 0 : _context$interceptor.get);
579
454
  }, {
580
- key: "computeComputedAtom",
581
- value: function computeComputedAtom(atom) {
582
- var _this3 = this,
583
- _this$options2;
584
- var self = atom;
585
- var atomState = this.atomStateMap.get(self);
586
- if (!atomState) {
587
- atomState = {
588
- dependencies: new Map(),
589
- epoch: -1
590
- };
591
- this.atomStateMap.set(self, atomState);
592
- }
593
- var lastDeps = atomState.dependencies;
594
- var readDeps = new Map();
595
- atomState.dependencies = readDeps;
596
- var wrappedGet = function wrappedGet(depAtom) {
597
- var depState = _this3.readAtomState(depAtom);
455
+ get signal() {
456
+ var _computedState$abortC, _computed$$debugLabel;
457
+ (_computedState$abortC = computedState.abortController) === null || _computedState$abortC === void 0 || _computedState$abortC.abort("abort ".concat((_computed$$debugLabel = computed$.debugLabel) !== null && _computed$$debugLabel !== void 0 ? _computed$$debugLabel : 'anonymous', " atom"));
458
+ computedState.abortController = new AbortController();
459
+ return computedState.abortController.signal;
460
+ }
461
+ });
462
+ cleanupMissingDependencies(unmount, computed$, lastDeps, dependencies, context, mutation);
463
+ computedState.val = evalVal;
464
+ computedState.epoch += 1;
465
+ return computedState;
466
+ }
598
467
 
599
- // get 可能发生在异步过程中,当重复调用时,只有最新的 get 过程会修改 deps
600
- if (atomState.dependencies === readDeps) {
601
- readDeps.set(depAtom, depState.epoch);
602
- var selfMounted = !!atomState.mounted;
603
- if (selfMounted && !depState.mounted) {
604
- _this3.mount(depAtom).readDepts.add(self);
605
- } else if (selfMounted && depState.mounted) {
606
- depState.mounted.readDepts.add(self);
468
+ function pushDirtyMarkers(signalState, context, mutation) {
469
+ var _signalState$mounted$, _signalState$mounted;
470
+ var queue = Array.from((_signalState$mounted$ = (_signalState$mounted = signalState.mounted) === null || _signalState$mounted === void 0 ? void 0 : _signalState$mounted.readDepts) !== null && _signalState$mounted$ !== void 0 ? _signalState$mounted$ : []);
471
+ while (queue.length > 0) {
472
+ var nextQueue = [];
473
+ var _iterator = _createForOfIteratorHelper(queue),
474
+ _step;
475
+ try {
476
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
477
+ var computed$ = _step.value;
478
+ mutation.dirtyMarkers.add(computed$.id);
479
+ var computedState = context.stateMap.get(computed$);
480
+ // This computed$ is read from other computed$'s readDepts, so it must not be null and must have mounted
481
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
482
+ var _iterator2 = _createForOfIteratorHelper(computedState.mounted.readDepts),
483
+ _step2;
484
+ try {
485
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
486
+ var dep = _step2.value;
487
+ nextQueue.push(dep);
607
488
  }
489
+ } catch (err) {
490
+ _iterator2.e(err);
491
+ } finally {
492
+ _iterator2.f();
608
493
  }
609
- return depState.val;
610
- };
611
- var getInterceptor = (_this$options2 = this.options) === null || _this$options2 === void 0 || (_this$options2 = _this$options2.interceptor) === null || _this$options2 === void 0 ? void 0 : _this$options2.get;
612
- var ret = self.read(function (depAtom) {
613
- if (!getInterceptor) {
614
- return wrappedGet(depAtom);
615
- }
616
- var result = {
617
- called: false
618
- };
619
- var fn = function fn() {
620
- result = {
621
- called: true,
622
- data: wrappedGet(depAtom)
623
- };
624
- return result.data;
625
- };
626
- getInterceptor(depAtom, fn);
627
- if (!result.called) {
628
- throw new Error('interceptor must call fn sync');
629
- }
630
- return result.data;
631
- }, {
632
- get signal() {
633
- var _atomState$abortContr, _self$debugLabel;
634
- (_atomState$abortContr = atomState.abortController) === null || _atomState$abortContr === void 0 || _atomState$abortContr.abort("abort ".concat((_self$debugLabel = self.debugLabel) !== null && _self$debugLabel !== void 0 ? _self$debugLabel : 'anonymous', " atom"));
635
- atomState.abortController = new AbortController();
636
- return atomState.abortController.signal;
637
- }
638
- });
639
- if (atomState.val !== ret) {
640
- atomState.val = ret;
641
- atomState.epoch += 1;
642
494
  }
643
- var _iterator2 = _createForOfIteratorHelper(lastDeps.keys()),
644
- _step2;
645
- try {
646
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
647
- var key = _step2.value;
648
- if (!readDeps.has(key)) {
649
- var depState = this.atomStateMap.get(key);
650
- if (depState !== null && depState !== void 0 && depState.mounted) {
651
- depState.mounted.readDepts["delete"](self);
652
- this.tryUnmount(key);
495
+ } catch (err) {
496
+ _iterator.e(err);
497
+ } finally {
498
+ _iterator.f();
499
+ }
500
+ queue = nextQueue;
501
+ }
502
+ }
503
+ function pullEvaluate(readComputed, signalState, context, mutation) {
504
+ var _signalState$mounted$2, _signalState$mounted2, _signalState$mounted$3, _signalState$mounted3;
505
+ var queue = Array.from((_signalState$mounted$2 = (_signalState$mounted2 = signalState.mounted) === null || _signalState$mounted2 === void 0 ? void 0 : _signalState$mounted2.readDepts) !== null && _signalState$mounted$2 !== void 0 ? _signalState$mounted$2 : []);
506
+ var _iterator3 = _createForOfIteratorHelper((_signalState$mounted$3 = (_signalState$mounted3 = signalState.mounted) === null || _signalState$mounted3 === void 0 ? void 0 : _signalState$mounted3.listeners) !== null && _signalState$mounted$3 !== void 0 ? _signalState$mounted$3 : []),
507
+ _step3;
508
+ try {
509
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
510
+ var _listener = _step3.value;
511
+ mutation.pendingListeners.add(_listener);
512
+ }
513
+ } catch (err) {
514
+ _iterator3.e(err);
515
+ } finally {
516
+ _iterator3.f();
517
+ }
518
+ while (queue.length > 0) {
519
+ var nextQueue = [];
520
+ var _iterator4 = _createForOfIteratorHelper(queue),
521
+ _step4;
522
+ try {
523
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
524
+ var _computedState$mounte, _computedState$mounte2;
525
+ var computed$ = _step4.value;
526
+ var computedState = readComputed(computed$, context, mutation);
527
+ if ((_computedState$mounte = computedState.mounted) !== null && _computedState$mounte !== void 0 && _computedState$mounte.listeners) {
528
+ var _iterator5 = _createForOfIteratorHelper(computedState.mounted.listeners),
529
+ _step5;
530
+ try {
531
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
532
+ var listener = _step5.value;
533
+ mutation.pendingListeners.add(listener);
653
534
  }
535
+ } catch (err) {
536
+ _iterator5.e(err);
537
+ } finally {
538
+ _iterator5.f();
654
539
  }
655
540
  }
656
- } catch (err) {
657
- _iterator2.e(err);
658
- } finally {
659
- _iterator2.f();
660
- }
661
- return atomState;
662
- }
663
- }, {
664
- key: "readStateAtom",
665
- value: function readStateAtom(atom) {
666
- var atomState = this.atomStateMap.get(atom);
667
- if (!atomState) {
668
- var initState = {
669
- val: atom.init,
670
- epoch: 0
671
- };
672
- this.atomStateMap.set(atom, initState);
673
- return initState;
674
- }
675
- return atomState;
676
- }
677
- }, {
678
- key: "readAtomState",
679
- value: function readAtomState(atom) {
680
- var ignoreMounted = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
681
- if (canReadAsCompute(atom)) {
682
- return this.readComputedAtom(atom, ignoreMounted);
683
- }
684
- return this.readStateAtom(atom);
685
- }
686
- }, {
687
- key: "tryGetMount",
688
- value: function tryGetMount(atom) {
689
- var _this$atomStateMap$ge;
690
- return (_this$atomStateMap$ge = this.atomStateMap.get(atom)) === null || _this$atomStateMap$ge === void 0 ? void 0 : _this$atomStateMap$ge.mounted;
691
- }
692
- }, {
693
- key: "mount",
694
- value: function mount(atom) {
695
- var _this$options3, _this$options3$mount, _atomState$mounted;
696
- var mounted = this.tryGetMount(atom);
697
- if (mounted) {
698
- return mounted;
699
- }
700
- (_this$options3 = this.options) === null || _this$options3 === void 0 || (_this$options3 = _this$options3.interceptor) === null || _this$options3 === void 0 || (_this$options3$mount = _this$options3.mount) === null || _this$options3$mount === void 0 || _this$options3$mount.call(_this$options3, atom);
701
- var atomState = this.readAtomState(atom);
702
- atomState.mounted = (_atomState$mounted = atomState.mounted) !== null && _atomState$mounted !== void 0 ? _atomState$mounted : {
703
- listeners: new Set(),
704
- readDepts: new Set()
705
- };
706
- if (isComputedState(atomState)) {
707
- for (var _i = 0, _Array$from = Array.from(atomState.dependencies); _i < _Array$from.length; _i++) {
708
- var _Array$from$_i = _slicedToArray(_Array$from[_i], 1),
709
- dep = _Array$from$_i[0];
710
- var _mounted = this.mount(dep);
711
- _mounted.readDepts.add(atom);
541
+ var readDepts = (_computedState$mounte2 = computedState.mounted) === null || _computedState$mounte2 === void 0 ? void 0 : _computedState$mounte2.readDepts;
542
+ if (readDepts) {
543
+ for (var _i = 0, _Array$from = Array.from(readDepts); _i < _Array$from.length; _i++) {
544
+ var dep = _Array$from[_i];
545
+ nextQueue.push(dep);
546
+ }
712
547
  }
713
548
  }
714
- return atomState.mounted;
549
+ } catch (err) {
550
+ _iterator4.e(err);
551
+ } finally {
552
+ _iterator4.f();
715
553
  }
716
- }, {
717
- key: "tryUnmount",
718
- value: function tryUnmount(atom) {
719
- var _this$options4, _this$options4$unmoun;
720
- var atomState = this.atomStateMap.get(atom);
721
- if (!(atomState !== null && atomState !== void 0 && atomState.mounted) || atomState.mounted.listeners.size || atomState.mounted.readDepts.size) {
722
- return;
723
- }
724
- (_this$options4 = this.options) === null || _this$options4 === void 0 || (_this$options4 = _this$options4.interceptor) === null || _this$options4 === void 0 || (_this$options4$unmoun = _this$options4.unmount) === null || _this$options4$unmoun === void 0 || _this$options4$unmoun.call(_this$options4, atom);
725
- if (isComputedState(atomState)) {
726
- for (var _i2 = 0, _Array$from2 = Array.from(atomState.dependencies); _i2 < _Array$from2.length; _i2++) {
727
- var _depState$mounted;
728
- var _Array$from2$_i = _slicedToArray(_Array$from2[_i2], 1),
729
- dep = _Array$from2$_i[0];
730
- var depState = this.readAtomState(dep);
731
- (_depState$mounted = depState.mounted) === null || _depState$mounted === void 0 || _depState$mounted.readDepts["delete"](atom);
732
- this.tryUnmount(dep);
554
+ queue = nextQueue;
555
+ }
556
+ }
557
+ function propagationChanges(readComputed, signalState, context, mutation) {
558
+ pushDirtyMarkers(signalState, context, mutation);
559
+ pullEvaluate(readComputed, signalState, context, mutation);
560
+ }
561
+ function innerSetState(readComputed, signal$, context, mutation, val) {
562
+ var _ref, _context$stateMap$get;
563
+ var newValue = typeof val === 'function' ? val((_ref = (_context$stateMap$get = context.stateMap.get(signal$)) === null || _context$stateMap$get === void 0 ? void 0 : _context$stateMap$get.val) !== null && _ref !== void 0 ? _ref : signal$.init) : val;
564
+ var signalState = context.stateMap.get(signal$);
565
+ if (!signalState) {
566
+ context.stateMap.set(signal$, {
567
+ val: newValue,
568
+ epoch: 0
569
+ });
570
+ return;
571
+ }
572
+ signalState.val = newValue;
573
+ signalState.epoch += 1;
574
+ propagationChanges(readComputed, signalState, context, mutation);
575
+ return undefined;
576
+ }
577
+ function set(readComputed, writable$, context, mutation) {
578
+ if ('read' in writable$) {
579
+ return;
580
+ }
581
+ for (var _len = arguments.length, args = new Array(_len > 4 ? _len - 4 : 0), _key = 4; _key < _len; _key++) {
582
+ args[_key - 4] = arguments[_key];
583
+ }
584
+ if ('write' in writable$) {
585
+ return writable$.write.apply(writable$, [mutation.visitor].concat(_toConsumableArray(args)));
586
+ }
587
+ innerSetState(readComputed, writable$, context, mutation, args[0]);
588
+ return;
589
+ }
590
+
591
+ /**
592
+ * Creates a mutation operation context. The Mutation remains unique throughout
593
+ * the mutation cycle and can track side effects produced by this mutation operation
594
+ *
595
+ * This tracking is implemented by coloring the visitor function, so the Mutation
596
+ * needs to wrap get & set functions and ensure that all get & set operations
597
+ * executed in the mutation context come from the same Mutation
598
+ *
599
+ * @param context
600
+ * @param get
601
+ * @param set
602
+ * @returns
603
+ */
604
+ function createMutation(context, _get, _set) {
605
+ var mutation = {
606
+ dirtyMarkers: new Set(),
607
+ pendingListeners: new Set(),
608
+ visitor: {
609
+ get: function get(signal$) {
610
+ return _get(signal$, context, mutation);
611
+ },
612
+ set: function set(signal$) {
613
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
614
+ args[_key2 - 1] = arguments[_key2];
733
615
  }
616
+ return _set.apply(void 0, [signal$, context].concat(args));
734
617
  }
735
- atomState.mounted = undefined;
736
- }
737
- }, {
738
- key: "inited",
739
- value: function inited(atom) {
740
- return this.atomStateMap.has(atom);
741
618
  }
742
- }]);
743
- }();
744
- var ListenerManager = /*#__PURE__*/function () {
745
- function ListenerManager() {
746
- _classCallCheck(this, ListenerManager);
747
- _defineProperty(this, "pendingListeners", new Set());
619
+ };
620
+ return mutation;
621
+ }
622
+
623
+ function readState(state, context) {
624
+ var atomState = context.stateMap.get(state);
625
+ if (!atomState) {
626
+ var initState = {
627
+ val: state.init,
628
+ epoch: 0
629
+ };
630
+ context.stateMap.set(state, initState);
631
+ return initState;
748
632
  }
749
- return _createClass(ListenerManager, [{
750
- key: "markPendingListeners",
751
- value: function markPendingListeners(atomManager, atom) {
752
- var queue = [atom];
753
- while (queue.length > 0) {
754
- var nextQueue = [];
755
- var _iterator3 = _createForOfIteratorHelper(queue),
756
- _step3;
757
- try {
758
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
759
- var _atomState$mounted2, _atomState$mounted3;
760
- var _atom = _step3.value;
761
- var atomState = atomManager.readAtomState(_atom, true);
762
- if ((_atomState$mounted2 = atomState.mounted) !== null && _atomState$mounted2 !== void 0 && _atomState$mounted2.listeners) {
763
- var _iterator4 = _createForOfIteratorHelper(atomState.mounted.listeners),
764
- _step4;
765
- try {
766
- for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
767
- var listener = _step4.value;
768
- this.pendingListeners.add(listener);
769
- }
770
- } catch (err) {
771
- _iterator4.e(err);
772
- } finally {
773
- _iterator4.f();
774
- }
775
- }
776
- var readDepts = (_atomState$mounted3 = atomState.mounted) === null || _atomState$mounted3 === void 0 ? void 0 : _atomState$mounted3.readDepts;
777
- if (readDepts) {
778
- for (var _i3 = 0, _Array$from3 = Array.from(readDepts); _i3 < _Array$from3.length; _i3++) {
779
- var dep = _Array$from3[_i3];
780
- nextQueue.push(dep);
781
- }
782
- }
783
- }
784
- } catch (err) {
785
- _iterator3.e(err);
786
- } finally {
787
- _iterator3.f();
633
+ return atomState;
634
+ }
635
+
636
+ function unmountComputedDependencies(computed$, computedState, context, mutation) {
637
+ for (var _i = 0, _Array$from = Array.from(computedState.dependencies); _i < _Array$from.length; _i++) {
638
+ var _context$stateMap$get;
639
+ var _Array$from$_i = _slicedToArray(_Array$from[_i], 1),
640
+ dep = _Array$from$_i[0];
641
+ (_context$stateMap$get = context.stateMap.get(dep)) === null || _context$stateMap$get === void 0 || (_context$stateMap$get = _context$stateMap$get.mounted) === null || _context$stateMap$get === void 0 || _context$stateMap$get.readDepts["delete"](computed$);
642
+ unmount(dep, context);
643
+ }
644
+ }
645
+ function mountComputedDependencies(readSignal, computed$, computedState, context, mutation) {
646
+ for (var _i2 = 0, _Array$from2 = Array.from(computedState.dependencies); _i2 < _Array$from2.length; _i2++) {
647
+ var _Array$from2$_i = _slicedToArray(_Array$from2[_i2], 1),
648
+ dep = _Array$from2$_i[0];
649
+ var mounted = mount$1(readSignal, dep, context, mutation);
650
+ mounted.readDepts.add(computed$);
651
+ }
652
+ }
653
+ function initMount(readSignal, signal$, context, mutation) {
654
+ var _context$interceptor, _context$interceptor$, _signalState$mounted;
655
+ (_context$interceptor = context.interceptor) === null || _context$interceptor === void 0 || (_context$interceptor$ = _context$interceptor.mount) === null || _context$interceptor$ === void 0 || _context$interceptor$.call(_context$interceptor, signal$);
656
+ var signalState = readSignal(signal$, context, mutation);
657
+ signalState.mounted = (_signalState$mounted = signalState.mounted) !== null && _signalState$mounted !== void 0 ? _signalState$mounted : {
658
+ listeners: new Set(),
659
+ readDepts: new Set()
660
+ };
661
+ if (isComputedState(signalState)) {
662
+ mountComputedDependencies(readSignal, signal$, signalState, context, mutation);
663
+ }
664
+ return signalState.mounted;
665
+ }
666
+ function mount$1(readSignal, signal$, context, mutation) {
667
+ var _context$stateMap$get2;
668
+ var mounted = (_context$stateMap$get2 = context.stateMap.get(signal$)) === null || _context$stateMap$get2 === void 0 ? void 0 : _context$stateMap$get2.mounted;
669
+ if (mounted) {
670
+ return mounted;
671
+ }
672
+ return initMount(readSignal, signal$, context, mutation);
673
+ }
674
+ function doUnmount(signal$, signalState, context, mutation) {
675
+ var _context$interceptor2, _context$interceptor3;
676
+ (_context$interceptor2 = context.interceptor) === null || _context$interceptor2 === void 0 || (_context$interceptor3 = _context$interceptor2.unmount) === null || _context$interceptor3 === void 0 || _context$interceptor3.call(_context$interceptor2, signal$);
677
+ if (isComputedState(signalState)) {
678
+ unmountComputedDependencies(signal$, signalState, context);
679
+ }
680
+ signalState.mounted = undefined;
681
+ }
682
+ function unmount(signal$, context, mutation) {
683
+ var signalState = context.stateMap.get(signal$);
684
+ if (!(signalState !== null && signalState !== void 0 && signalState.mounted) || signalState.mounted.listeners.size || signalState.mounted.readDepts.size) {
685
+ return;
686
+ }
687
+ doUnmount(signal$, signalState, context);
688
+ }
689
+ function subSingleSignal(readSignal, signal$, callback$, context, signal) {
690
+ var _context$interceptor5;
691
+ withSubInterceptor(function () {
692
+ var mounted = mount$1(readSignal, signal$, context);
693
+ mounted.listeners.add(callback$);
694
+ var unsub = function unsub() {
695
+ var _context$interceptor4;
696
+ withUnsubInterceptor(function () {
697
+ mounted.listeners["delete"](callback$);
698
+ if (mounted.readDepts.size === 0 && mounted.listeners.size === 0) {
699
+ unmount(signal$, context);
788
700
  }
789
- queue = nextQueue;
790
- }
701
+ }, signal$, callback$, (_context$interceptor4 = context.interceptor) === null || _context$interceptor4 === void 0 ? void 0 : _context$interceptor4.unsub);
702
+ };
703
+ signal.addEventListener('abort', unsub, {
704
+ once: true
705
+ });
706
+ }, signal$, callback$, (_context$interceptor5 = context.interceptor) === null || _context$interceptor5 === void 0 ? void 0 : _context$interceptor5.sub);
707
+ }
708
+ function notify(context, mutation) {
709
+ var pendingListeners = mutation.pendingListeners;
710
+ mutation.pendingListeners = new Set();
711
+ var _iterator = _createForOfIteratorHelper(pendingListeners),
712
+ _step;
713
+ try {
714
+ var _loop = function _loop() {
715
+ var _context$interceptor6;
716
+ var listener = _step.value;
717
+ withNotifyInterceptor(function () {
718
+ return listener.write(mutation.visitor);
719
+ }, listener, (_context$interceptor6 = context.interceptor) === null || _context$interceptor6 === void 0 ? void 0 : _context$interceptor6.notify);
720
+ };
721
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
722
+ _loop();
791
723
  }
792
- }, {
793
- key: "notify",
794
- value: /*#__PURE__*/_regeneratorRuntime().mark(function notify() {
795
- var pendingListeners, _iterator5, _step5, listener;
796
- return _regeneratorRuntime().wrap(function notify$(_context) {
797
- while (1) switch (_context.prev = _context.next) {
798
- case 0:
799
- pendingListeners = this.pendingListeners;
800
- this.pendingListeners = new Set();
801
- _iterator5 = _createForOfIteratorHelper(pendingListeners);
802
- _context.prev = 3;
803
- _iterator5.s();
804
- case 5:
805
- if ((_step5 = _iterator5.n()).done) {
806
- _context.next = 11;
807
- break;
808
- }
809
- listener = _step5.value;
810
- _context.next = 9;
811
- return listener;
812
- case 9:
813
- _context.next = 5;
814
- break;
815
- case 11:
816
- _context.next = 16;
817
- break;
818
- case 13:
819
- _context.prev = 13;
820
- _context.t0 = _context["catch"](3);
821
- _iterator5.e(_context.t0);
822
- case 16:
823
- _context.prev = 16;
824
- _iterator5.f();
825
- return _context.finish(16);
826
- case 19:
827
- case "end":
828
- return _context.stop();
829
- }
830
- }, notify, this, [[3, 13, 16, 19]]);
831
- })
832
- }]);
833
- }();
724
+ } catch (err) {
725
+ _iterator.e(err);
726
+ } finally {
727
+ _iterator.f();
728
+ }
729
+ }
834
730
 
731
+ var _readComputed = function readComputed(computed$, context, mutation) {
732
+ var _context$interceptor;
733
+ var cachedState = tryGetCached(_readComputed, computed$, context, mutation);
734
+ if (cachedState) {
735
+ return cachedState;
736
+ }
737
+ mutation === null || mutation === void 0 || mutation.dirtyMarkers["delete"](computed$.id);
738
+ return withComputedInterceptor(function () {
739
+ return evaluateComputed(readSignal, mount, unmount, computed$, context, mutation);
740
+ }, computed$, (_context$interceptor = context.interceptor) === null || _context$interceptor === void 0 ? void 0 : _context$interceptor.computed);
741
+ };
742
+ function readSignal(signal$, context, mutation) {
743
+ if (canReadAsCompute(signal$)) {
744
+ return _readComputed(signal$, context, mutation);
745
+ }
746
+ return readState(signal$, context);
747
+ }
748
+ function mount(signal$, context, mutation) {
749
+ return mount$1(readSignal, signal$, context, mutation);
750
+ }
751
+ function _sub(signals$, callback$, context, options) {
752
+ if (Array.isArray(signals$) && signals$.length === 0) {
753
+ return function () {
754
+ return void 0;
755
+ };
756
+ }
757
+ var controller = new AbortController();
758
+ var signal = options !== null && options !== void 0 && options.signal ? AbortSignal.any([controller.signal, options.signal]) : controller.signal;
759
+ if (!Array.isArray(signals$)) {
760
+ subSingleSignal(readSignal, signals$, callback$, context, signal);
761
+ } else {
762
+ signals$.forEach(function (atom) {
763
+ subSingleSignal(readSignal, atom, callback$, context, signal);
764
+ });
765
+ }
766
+ return function () {
767
+ controller.abort();
768
+ };
769
+ }
770
+ var get = function get(signal, context, mutation) {
771
+ var _context$interceptor2;
772
+ return withGetInterceptor(function () {
773
+ return readSignal(signal, context, mutation).val;
774
+ }, signal, (_context$interceptor2 = context.interceptor) === null || _context$interceptor2 === void 0 ? void 0 : _context$interceptor2.get);
775
+ };
776
+ var _set = function set$1(atom, context) {
777
+ var _context$interceptor3;
778
+ for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
779
+ args[_key - 2] = arguments[_key];
780
+ }
781
+ return withSetInterceptor.apply(void 0, [function () {
782
+ var mutation = createMutation(context, get, _set);
783
+ var ret;
784
+ try {
785
+ ret = set.apply(void 0, [_readComputed, atom, context, mutation].concat(args));
786
+ } finally {
787
+ notify(context, mutation);
788
+ }
789
+ return ret;
790
+ }, atom, (_context$interceptor3 = context.interceptor) === null || _context$interceptor3 === void 0 ? void 0 : _context$interceptor3.set].concat(args));
791
+ };
835
792
  var StoreImpl = /*#__PURE__*/function () {
836
- function StoreImpl(atomManager, listenerManager, options) {
837
- var _this = this;
793
+ function StoreImpl(options) {
794
+ var _this = this,
795
+ _this$options;
838
796
  _classCallCheck(this, StoreImpl);
839
- _defineProperty(this, "innerSet", function (atom) {
840
- if ('read' in atom) {
841
- return;
842
- }
843
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
844
- args[_key - 1] = arguments[_key];
845
- }
846
- if ('write' in atom) {
847
- var ret = atom.write.apply(atom, [{
848
- get: _this.get,
849
- set: _this.set
850
- }].concat(_toConsumableArray(args)));
851
- return ret;
852
- }
853
- var newValue = typeof args[0] === 'function' ? args[0](_this.atomManager.readAtomState(atom).val) : args[0];
854
- if (!_this.atomManager.inited(atom)) {
855
- _this.atomManager.readAtomState(atom).val = newValue;
856
- _this.listenerManager.markPendingListeners(_this.atomManager, atom);
857
- return;
858
- }
859
- var atomState = _this.atomManager.readAtomState(atom);
860
- atomState.val = newValue;
861
- atomState.epoch += 1;
862
- _this.listenerManager.markPendingListeners(_this.atomManager, atom);
863
- return undefined;
864
- });
797
+ _defineProperty(this, "stateMap", new WeakMap());
865
798
  _defineProperty(this, "get", function (atom) {
866
- var _this$options;
867
- if (!((_this$options = _this.options) !== null && _this$options !== void 0 && (_this$options = _this$options.interceptor) !== null && _this$options !== void 0 && _this$options.get)) {
868
- return _this.atomManager.readAtomState(atom).val;
869
- }
870
- var result = {
871
- called: false
872
- };
873
- var fnWithRet = function fnWithRet() {
874
- result = {
875
- called: true,
876
- data: _this.atomManager.readAtomState(atom).val
877
- };
878
- return result.data;
879
- };
880
- _this.options.interceptor.get(atom, fnWithRet);
881
- if (!result.called) {
882
- throw new Error('interceptor must call fn sync');
883
- }
884
- return result.data;
885
- });
886
- _defineProperty(this, "notify", function () {
887
- var _iterator = _createForOfIteratorHelper(_this.listenerManager.notify()),
888
- _step;
889
- try {
890
- var _loop = function _loop() {
891
- var _this$options2;
892
- var listener = _step.value;
893
- var notifyed = false;
894
- var fn = function fn() {
895
- notifyed = true;
896
- return listener.write({
897
- get: _this.get,
898
- set: _this.set
899
- });
900
- };
901
- if ((_this$options2 = _this.options) !== null && _this$options2 !== void 0 && (_this$options2 = _this$options2.interceptor) !== null && _this$options2 !== void 0 && _this$options2.notify) {
902
- _this.options.interceptor.notify(listener, fn);
903
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- notify must call fn sync
904
- if (!notifyed) {
905
- throw new Error('interceptor must call fn sync');
906
- }
907
- } else {
908
- fn();
909
- }
910
- };
911
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
912
- _loop();
913
- }
914
- } catch (err) {
915
- _iterator.e(err);
916
- } finally {
917
- _iterator.f();
918
- }
799
+ return get(atom, _this.context);
919
800
  });
920
801
  _defineProperty(this, "set", function (atom) {
921
- var _this$options3;
922
802
  for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
923
803
  args[_key2 - 1] = arguments[_key2];
924
804
  }
925
- var ret;
926
- var fn = function fn() {
927
- try {
928
- ret = _this.innerSet.apply(_this, [atom].concat(args));
929
- } finally {
930
- _this.notify();
931
- }
932
- return ret;
933
- };
934
- if ((_this$options3 = _this.options) !== null && _this$options3 !== void 0 && (_this$options3 = _this$options3.interceptor) !== null && _this$options3 !== void 0 && _this$options3.set) {
935
- if ('write' in atom) {
936
- var _this$options$interce;
937
- (_this$options$interce = _this.options.interceptor).set.apply(_this$options$interce, [atom, fn].concat(_toConsumableArray(args)));
938
- } else {
939
- _this.options.interceptor.set(atom, fn, args[0]);
940
- }
941
- } else {
942
- fn();
943
- }
944
- return ret;
805
+ return _set.apply(void 0, [atom, _this.context].concat(args));
945
806
  });
946
- this.atomManager = atomManager;
947
- this.listenerManager = listenerManager;
948
807
  this.options = options;
808
+ this.context = {
809
+ stateMap: this.stateMap,
810
+ interceptor: (_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.interceptor
811
+ };
949
812
  }
950
813
  return _createClass(StoreImpl, [{
951
- key: "_subSingleAtom",
952
- value: function _subSingleAtom(target$, cb$, options) {
953
- var _this2 = this,
954
- _this$options4;
955
- var unsub;
956
- var _fn2 = function fn() {
957
- var _options$signal2;
958
- var subscribed = true;
959
- var mounted = _this2.atomManager.mount(target$);
960
- mounted.listeners.add(cb$);
961
- unsub = function unsub() {
962
- var _this2$options;
963
- if (!subscribed) {
964
- return;
965
- }
966
- var _fn = function fn() {
967
- var _options$signal;
968
- subscribed = false;
969
- mounted.listeners["delete"](cb$);
970
- if (mounted.readDepts.size === 0 && mounted.listeners.size === 0) {
971
- _this2.atomManager.tryUnmount(target$);
972
- }
973
- options === null || options === void 0 || (_options$signal = options.signal) === null || _options$signal === void 0 || _options$signal.addEventListener('abort', _fn);
974
- };
975
- if ((_this2$options = _this2.options) !== null && _this2$options !== void 0 && (_this2$options = _this2$options.interceptor) !== null && _this2$options !== void 0 && _this2$options.unsub) {
976
- _this2.options.interceptor.unsub(target$, cb$, _fn);
977
-
978
- // subscribed should be false if interceptor called fn sync
979
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
980
- if (subscribed) {
981
- throw new Error('interceptor must call fn sync');
982
- }
983
- } else {
984
- _fn();
985
- }
986
- };
987
- options === null || options === void 0 || (_options$signal2 = options.signal) === null || _options$signal2 === void 0 || _options$signal2.addEventListener('abort', unsub);
988
- };
989
- if ((_this$options4 = this.options) !== null && _this$options4 !== void 0 && (_this$options4 = _this$options4.interceptor) !== null && _this$options4 !== void 0 && _this$options4.sub) {
990
- this.options.interceptor.sub(target$, cb$, _fn2);
991
- } else {
992
- _fn2();
993
- }
994
- if (!unsub) {
995
- throw new Error('interceptor must call fn sync');
996
- }
997
- return unsub;
998
- }
999
- }, {
1000
814
  key: "sub",
1001
815
  value: function sub(targets$, cb$, options) {
1002
- var _this3 = this;
1003
- if (Array.isArray(targets$) && targets$.length === 0) {
1004
- return function () {
1005
- return void 0;
1006
- };
1007
- }
1008
- if (Array.isArray(targets$) && targets$.length === 1) {
1009
- return this._subSingleAtom(targets$[0], cb$, options);
1010
- } else if (!Array.isArray(targets$)) {
1011
- return this._subSingleAtom(targets$, cb$, options);
1012
- }
1013
- var unsubscribes = new Set();
1014
- targets$.forEach(function (atom) {
1015
- unsubscribes.add(_this3._subSingleAtom(atom, cb$, options));
1016
- });
1017
- var unsub = function unsub() {
1018
- var _iterator2 = _createForOfIteratorHelper(unsubscribes),
1019
- _step2;
1020
- try {
1021
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
1022
- var unsubscribe = _step2.value;
1023
- unsubscribe();
1024
- }
1025
- } catch (err) {
1026
- _iterator2.e(err);
1027
- } finally {
1028
- _iterator2.f();
1029
- }
1030
- };
1031
- return unsub;
816
+ return _sub(targets$, cb$, this.context, options);
1032
817
  }
1033
818
  }]);
1034
819
  }();
@@ -1070,7 +855,10 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1070
855
  };
1071
856
  });
1072
857
  _defineProperty(_this, "getReadDependencies", function (atom) {
1073
- var atomState = _this.atomManager.readAtomState(atom);
858
+ var atomState = _this.context.stateMap.get(atom);
859
+ if (!atomState) {
860
+ return [atom];
861
+ }
1074
862
  if (!('dependencies' in atomState)) {
1075
863
  return [atom];
1076
864
  }
@@ -1082,7 +870,10 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1082
870
  });
1083
871
  _defineProperty(_this, "getReadDependents", function (atom) {
1084
872
  var _atomState$mounted$re, _atomState$mounted;
1085
- var atomState = _this.atomManager.readAtomState(atom);
873
+ var atomState = _this.context.stateMap.get(atom);
874
+ if (!atomState) {
875
+ return [atom];
876
+ }
1086
877
  return [atom].concat(_toConsumableArray(Array.from((_atomState$mounted$re = (_atomState$mounted = atomState.mounted) === null || _atomState$mounted === void 0 ? void 0 : _atomState$mounted.readDepts) !== null && _atomState$mounted$re !== void 0 ? _atomState$mounted$re : []).map(function (key) {
1087
878
  return _this.getReadDependents(key);
1088
879
  })));
@@ -1090,15 +881,15 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1090
881
  _defineProperty(_this, "getSubscribeGraph", function () {
1091
882
  var subscribedAtoms = Array.from(_this.mountedAtomListenersCount.keys());
1092
883
  return subscribedAtoms.map(function (atom) {
1093
- var atomState = _this.atomManager.readAtomState(atom);
1094
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- we know it's mounted
884
+ var atomState = _this.context.stateMap.get(atom);
885
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1095
886
  var listeners = Array.from(atomState.mounted.listeners);
1096
887
  return [atom].concat(listeners);
1097
888
  });
1098
889
  });
1099
890
  _defineProperty(_this, "isMounted", function (atom) {
1100
- var mountState = _this.atomManager.readAtomState(atom);
1101
- return mountState.mounted !== undefined;
891
+ var mountState = _this.stateMap.get(atom);
892
+ return (mountState === null || mountState === void 0 ? void 0 : mountState.mounted) !== undefined;
1102
893
  });
1103
894
  return _this;
1104
895
  }
@@ -1106,11 +897,7 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1106
897
  return _createClass(DebugStoreImpl);
1107
898
  }(StoreImpl);
1108
899
  function createDebugStoreInternal(interceptor) {
1109
- var atomManager = new AtomManager({
1110
- interceptor: interceptor
1111
- });
1112
- var listenerManager = new ListenerManager();
1113
- return new DebugStoreImpl(atomManager, listenerManager, {
900
+ return new DebugStoreImpl({
1114
901
  interceptor: interceptor
1115
902
  });
1116
903
  }
@@ -1153,15 +940,15 @@ var ConsoleInterceptor = /*#__PURE__*/_createClass(function ConsoleInterceptor(w
1153
940
  console.groupEnd();
1154
941
  });
1155
942
  _defineProperty(this, "set", function (atom$, fn) {
943
+ var _console;
1156
944
  if (!_this.shouldLog(atom$, 'set')) {
1157
945
  fn();
1158
946
  return;
1159
947
  }
1160
- console.group('[R][SET] ' + atom$.toString());
1161
948
  for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
1162
949
  args[_key - 2] = arguments[_key];
1163
950
  }
1164
- console.log('arg:', args);
951
+ (_console = console).group.apply(_console, ['[R][SET] ' + atom$.toString(), '('].concat(args, [')']));
1165
952
  console.log('ret:', fn());
1166
953
  console.groupEnd();
1167
954
  });
@@ -1230,3 +1017,4 @@ function createDebugStore() {
1230
1017
  }
1231
1018
 
1232
1019
  exports.createDebugStore = createDebugStore;
1020
+ //# sourceMappingURL=index.cjs.map