ccstate 4.1.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/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,619 @@ 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(_toConsumableArray(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 checkEpoch(readComputed, computedState, context, mutation) {
401
+ var _iterator = _createForOfIteratorHelper(computedState.dependencies.entries()),
402
+ _step;
403
+ try {
404
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
405
+ var _context$stateMap$get;
406
+ var _step$value = _slicedToArray(_step.value, 2),
407
+ dep = _step$value[0],
408
+ epoch = _step$value[1];
409
+ 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;
410
+ if (depEpoch !== epoch) {
411
+ return false;
582
412
  }
583
- return atomState;
584
- });
585
- this.options = options;
413
+ }
414
+ } catch (err) {
415
+ _iterator.e(err);
416
+ } finally {
417
+ _iterator.f();
586
418
  }
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);
419
+ return true;
420
+ }
421
+ function tryGetCached(readComputed, computed$, context, mutation) {
422
+ var signalState = context.stateMap.get(computed$);
423
+ if (!signalState) {
424
+ return undefined;
425
+ }
426
+ if (mutation !== null && mutation !== void 0 && mutation.dirtyMarkers.has(computed$.id)) {
427
+ return undefined;
428
+ }
429
+ if (signalState.mounted) {
430
+ return signalState;
431
+ }
432
+ if (checkEpoch(readComputed, signalState, context, mutation)) {
433
+ return signalState;
434
+ }
435
+ return undefined;
436
+ }
437
+ function wrapGet(readSignal, mount, callerComputed$, callerState, context, mutation) {
438
+ var readDeps = new Map();
439
+ return [function (dep$) {
440
+ var depState = readSignal(dep$, context, mutation);
441
+ if (callerState.dependencies === readDeps) {
442
+ readDeps.set(dep$, depState.epoch);
443
+ var callerMounted = !!callerState.mounted;
444
+ if (callerMounted && !depState.mounted) {
445
+ mount(dep$, context, mutation).readDepts.add(callerComputed$);
446
+ } else if (callerMounted && depState.mounted) {
447
+ depState.mounted.readDepts.add(callerComputed$);
600
448
  }
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');
449
+ }
450
+ return depState.val;
451
+ }, readDeps];
452
+ }
453
+ function getOrInitComputedState(computed$, context) {
454
+ var computedState = context.stateMap.get(computed$);
455
+ if (!computedState) {
456
+ computedState = {
457
+ dependencies: new Map(),
458
+ epoch: -1
459
+ };
460
+ context.stateMap.set(computed$, computedState);
461
+ }
462
+ return computedState;
463
+ }
464
+ function cleanupMissingDependencies(unmount, computed$, lastDeps, currDeps, context, mutation) {
465
+ var _iterator2 = _createForOfIteratorHelper(lastDeps.keys()),
466
+ _step2;
467
+ try {
468
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
469
+ var key = _step2.value;
470
+ if (!currDeps.has(key)) {
471
+ var _depState$mounted;
472
+ var depState = context.stateMap.get(key);
473
+ depState === null || depState === void 0 || (_depState$mounted = depState.mounted) === null || _depState$mounted === void 0 || _depState$mounted.readDepts["delete"](computed$);
474
+ unmount(key, context, mutation);
613
475
  }
614
- return result.data;
615
476
  }
477
+ } catch (err) {
478
+ _iterator2.e(err);
479
+ } finally {
480
+ _iterator2.f();
481
+ }
482
+ }
483
+ function evaluateComputed(readSignal, mount, unmount, computed$, context, mutation) {
484
+ var computedState = getOrInitComputedState(computed$, context);
485
+ var lastDeps = computedState.dependencies;
486
+ var _wrapGet = wrapGet(readSignal, mount, computed$, computedState, context, mutation),
487
+ _wrapGet2 = _slicedToArray(_wrapGet, 2),
488
+ _get = _wrapGet2[0],
489
+ dependencies = _wrapGet2[1];
490
+ computedState.dependencies = dependencies;
491
+ var evalVal = computed$.read(function (depAtom) {
492
+ var _context$interceptor;
493
+ return withGeValInterceptor(function () {
494
+ return _get(depAtom);
495
+ }, depAtom, (_context$interceptor = context.interceptor) === null || _context$interceptor === void 0 ? void 0 : _context$interceptor.get);
616
496
  }, {
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);
497
+ get signal() {
498
+ var _computedState$abortC, _computed$$debugLabel;
499
+ (_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"));
500
+ computedState.abortController = new AbortController();
501
+ return computedState.abortController.signal;
502
+ }
503
+ });
504
+ cleanupMissingDependencies(unmount, computed$, lastDeps, dependencies, context, mutation);
505
+ computedState.val = evalVal;
506
+ computedState.epoch += 1;
507
+ return computedState;
508
+ }
636
509
 
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);
510
+ function pushDirtyMarkers(signalState, context, mutation) {
511
+ var _signalState$mounted$, _signalState$mounted;
512
+ 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$ : []);
513
+ while (queue.length > 0) {
514
+ var nextQueue = [];
515
+ var _iterator = _createForOfIteratorHelper(queue),
516
+ _step;
517
+ try {
518
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
519
+ var computed$ = _step.value;
520
+ mutation.dirtyMarkers.add(computed$.id);
521
+ var computedState = context.stateMap.get(computed$);
522
+ // This computed$ is read from other computed$'s readDepts, so it must not be null and must have mounted
523
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
524
+ var _iterator2 = _createForOfIteratorHelper(computedState.mounted.readDepts),
525
+ _step2;
526
+ try {
527
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
528
+ var dep = _step2.value;
529
+ nextQueue.push(dep);
645
530
  }
531
+ } catch (err) {
532
+ _iterator2.e(err);
533
+ } finally {
534
+ _iterator2.f();
646
535
  }
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
536
  }
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);
537
+ } catch (err) {
538
+ _iterator.e(err);
539
+ } finally {
540
+ _iterator.f();
541
+ }
542
+ queue = nextQueue;
543
+ }
544
+ }
545
+ function pullEvaluate(readComputed, signalState, context, mutation) {
546
+ var _signalState$mounted$2, _signalState$mounted2, _signalState$mounted$3, _signalState$mounted3;
547
+ 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 : []);
548
+ 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 : []),
549
+ _step3;
550
+ try {
551
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
552
+ var _listener = _step3.value;
553
+ mutation.pendingListeners.add(_listener);
554
+ }
555
+ } catch (err) {
556
+ _iterator3.e(err);
557
+ } finally {
558
+ _iterator3.f();
559
+ }
560
+ while (queue.length > 0) {
561
+ var nextQueue = [];
562
+ var _iterator4 = _createForOfIteratorHelper(queue),
563
+ _step4;
564
+ try {
565
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
566
+ var _computedState$mounte, _computedState$mounte2;
567
+ var computed$ = _step4.value;
568
+ var computedState = readComputed(computed$, context, mutation);
569
+ if ((_computedState$mounte = computedState.mounted) !== null && _computedState$mounte !== void 0 && _computedState$mounte.listeners) {
570
+ var _iterator5 = _createForOfIteratorHelper(computedState.mounted.listeners),
571
+ _step5;
572
+ try {
573
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
574
+ var listener = _step5.value;
575
+ mutation.pendingListeners.add(listener);
691
576
  }
577
+ } catch (err) {
578
+ _iterator5.e(err);
579
+ } finally {
580
+ _iterator5.f();
692
581
  }
693
582
  }
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);
583
+ var readDepts = (_computedState$mounte2 = computedState.mounted) === null || _computedState$mounte2 === void 0 ? void 0 : _computedState$mounte2.readDepts;
584
+ if (readDepts) {
585
+ for (var _i = 0, _Array$from = Array.from(readDepts); _i < _Array$from.length; _i++) {
586
+ var dep = _Array$from[_i];
587
+ nextQueue.push(dep);
588
+ }
750
589
  }
