ccstate 4.1.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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,614 @@ 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(_toConsumableArray(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();
543
- }
544
- return atomState;
545
- });
546
- this.options = options;
547
- }
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);
561
- }
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');
355
+
356
+ function checkEpoch(readComputed, computedState, context, mutation) {
357
+ var _iterator = _createForOfIteratorHelper(computedState.dependencies.entries()),
358
+ _step;
359
+ try {
360
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
361
+ var _context$stateMap$get;
362
+ var _step$value = _slicedToArray(_step.value, 2),
363
+ dep = _step$value[0],
364
+ epoch = _step$value[1];
365
+ 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;
366
+ if (depEpoch !== epoch) {
367
+ return false;
574
368
  }
575
- return result.data;
576
369
  }
370
+ } catch (err) {
371
+ _iterator.e(err);
372
+ } finally {
373
+ _iterator.f();
374
+ }
375
+ return true;
376
+ }
377
+ function tryGetCached(readComputed, computed$, context, mutation) {
378
+ var signalState = context.stateMap.get(computed$);
379
+ if (!signalState) {
380
+ return undefined;
381
+ }
382
+ if (mutation !== null && mutation !== void 0 && mutation.dirtyMarkers.has(computed$.id)) {
383
+ return undefined;
384
+ }
385
+ if (signalState.mounted) {
386
+ return signalState;
387
+ }
388
+ if (checkEpoch(readComputed, signalState, context, mutation)) {
389
+ return signalState;
390
+ }
391
+ return undefined;
392
+ }
393
+ function wrapGet(readSignal, mount, callerComputed$, callerState, context, mutation) {
394
+ var readDeps = new Map();
395
+ return [function (dep$) {
396
+ var depState = readSignal(dep$, context, mutation);
397
+ if (callerState.dependencies === readDeps) {
398
+ readDeps.set(dep$, depState.epoch);
399
+ var callerMounted = !!callerState.mounted;
400
+ if (callerMounted && !depState.mounted) {
401
+ mount(dep$, context, mutation).readDepts.add(callerComputed$);
402
+ } else if (callerMounted && depState.mounted) {
403
+ depState.mounted.readDepts.add(callerComputed$);
404
+ }
405
+ }
406
+ return depState.val;
407
+ }, readDeps];
408
+ }
409
+ function getOrInitComputedState(computed$, context) {
410
+ var computedState = context.stateMap.get(computed$);
411
+ if (!computedState) {
412
+ computedState = {
413
+ dependencies: new Map(),
414
+ epoch: -1
415
+ };
416
+ context.stateMap.set(computed$, computedState);
417
+ }
418
+ return computedState;
419
+ }
420
+ function cleanupMissingDependencies(unmount, computed$, lastDeps, currDeps, context, mutation) {
421
+ var _iterator2 = _createForOfIteratorHelper(lastDeps.keys()),
422
+ _step2;
423
+ try {
424
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
425
+ var key = _step2.value;
426
+ if (!currDeps.has(key)) {
427
+ var _depState$mounted;
428
+ var depState = context.stateMap.get(key);
429
+ depState === null || depState === void 0 || (_depState$mounted = depState.mounted) === null || _depState$mounted === void 0 || _depState$mounted.readDepts["delete"](computed$);
430
+ unmount(key, context, mutation);
431
+ }
432
+ }
433
+ } catch (err) {
434
+ _iterator2.e(err);
435
+ } finally {
436
+ _iterator2.f();
437
+ }
438
+ }
439
+ function evaluateComputed(readSignal, mount, unmount, computed$, context, mutation) {
440
+ var computedState = getOrInitComputedState(computed$, context);
441
+ var lastDeps = computedState.dependencies;
442
+ var _wrapGet = wrapGet(readSignal, mount, computed$, computedState, context, mutation),
443
+ _wrapGet2 = _slicedToArray(_wrapGet, 2),
444
+ _get = _wrapGet2[0],
445
+ dependencies = _wrapGet2[1];
446
+ computedState.dependencies = dependencies;
447
+ var evalVal = computed$.read(function (depAtom) {
448
+ var _context$interceptor;
449
+ return withGeValInterceptor(function () {
450
+ return _get(depAtom);
451
+ }, depAtom, (_context$interceptor = context.interceptor) === null || _context$interceptor === void 0 ? void 0 : _context$interceptor.get);
577
452
  }, {
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);
453
+ get signal() {
454
+ var _computedState$abortC, _computed$$debugLabel;
455
+ (_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"));
456
+ computedState.abortController = new AbortController();
457
+ return computedState.abortController.signal;
458
+ }
459
+ });
460
+ cleanupMissingDependencies(unmount, computed$, lastDeps, dependencies, context, mutation);
461
+ computedState.val = evalVal;
462
+ computedState.epoch += 1;
463
+ return computedState;
464
+ }
597
465
 
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);
466
+ function pushDirtyMarkers(signalState, context, mutation) {
467
+ var _signalState$mounted$, _signalState$mounted;
468
+ 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$ : []);
469
+ while (queue.length > 0) {
470
+ var nextQueue = [];
471
+ var _iterator = _createForOfIteratorHelper(queue),
472
+ _step;
473
+ try {
474
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
475
+ var computed$ = _step.value;
476
+ mutation.dirtyMarkers.add(computed$.id);
477
+ var computedState = context.stateMap.get(computed$);
478
+ // This computed$ is read from other computed$'s readDepts, so it must not be null and must have mounted
479
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
480
+ var _iterator2 = _createForOfIteratorHelper(computedState.mounted.readDepts),
481
+ _step2;
482
+ try {
483
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
484
+ var dep = _step2.value;
485
+ nextQueue.push(dep);
606
486
  }
487
+ } catch (err) {
488
+ _iterator2.e(err);
489
+ } finally {
490
+ _iterator2.f();
607
491
  }
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
492
  }
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);
493
+ } catch (err) {
494
+ _iterator.e(err);
495
+ } finally {
496
+ _iterator.f();
497
+ }
498
+ queue = nextQueue;
499
+ }
500
+ }
501
+ function pullEvaluate(readComputed, signalState, context, mutation) {
502
+ var _signalState$mounted$2, _signalState$mounted2, _signalState$mounted$3, _signalState$mounted3;
503
+ 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 : []);
504
+ 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 : []),
505
+ _step3;
506
+ try {
507
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
508
+ var _listener = _step3.value;
509
+ mutation.pendingListeners.add(_listener);
510
+ }
511
+ } catch (err) {
512
+ _iterator3.e(err);
513
+ } finally {
514
+ _iterator3.f();
515
+ }
516
+ while (queue.length > 0) {
517
+ var nextQueue = [];
518
+ var _iterator4 = _createForOfIteratorHelper(queue),
519
+ _step4;
520
+ try {
521
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
522
+ var _computedState$mounte, _computedState$mounte2;
523
+ var computed$ = _step4.value;
524
+ var computedState = readComputed(computed$, context, mutation);
525
+ if ((_computedState$mounte = computedState.mounted) !== null && _computedState$mounte !== void 0 && _computedState$mounte.listeners) {
526
+ var _iterator5 = _createForOfIteratorHelper(computedState.mounted.listeners),
527
+ _step5;
528
+ try {
529
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
530
+ var listener = _step5.value;
531
+ mutation.pendingListeners.add(listener);
652
532
  }
533
+ } catch (err) {
534
+ _iterator5.e(err);
535
+ } finally {
536
+ _iterator5.f();
653
537
  }
654
538
  }
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);
539
+ var readDepts = (_computedState$mounte2 = computedState.mounted) === null || _computedState$mounte2 === void 0 ? void 0 : _computedState$mounte2.readDepts;
540
+ if (readDepts) {
541
+ for (var _i = 0, _Array$from = Array.from(readDepts); _i < _Array$from.length; _i++) {
542
+ var dep = _Array$from[_i];
543
+ nextQueue.push(dep);
544
+ }
711
545
  }
