ccstate 4.0.0 → 4.2.0

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