751
590
  }
752
- return atomState.mounted;
591
+ } catch (err) {
592
+ _iterator4.e(err);
593
+ } finally {
594
+ _iterator4.f();
753
595
  }
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);
596
+ queue = nextQueue;
597
+ }
598
+ }
599
+ function propagationChanges(readComputed, signalState, context, mutation) {
600
+ pushDirtyMarkers(signalState, context, mutation);
601
+ pullEvaluate(readComputed, signalState, context, mutation);
602
+ }
603
+ function innerSetState(readComputed, signal$, context, mutation, val) {
604
+ var _ref, _context$stateMap$get;
605
+ 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;
606
+ var signalState = context.stateMap.get(signal$);
607
+ if (!signalState) {
608
+ context.stateMap.set(signal$, {
609
+ val: newValue,
610
+ epoch: 0
611
+ });
612
+ return;
613
+ }
614
+ signalState.val = newValue;
615
+ signalState.epoch += 1;
616
+ propagationChanges(readComputed, signalState, context, mutation);
617
+ return undefined;
618
+ }
619
+ function set(readComputed, writable$, context, mutation) {
620
+ if ('read' in writable$) {
621
+ return;
622
+ }
623
+ for (var _len = arguments.length, args = new Array(_len > 4 ? _len - 4 : 0), _key = 4; _key < _len; _key++) {
624
+ args[_key - 4] = arguments[_key];
625
+ }
626
+ if ('write' in writable$) {
627
+ return writable$.write.apply(writable$, [mutation.visitor].concat(_toConsumableArray(args)));
628
+ }
629
+ innerSetState(readComputed, writable$, context, mutation, args[0]);
630
+ return;
631
+ }
632
+
633
+ /**
634
+ * Creates a mutation operation context. The Mutation remains unique throughout
635
+ * the mutation cycle and can track side effects produced by this mutation operation
636
+ *
637
+ * This tracking is implemented by coloring the visitor function, so the Mutation
638
+ * needs to wrap get & set functions and ensure that all get & set operations
639
+ * executed in the mutation context come from the same Mutation
640
+ *
641
+ * @param context
642
+ * @param get
643
+ * @param set
644
+ * @returns
645
+ */
646
+ function createMutation(context, _get, _set) {
647
+ var mutation = {
648
+ dirtyMarkers: new Set(),
649
+ pendingListeners: new Set(),
650
+ visitor: {
651
+ get: function get(signal$) {
652
+ return _get(signal$, context, mutation);
653
+ },
654
+ set: function set(signal$) {
655
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
656
+ args[_key2 - 1] = arguments[_key2];
771
657
  }
658
+ return _set.apply(void 0, [signal$, context].concat(args));
772
659
  }
773
- atomState.mounted = undefined;
774
660
  }
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());
661
+ };
662
+ return mutation;
663
+ }
664
+
665
+ function readState(state, context) {
666
+ var atomState = context.stateMap.get(state);
667
+ if (!atomState) {
668
+ var initState = {
669
+ val: state.init,
670
+ epoch: 0
671
+ };
672
+ context.stateMap.set(state, initState);
673
+ return initState;
786
674
  }
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();
675
+ return atomState;
676
+ }
677
+
678
+ function unmountComputedDependencies(computed$, computedState, context, mutation) {
679
+ for (var _i = 0, _Array$from = Array.from(computedState.dependencies); _i < _Array$from.length; _i++) {
680
+ var _context$stateMap$get;
681
+ var _Array$from$_i = _slicedToArray(_Array$from[_i], 1),
682
+ dep = _Array$from$_i[0];
683
+ (_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$);
684
+ unmount(dep, context);
685
+ }
686
+ }
687
+ function mountComputedDependencies(readSignal, computed$, computedState, context, mutation) {
688
+ for (var _i2 = 0, _Array$from2 = Array.from(computedState.dependencies); _i2 < _Array$from2.length; _i2++) {
689
+ var _Array$from2$_i = _slicedToArray(_Array$from2[_i2], 1),
690
+ dep = _Array$from2$_i[0];
691
+ var mounted = mount$1(readSignal, dep, context, mutation);
692
+ mounted.readDepts.add(computed$);
693
+ }
694
+ }
695
+ function initMount(readSignal, signal$, context, mutation) {
696
+ var _context$interceptor, _context$interceptor$, _signalState$mounted;
697
+ (_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$);
698
+ var signalState = readSignal(signal$, context, mutation);
699
+ signalState.mounted = (_signalState$mounted = signalState.mounted) !== null && _signalState$mounted !== void 0 ? _signalState$mounted : {
700
+ listeners: new Set(),
701
+ readDepts: new Set()
702
+ };
703
+ if (isComputedState(signalState)) {
704
+ mountComputedDependencies(readSignal, signal$, signalState, context, mutation);
705
+ }
706
+ return signalState.mounted;
707
+ }
708
+ function mount$1(readSignal, signal$, context, mutation) {
709
+ var _context$stateMap$get2;
710
+ var mounted = (_context$stateMap$get2 = context.stateMap.get(signal$)) === null || _context$stateMap$get2 === void 0 ? void 0 : _context$stateMap$get2.mounted;
711
+ if (mounted) {
712
+ return mounted;
713
+ }
714
+ return initMount(readSignal, signal$, context, mutation);
715
+ }
716
+ function doUnmount(signal$, signalState, context, mutation) {
717
+ var _context$interceptor2, _context$interceptor3;
718
+ (_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$);
719
+ if (isComputedState(signalState)) {
720
+ unmountComputedDependencies(signal$, signalState, context);
721
+ }
722
+ signalState.mounted = undefined;
723
+ }
724
+ function unmount(signal$, context, mutation) {
725
+ var signalState = context.stateMap.get(signal$);
726
+ if (!(signalState !== null && signalState !== void 0 && signalState.mounted) || signalState.mounted.listeners.size || signalState.mounted.readDepts.size) {
727
+ return;
728
+ }
729
+ doUnmount(signal$, signalState, context);
730
+ }
731
+ function subSingleSignal(readSignal, signal$, callback$, context, signal) {
732
+ var _context$interceptor5;
733
+ withSubInterceptor(function () {
734
+ var mounted = mount$1(readSignal, signal$, context);
735
+ mounted.listeners.add(callback$);
736
+ var unsub = function unsub() {
737
+ var _context$interceptor4;
738
+ withUnsubInterceptor(function () {
739
+ mounted.listeners["delete"](callback$);
740
+ if (mounted.readDepts.size === 0 && mounted.listeners.size === 0) {
741
+ unmount(signal$, context);
826
742
  }
827
- queue = nextQueue;
828
- }
743
+ }, signal$, callback$, (_context$interceptor4 = context.interceptor) === null || _context$interceptor4 === void 0 ? void 0 : _context$interceptor4.unsub);
744
+ };
745
+ signal.addEventListener('abort', unsub, {
746
+ once: true
747
+ });
748
+ }, signal$, callback$, (_context$interceptor5 = context.interceptor) === null || _context$interceptor5 === void 0 ? void 0 : _context$interceptor5.sub);
749
+ }
750
+ function notify(context, mutation) {
751
+ var pendingListeners = mutation.pendingListeners;
752
+ mutation.pendingListeners = new Set();
753
+ var _iterator = _createForOfIteratorHelper(pendingListeners),
754
+ _step;
755
+ try {
756
+ var _loop = function _loop() {
757
+ var _context$interceptor6;
758
+ var listener = _step.value;
759
+ withNotifyInterceptor(function () {
760
+ return listener.write(mutation.visitor);
761
+ }, listener, (_context$interceptor6 = context.interceptor) === null || _context$interceptor6 === void 0 ? void 0 : _context$interceptor6.notify);
762
+ };
763
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
764
+ _loop();
829
765
  }
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
- }();
766
+ } catch (err) {
767
+ _iterator.e(err);
768
+ } finally {
769
+ _iterator.f();
770
+ }
771
+ }
872
772
 