712
546
  }
713
- return atomState.mounted;
547
+ } catch (err) {
548
+ _iterator4.e(err);
549
+ } finally {
550
+ _iterator4.f();
714
551
  }
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);
552
+ queue = nextQueue;
553
+ }
554
+ }
555
+ function propagationChanges(readComputed, signalState, context, mutation) {
556
+ pushDirtyMarkers(signalState, context, mutation);
557
+ pullEvaluate(readComputed, signalState, context, mutation);
558
+ }
559
+ function innerSetState(readComputed, signal$, context, mutation, val) {
560
+ var _ref, _context$stateMap$get;
561
+ var newValue = typeof val === 'function' ? val((_ref = (_context$stateMap$get = context.stateMap.get(signal$)) === null || _context$stateMap$get === void 0 ? void 0 : _context$stateMap$get.val) !== null && _ref !== void 0 ? _ref : signal$.init) : val;
562
+ var signalState = context.stateMap.get(signal$);
563
+ if (!signalState) {
564
+ context.stateMap.set(signal$, {
565
+ val: newValue,
566
+ epoch: 0
567
+ });
568
+ return;
569
+ }
570
+ signalState.val = newValue;
571
+ signalState.epoch += 1;
572
+ propagationChanges(readComputed, signalState, context, mutation);
573
+ return undefined;
574
+ }
575
+ function set(readComputed, writable$, context, mutation) {
576
+ if ('read' in writable$) {
577
+ return;
578
+ }
579
+ for (var _len = arguments.length, args = new Array(_len > 4 ? _len - 4 : 0), _key = 4; _key < _len; _key++) {
580
+ args[_key - 4] = arguments[_key];
581
+ }
582
+ if ('write' in writable$) {
583
+ return writable$.write.apply(writable$, [mutation.visitor].concat(_toConsumableArray(args)));
584
+ }
585
+ innerSetState(readComputed, writable$, context, mutation, args[0]);
586
+ return;
587
+ }
588
+
589
+ /**
590
+ * Creates a mutation operation context. The Mutation remains unique throughout
591
+ * the mutation cycle and can track side effects produced by this mutation operation
592
+ *
593
+ * This tracking is implemented by coloring the visitor function, so the Mutation
594
+ * needs to wrap get & set functions and ensure that all get & set operations
595
+ * executed in the mutation context come from the same Mutation
596
+ *
597
+ * @param context
598
+ * @param get
599
+ * @param set
600
+ * @returns
601
+ */
602
+ function createMutation(context, _get, _set) {
603
+ var mutation = {
604
+ dirtyMarkers: new Set(),
605
+ pendingListeners: new Set(),
606
+ visitor: {
607
+ get: function get(signal$) {
608
+ return _get(signal$, context, mutation);
609
+ },
610
+ set: function set(signal$) {
611
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
612
+ args[_key2 - 1] = arguments[_key2];
732
613
  }
614
+ return _set.apply(void 0, [signal$, context].concat(args));
733
615
  }
734
- atomState.mounted = undefined;
735
- }
736
- }, {
737
- key: "inited",
738
- value: function inited(atom) {
739
- return this.atomStateMap.has(atom);
740
616
  }
741
- }]);
742
- }();
743
- var ListenerManager = /*#__PURE__*/function () {
744
- function ListenerManager() {
745
- _classCallCheck(this, ListenerManager);
746
- _defineProperty(this, "pendingListeners", new Set());
617
+ };
618
+ return mutation;
619
+ }
620
+
621
+ function readState(state, context) {
622
+ var atomState = context.stateMap.get(state);
623
+ if (!atomState) {
624
+ var initState = {
625
+ val: state.init,
626
+ epoch: 0
627
+ };
628
+ context.stateMap.set(state, initState);
629
+ return initState;
747
630
  }
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();
631
+ return atomState;
632
+ }
633
+
634
+ function unmountComputedDependencies(computed$, computedState, context, mutation) {
635
+ for (var _i = 0, _Array$from = Array.from(computedState.dependencies); _i < _Array$from.length; _i++) {
636
+ var _context$stateMap$get;
637
+ var _Array$from$_i = _slicedToArray(_Array$from[_i], 1),
638
+ dep = _Array$from$_i[0];
639
+ (_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$);
640
+ unmount(dep, context);
641
+ }
642
+ }
643
+ function mountComputedDependencies(readSignal, computed$, computedState, context, mutation) {
644
+ for (var _i2 = 0, _Array$from2 = Array.from(computedState.dependencies); _i2 < _Array$from2.length; _i2++) {
645
+ var _Array$from2$_i = _slicedToArray(_Array$from2[_i2], 1),
646
+ dep = _Array$from2$_i[0];
647
+ var mounted = mount$1(readSignal, dep, context, mutation);
648
+ mounted.readDepts.add(computed$);
649
+ }
650
+ }
651
+ function initMount(readSignal, signal$, context, mutation) {
652
+ var _context$interceptor, _context$interceptor$, _signalState$mounted;
653
+ (_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$);
654
+ var signalState = readSignal(signal$, context, mutation);
655
+ signalState.mounted = (_signalState$mounted = signalState.mounted) !== null && _signalState$mounted !== void 0 ? _signalState$mounted : {
656
+ listeners: new Set(),
657
+ readDepts: new Set()
658
+ };
659
+ if (isComputedState(signalState)) {
660
+ mountComputedDependencies(readSignal, signal$, signalState, context, mutation);
661
+ }
662
+ return signalState.mounted;
663
+ }
664
+ function mount$1(readSignal, signal$, context, mutation) {
665
+ var _context$stateMap$get2;
666
+ var mounted = (_context$stateMap$get2 = context.stateMap.get(signal$)) === null || _context$stateMap$get2 === void 0 ? void 0 : _context$stateMap$get2.mounted;
667
+ if (mounted) {
668
+ return mounted;
669
+ }
670
+ return initMount(readSignal, signal$, context, mutation);
671
+ }
672
+ function doUnmount(signal$, signalState, context, mutation) {
673
+ var _context$interceptor2, _context$interceptor3;
674
+ (_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$);
675
+ if (isComputedState(signalState)) {
676
+ unmountComputedDependencies(signal$, signalState, context);
677
+ }
678
+ signalState.mounted = undefined;
679
+ }
680
+ function unmount(signal$, context, mutation) {
681
+ var signalState = context.stateMap.get(signal$);
682
+ if (!(signalState !== null && signalState !== void 0 && signalState.mounted) || signalState.mounted.listeners.size || signalState.mounted.readDepts.size) {
683
+ return;
684
+ }
685
+ doUnmount(signal$, signalState, context);
686
+ }
687
+ function subSingleSignal(readSignal, signal$, callback$, context, signal) {
688
+ var _context$interceptor5;
689
+ withSubInterceptor(function () {
690
+ var mounted = mount$1(readSignal, signal$, context);
691
+ mounted.listeners.add(callback$);
692
+ var unsub = function unsub() {
693
+ var _context$interceptor4;
694
+ withUnsubInterceptor(function () {
695
+ mounted.listeners["delete"](callback$);
696
+ if (mounted.readDepts.size === 0 && mounted.listeners.size === 0) {
697
+ unmount(signal$, context);
787
698
  }
788
- queue = nextQueue;
789
- }
699
+ }, signal$, callback$, (_context$interceptor4 = context.interceptor) === null || _context$interceptor4 === void 0 ? void 0 : _context$interceptor4.unsub);
700
+ };
701
+ signal.addEventListener('abort', unsub, {
702
+ once: true
703
+ });
704
+ }, signal$, callback$, (_context$interceptor5 = context.interceptor) === null || _context$interceptor5 === void 0 ? void 0 : _context$interceptor5.sub);
705
+ }
706
+ function notify(context, mutation) {
707
+ var pendingListeners = mutation.pendingListeners;
708
+ mutation.pendingListeners = new Set();
709
+ var _iterator = _createForOfIteratorHelper(pendingListeners),
710
+ _step;
711
+ try {
712
+ var _loop = function _loop() {
713
+ var _context$interceptor6;
714
+ var listener = _step.value;
715
+ withNotifyInterceptor(function () {
716
+ return listener.write(mutation.visitor);
717
+ }, listener, (_context$interceptor6 = context.interceptor) === null || _context$interceptor6 === void 0 ? void 0 : _context$interceptor6.notify);
718
+ };
719
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
720
+ _loop();
790
721
  }
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
- }();
722
+ } catch (err) {
723
+ _iterator.e(err);
724
+ } finally {
725
+ _iterator.f();
726
+ }
727
+ }
833
728
 
