ccstate 4.1.0 → 4.3.0

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