773
+ var _readComputed = function readComputed(computed$, context, mutation) {
774
+ var _context$interceptor;
775
+ var cachedState = tryGetCached(_readComputed, computed$, context, mutation);
776
+ if (cachedState) {
777
+ return cachedState;
778
+ }
779
+ mutation === null || mutation === void 0 || mutation.dirtyMarkers["delete"](computed$.id);
780
+ return withComputedInterceptor(function () {
781
+ return evaluateComputed(readSignal, mount, unmount, computed$, context, mutation);
782
+ }, computed$, (_context$interceptor = context.interceptor) === null || _context$interceptor === void 0 ? void 0 : _context$interceptor.computed);
783
+ };
784
+ function readSignal(signal$, context, mutation) {
785
+ if (canReadAsCompute(signal$)) {
786
+ return _readComputed(signal$, context, mutation);
787
+ }
788
+ return readState(signal$, context);
789
+ }
790
+ function mount(signal$, context, mutation) {
791
+ return mount$1(readSignal, signal$, context, mutation);
792
+ }
793
+ function _sub(signals$, callback$, context, options) {
794
+ if (Array.isArray(signals$) && signals$.length === 0) {
795
+ return function () {
796
+ return void 0;
797
+ };
798
+ }
799
+ var controller = new AbortController();
800
+ var signal = options !== null && options !== void 0 && options.signal ? AbortSignal.any([controller.signal, options.signal]) : controller.signal;
801
+ if (!Array.isArray(signals$)) {
802
+ subSingleSignal(readSignal, signals$, callback$, context, signal);
803
+ } else {
804
+ signals$.forEach(function (atom) {
805
+ subSingleSignal(readSignal, atom, callback$, context, signal);
806
+ });
807
+ }
808
+ return function () {
809
+ controller.abort();
810
+ };
811
+ }
812
+ var get = function get(signal, context, mutation) {
813
+ var _context$interceptor2;
814
+ return withGetInterceptor(function () {
815
+ return readSignal(signal, context, mutation).val;
816
+ }, signal, (_context$interceptor2 = context.interceptor) === null || _context$interceptor2 === void 0 ? void 0 : _context$interceptor2.get);
817
+ };
818
+ var _set = function set$1(atom, context) {
819
+ var _context$interceptor3;
820
+ for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
821
+ args[_key - 2] = arguments[_key];
822
+ }
823
+ return withSetInterceptor.apply(void 0, [function () {
824
+ var mutation = createMutation(context, get, _set);
825
+ var ret;
826
+ try {
827
+ ret = set.apply(void 0, [_readComputed, atom, context, mutation].concat(args));
828
+ } finally {
829
+ notify(context, mutation);
830
+ }
831
+ return ret;
832
+ }, atom, (_context$interceptor3 = context.interceptor) === null || _context$interceptor3 === void 0 ? void 0 : _context$interceptor3.set].concat(args));
833
+ };
873
834
  var StoreImpl = /*#__PURE__*/function () {
874
- function StoreImpl(atomManager, listenerManager, options) {
875
- var _this = this;
835
+ function StoreImpl(options) {
836
+ var _this = this,
837
+ _this$options;
876
838
  _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
- });
839
+ _defineProperty(this, "stateMap", new WeakMap());
903
840
  _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
- }
841
+ return get(atom, _this.context);
957
842
  });