729
+ var _readComputed = function readComputed(computed$, context, mutation) {
730
+ var _context$interceptor;
731
+ var cachedState = tryGetCached(_readComputed, computed$, context, mutation);
732
+ if (cachedState) {
733
+ return cachedState;
734
+ }
735
+ mutation === null || mutation === void 0 || mutation.dirtyMarkers["delete"](computed$.id);
736
+ return withComputedInterceptor(function () {
737
+ return evaluateComputed(readSignal, mount, unmount, computed$, context, mutation);
738
+ }, computed$, (_context$interceptor = context.interceptor) === null || _context$interceptor === void 0 ? void 0 : _context$interceptor.computed);
739
+ };
740
+ function readSignal(signal$, context, mutation) {
741
+ if (canReadAsCompute(signal$)) {
742
+ return _readComputed(signal$, context, mutation);
743
+ }
744
+ return readState(signal$, context);
745
+ }
746
+ function mount(signal$, context, mutation) {
747
+ return mount$1(readSignal, signal$, context, mutation);
748
+ }
749
+ function _sub(signals$, callback$, context, options) {
750
+ if (Array.isArray(signals$) && signals$.length === 0) {
751
+ return function () {
752
+ return void 0;
753
+ };
754
+ }
755
+ var controller = new AbortController();
756
+ var signal = options !== null && options !== void 0 && options.signal ? AbortSignal.any([controller.signal, options.signal]) : controller.signal;
757
+ if (!Array.isArray(signals$)) {
758
+ subSingleSignal(readSignal, signals$, callback$, context, signal);
759
+ } else {
760
+ signals$.forEach(function (atom) {
761
+ subSingleSignal(readSignal, atom, callback$, context, signal);
762
+ });
763
+ }
764
+ return function () {
765
+ controller.abort();
766
+ };
767
+ }
768
+ var get = function get(signal, context, mutation) {
769
+ var _context$interceptor2;
770
+ return withGetInterceptor(function () {
771
+ return readSignal(signal, context, mutation).val;
772
+ }, signal, (_context$interceptor2 = context.interceptor) === null || _context$interceptor2 === void 0 ? void 0 : _context$interceptor2.get);
773
+ };
774
+ var _set = function set$1(atom, context) {
775
+ var _context$interceptor3;
776
+ for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
777
+ args[_key - 2] = arguments[_key];
778
+ }
779
+ return withSetInterceptor.apply(void 0, [function () {
780
+ var mutation = createMutation(context, get, _set);
781
+ var ret;
782
+ try {
783
+ ret = set.apply(void 0, [_readComputed, atom, context, mutation].concat(args));
784
+ } finally {
785
+ notify(context, mutation);
786
+ }
787
+ return ret;
788
+ }, atom, (_context$interceptor3 = context.interceptor) === null || _context$interceptor3 === void 0 ? void 0 : _context$interceptor3.set].concat(args));
789
+ };
834
790
  var StoreImpl = /*#__PURE__*/function () {
835
- function StoreImpl(atomManager, listenerManager, options) {
836
- var _this = this;
791
+ function StoreImpl(options) {
792
+ var _this = this,
793
+ _this$options;
837
794
  _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
- });
795
+ _defineProperty(this, "stateMap", new WeakMap());
864
796
  _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
- }
797
+ return get(atom, _this.context);
918
798
  });
