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