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