919
799
  _defineProperty(this, "set", function (atom) {
920
- var _this$options3;
921
800
  for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
922
801
  args[_key2 - 1] = arguments[_key2];
923
802
  }
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;
803
+ return _set.apply(void 0, [atom, _this.context].concat(args));
944
804
  });
945
- this.atomManager = atomManager;
946
- this.listenerManager = listenerManager;
947
805
  this.options = options;
806
+ this.context = {
807
+ stateMap: this.stateMap,
808
+ interceptor: (_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.interceptor
809
+ };
948
810
  }
949
811
  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
812
  key: "sub",
1000
813
  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;
814
+ return _sub(targets$, cb$, this.context, options);
1031
815
  }
1032
816
  }]);
1033
817
  }();
@@ -1069,7 +853,10 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1069
853
  };
1070
854
  });
1071
855
  _defineProperty(_this, "getReadDependencies", function (atom) {
1072
- var atomState = _this.atomManager.readAtomState(atom);
856
+ var atomState = _this.context.stateMap.get(atom);
857
+ if (!atomState) {
858
+ return [atom];
859
+ }
1073
860
  if (!('dependencies' in atomState)) {
1074
861
  return [atom];
1075
862
  }
@@ -1081,7 +868,10 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1081
868
  });
1082
869
  _defineProperty(_this, "getReadDependents", function (atom) {
1083
870
  var _atomState$mounted$re, _atomState$mounted;
1084
- var atomState = _this.atomManager.readAtomState(atom);
871
+ var atomState = _this.context.stateMap.get(atom);
872
+ if (!atomState) {
873
+ return [atom];
874
+ }
1085
875
  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
876
  return _this.getReadDependents(key);
1087
877
  })));