958
843
  _defineProperty(this, "set", function (atom) {
959
- var _this$options3;
960
844
  for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
961
845
  args[_key2 - 1] = arguments[_key2];
962
846
  }
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;
847
+ return _set.apply(void 0, [atom, _this.context].concat(args));
983
848
  });
984
- this.atomManager = atomManager;
985
- this.listenerManager = listenerManager;
986
849
  this.options = options;
850
+ this.context = {
851
+ stateMap: this.stateMap,
852
+ interceptor: (_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.interceptor
853
+ };
987
854
  }
988
855
  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
856
  key: "sub",
1039
857
  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;
858
+ return _sub(targets$, cb$, this.context, options);
1070
859
  }
1071
860
  }]);
1072
861
  }();
1073
862
  function createStore() {
1074
- var atomManager = new AtomManager();
1075
- var listenerManager = new ListenerManager();
1076
- return new StoreImpl(atomManager, listenerManager);
863
+ return new StoreImpl();
1077
864
  }
1078
865
  var defaultStore = undefined;
1079
866
  function getDefaultStore() {
@@ -1120,7 +907,10 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1120
907
  };
1121
908
  });
1122
909
  _defineProperty(_this, "getReadDependencies", function (atom) {
1123
- var atomState = _this.atomManager.readAtomState(atom);
910
+ var atomState = _this.context.stateMap.get(atom);
911
+ if (!atomState) {
912
+ return [atom];
913
+ }
1124
914
  if (!('dependencies' in atomState)) {
1125
915
  return [atom];
1126
916
  }
@@ -1132,7 +922,10 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1132
922
  });