@@ -1089,15 +879,15 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1089
879
  _defineProperty(_this, "getSubscribeGraph", function () {
1090
880
  var subscribedAtoms = Array.from(_this.mountedAtomListenersCount.keys());
1091
881
  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
882
+ var atomState = _this.context.stateMap.get(atom);
883
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1094
884
  var listeners = Array.from(atomState.mounted.listeners);
1095
885
  return [atom].concat(listeners);
1096
886
  });
1097
887
  });
1098
888
  _defineProperty(_this, "isMounted", function (atom) {
1099
- var mountState = _this.atomManager.readAtomState(atom);
1100
- return mountState.mounted !== undefined;
889
+ var mountState = _this.stateMap.get(atom);
890
+ return (mountState === null || mountState === void 0 ? void 0 : mountState.mounted) !== undefined;
1101
891
  });
1102
892
  return _this;
1103
893
  }
@@ -1105,11 +895,7 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1105
895
  return _createClass(DebugStoreImpl);
1106
896
  }(StoreImpl);
1107
897
  function createDebugStoreInternal(interceptor) {
1108
- var atomManager = new AtomManager({
1109
- interceptor: interceptor
1110
- });
1111
- var listenerManager = new ListenerManager();
1112
- return new DebugStoreImpl(atomManager, listenerManager, {
898
+ return new DebugStoreImpl({
1113
899
  interceptor: interceptor
1114
900
  });
1115
901
  }