1133
923
  _defineProperty(_this, "getReadDependents", function (atom) {
1134
924
  var _atomState$mounted$re, _atomState$mounted;
1135
- var atomState = _this.atomManager.readAtomState(atom);
925
+ var atomState = _this.context.stateMap.get(atom);
926
+ if (!atomState) {
927
+ return [atom];
928
+ }
1136
929
  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
930
  return _this.getReadDependents(key);
1138
931
  })));
@@ -1140,15 +933,15 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1140
933
  _defineProperty(_this, "getSubscribeGraph", function () {
1141
934
  var subscribedAtoms = Array.from(_this.mountedAtomListenersCount.keys());
1142
935
  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
936
+ var atomState = _this.context.stateMap.get(atom);
937
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1145
938
  var listeners = Array.from(atomState.mounted.listeners);
1146
939
  return [atom].concat(listeners);
1147
940
  });
1148
941
  });
1149
942
  _defineProperty(_this, "isMounted", function (atom) {
1150
- var mountState = _this.atomManager.readAtomState(atom);
1151
- return mountState.mounted !== undefined;
943
+ var mountState = _this.stateMap.get(atom);
944
+ return (mountState === null || mountState === void 0 ? void 0 : mountState.mounted) !== undefined;
1152
945
  });
1153
946
  return _this;
1154
947
  }
@@ -1156,11 +949,7 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1156
949
  return _createClass(DebugStoreImpl);
1157
950
  }(StoreImpl);
1158
951
  function createDebugStoreInternal(interceptor) {
1159
- var atomManager = new AtomManager({
1160
- interceptor: interceptor
1161
- });
1162
- var listenerManager = new ListenerManager();
1163
- return new DebugStoreImpl(atomManager, listenerManager, {
952
+ return new DebugStoreImpl({
1164
953
  interceptor: interceptor
1165
954
  });
1166
955
  }