ccstate 4.0.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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,528 +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);
561
- }
562
- var result = {
563
- called: false
564
- };
565
- computedInterceptor(atom, function () {
566
- result = {
567
- called: true,
568
- data: _this2.computeComputedAtom(atom)
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 self = atom;
583
- var atomState = this.atomStateMap.get(self);
584
- if (!atomState) {
585
- atomState = {
586
- dependencies: new Map(),
587
- epoch: -1
588
- };
589
- this.atomStateMap.set(self, atomState);
590
- }
591
- var lastDeps = atomState.dependencies;
592
- var readDeps = new Map();
593
- atomState.dependencies = readDeps;
594
- var wrappedGet = function wrappedGet(depAtom) {
595
- var depState = _this3.readAtomState(depAtom);
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
+ }
596
465
 
597
- // get 可能发生在异步过程中,当重复调用时,只有最新的 get 过程会修改 deps
598
- if (atomState.dependencies === readDeps) {
599
- readDeps.set(depAtom, depState.epoch);
600
- var selfMounted = !!atomState.mounted;
601
- if (selfMounted && !depState.mounted) {
602
- _this3.mount(depAtom).readDepts.add(self);
603
- } else if (selfMounted && depState.mounted) {
604
- 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);
605
486
  }
487
+ } catch (err) {
488
+ _iterator2.e(err);
489
+ } finally {
490
+ _iterator2.f();
606
491
  }
607
- return depState.val;
608
- };
609
- 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;
610
- var ret = self.read(function (depAtom) {
611
- if (!getInterceptor) {
612
- return wrappedGet(depAtom);
613
- }
614
- var result = {
615
- called: false
616
- };
617
- var fn = function fn() {
618
- result = {
619
- called: true,
620
- data: wrappedGet(depAtom)
621
- };
622
- return result.data;
623
- };
624
- getInterceptor(depAtom, fn);
625
- if (!result.called) {
626
- throw new Error('interceptor must call fn sync');
627
- }
628
- return result.data;
629
- }, {
630
- get signal() {
631
- var _atomState$abortContr, _self$debugLabel;
632
- (_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"));
633
- atomState.abortController = new AbortController();
634
- return atomState.abortController.signal;
635
- }
636
- });
637
- if (atomState.val !== ret) {
638
- atomState.val = ret;
639
- atomState.epoch += 1;
640
492
  }
641
- var _iterator2 = _createForOfIteratorHelper(lastDeps.keys()),
642
- _step2;
643
- try {
644
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
645
- var key = _step2.value;
646
- if (!readDeps.has(key)) {
647
- var depState = this.atomStateMap.get(key);
648
- if (depState !== null && depState !== void 0 && depState.mounted) {
649
- depState.mounted.readDepts["delete"](self);
650
- 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);
651
532
  }
533
+ } catch (err) {
534
+ _iterator5.e(err);
535
+ } finally {
536
+ _iterator5.f();
652
537
  }
653
538
  }
654
- } catch (err) {
655
- _iterator2.e(err);
656
- } finally {
657
- _iterator2.f();
658
- }
659
- return atomState;
660
- }
661
- }, {
662
- key: "readStateAtom",
663
- value: function readStateAtom(atom) {
664
- var atomState = this.atomStateMap.get(atom);
665
- if (!atomState) {
666
- var initState = {
667
- val: atom.init,
668
- epoch: 0
669
- };
670
- this.atomStateMap.set(atom, initState);
671
- return initState;
672
- }
673
- return atomState;
674
- }
675
- }, {
676
- key: "readAtomState",
677
- value: function readAtomState(atom) {
678
- var ignoreMounted = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
679
- if (canReadAsCompute(atom)) {
680
- return this.readComputedAtom(atom, ignoreMounted);
681
- }
682
- return this.readStateAtom(atom);
683
- }
684
- }, {
685
- key: "tryGetMount",
686
- value: function tryGetMount(atom) {
687
- var _this$atomStateMap$ge;
688
- return (_this$atomStateMap$ge = this.atomStateMap.get(atom)) === null || _this$atomStateMap$ge === void 0 ? void 0 : _this$atomStateMap$ge.mounted;
689
- }
690
- }, {
691
- key: "mount",
692
- value: function mount(atom) {
693
- var _this$options3, _this$options3$mount, _atomState$mounted;
694
- var mounted = this.tryGetMount(atom);
695
- if (mounted) {
696
- return mounted;
697
- }
698
- (_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);
699
- var atomState = this.readAtomState(atom);
700
- atomState.mounted = (_atomState$mounted = atomState.mounted) !== null && _atomState$mounted !== void 0 ? _atomState$mounted : {
701
- listeners: new Set(),
702
- readDepts: new Set()
703
- };
704
- if (isComputedState(atomState)) {
705
- for (var _i = 0, _Array$from = Array.from(atomState.dependencies); _i < _Array$from.length; _i++) {
706
- var _Array$from$_i = _slicedToArray(_Array$from[_i], 1),
707
- dep = _Array$from$_i[0];
708
- var _mounted = this.mount(dep);
709
- _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
+ }
710
545
  }
711
546
  }
712
- return atomState.mounted;
547
+ } catch (err) {
548
+ _iterator4.e(err);
549
+ } finally {
550
+ _iterator4.f();
713
551
  }
714
- }, {
715
- key: "tryUnmount",
716
- value: function tryUnmount(atom) {
717
- var _this$options4, _this$options4$unmoun;
718
- var atomState = this.atomStateMap.get(atom);
719
- if (!(atomState !== null && atomState !== void 0 && atomState.mounted) || atomState.mounted.listeners.size || atomState.mounted.readDepts.size) {
720
- return;
721
- }
722
- (_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);
723
- if (isComputedState(atomState)) {
724
- for (var _i2 = 0, _Array$from2 = Array.from(atomState.dependencies); _i2 < _Array$from2.length; _i2++) {
725
- var _depState$mounted;
726
- var _Array$from2$_i = _slicedToArray(_Array$from2[_i2], 1),
727
- dep = _Array$from2$_i[0];
728
- var depState = this.readAtomState(dep);
729
- (_depState$mounted = depState.mounted) === null || _depState$mounted === void 0 || _depState$mounted.readDepts["delete"](atom);
730
- 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];
731
613
  }
614
+ return _set.apply(void 0, [signal$, context].concat(args));
732
615
  }
733
- atomState.mounted = undefined;
734
- }
735
- }, {
736
- key: "inited",
737
- value: function inited(atom) {
738
- return this.atomStateMap.has(atom);
739
616
  }
740
- }]);
741
- }();
742
- var ListenerManager = /*#__PURE__*/function () {
743
- function ListenerManager() {
744
- _classCallCheck(this, ListenerManager);
745
- _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;
746
630
  }
747
- return _createClass(ListenerManager, [{
748
- key: "markPendingListeners",
749
- value: function markPendingListeners(atomManager, atom) {
750
- var queue = [atom];
751
- while (queue.length > 0) {
752
- var nextQueue = [];
753
- var _iterator3 = _createForOfIteratorHelper(queue),
754
- _step3;
755
- try {
756
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
757
- var _atomState$mounted2, _atomState$mounted3;
758
- var _atom = _step3.value;
759
- var atomState = atomManager.readAtomState(_atom, true);
760
- if ((_atomState$mounted2 = atomState.mounted) !== null && _atomState$mounted2 !== void 0 && _atomState$mounted2.listeners) {
761
- var _iterator4 = _createForOfIteratorHelper(atomState.mounted.listeners),
762
- _step4;
763
- try {
764
- for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
765
- var listener = _step4.value;
766
- this.pendingListeners.add(listener);
767
- }
768
- } catch (err) {
769
- _iterator4.e(err);
770
- } finally {
771
- _iterator4.f();
772
- }
773
- }
774
- var readDepts = (_atomState$mounted3 = atomState.mounted) === null || _atomState$mounted3 === void 0 ? void 0 : _atomState$mounted3.readDepts;
775
- if (readDepts) {
776
- for (var _i3 = 0, _Array$from3 = Array.from(readDepts); _i3 < _Array$from3.length; _i3++) {
777
- var dep = _Array$from3[_i3];
778
- nextQueue.push(dep);
779
- }
780
- }
781
- }
782
- } catch (err) {
783
- _iterator3.e(err);
784
- } finally {
785
- _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);
786
698
  }
787
- queue = nextQueue;
788
- }
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();
789
721
  }
790
- }, {
791
- key: "notify",
792
- value: /*#__PURE__*/_regeneratorRuntime().mark(function notify() {
793
- var pendingListeners, _iterator5, _step5, listener;
794
- return _regeneratorRuntime().wrap(function notify$(_context) {
795
- while (1) switch (_context.prev = _context.next) {
796
- case 0:
797
- pendingListeners = this.pendingListeners;
798
- this.pendingListeners = new Set();
799
- _iterator5 = _createForOfIteratorHelper(pendingListeners);
800
- _context.prev = 3;
801
- _iterator5.s();
802
- case 5:
803
- if ((_step5 = _iterator5.n()).done) {
804
- _context.next = 11;
805
- break;
806
- }
807
- listener = _step5.value;
808
- _context.next = 9;
809
- return listener;
810
- case 9:
811
- _context.next = 5;
812
- break;
813
- case 11:
814
- _context.next = 16;
815
- break;
816
- case 13:
817
- _context.prev = 13;
818
- _context.t0 = _context["catch"](3);
819
- _iterator5.e(_context.t0);
820
- case 16:
821
- _context.prev = 16;
822
- _iterator5.f();
823
- return _context.finish(16);
824
- case 19:
825
- case "end":
826
- return _context.stop();
827
- }
828
- }, notify, this, [[3, 13, 16, 19]]);
829
- })
830
- }]);
831
- }();
722
+ } catch (err) {
723
+ _iterator.e(err);
724
+ } finally {
725
+ _iterator.f();
726
+ }
727
+ }
832
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
+ };
833
790
  var StoreImpl = /*#__PURE__*/function () {
834
- function StoreImpl(atomManager, listenerManager, options) {
835
- var _this = this;
791
+ function StoreImpl(options) {
792
+ var _this = this,
793
+ _this$options;
836
794
  _classCallCheck(this, StoreImpl);
837
- _defineProperty(this, "innerSet", function (atom) {
838
- if ('read' in atom) {
839
- return;
840
- }
841
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
842
- args[_key - 1] = arguments[_key];
843
- }
844
- if ('write' in atom) {
845
- var ret = atom.write.apply(atom, [{
846
- get: _this.get,
847
- set: _this.set
848
- }].concat(_toConsumableArray(args)));
849
- return ret;
850
- }
851
- var newValue = typeof args[0] === 'function' ? args[0](_this.atomManager.readAtomState(atom).val) : args[0];
852
- if (!_this.atomManager.inited(atom)) {
853
- _this.atomManager.readAtomState(atom).val = newValue;
854
- _this.listenerManager.markPendingListeners(_this.atomManager, atom);
855
- return;
856
- }
857
- var atomState = _this.atomManager.readAtomState(atom);
858
- atomState.val = newValue;
859
- atomState.epoch += 1;
860
- _this.listenerManager.markPendingListeners(_this.atomManager, atom);
861
- return undefined;
862
- });
795
+ _defineProperty(this, "stateMap", new WeakMap());
863
796
  _defineProperty(this, "get", function (atom) {
864
- var _this$options;
865
- if (!((_this$options = _this.options) !== null && _this$options !== void 0 && (_this$options = _this$options.interceptor) !== null && _this$options !== void 0 && _this$options.get)) {
866
- return _this.atomManager.readAtomState(atom).val;
867
- }
868
- var result = {
869
- called: false
870
- };
871
- var fnWithRet = function fnWithRet() {
872
- result = {
873
- called: true,
874
- data: _this.atomManager.readAtomState(atom).val
875
- };
876
- return result.data;
877
- };
878
- _this.options.interceptor.get(atom, fnWithRet);
879
- if (!result.called) {
880
- throw new Error('interceptor must call fn sync');
881
- }
882
- return result.data;
883
- });
884
- _defineProperty(this, "notify", function () {
885
- var _iterator = _createForOfIteratorHelper(_this.listenerManager.notify()),
886
- _step;
887
- try {
888
- var _loop = function _loop() {
889
- var _this$options2;
890
- var listener = _step.value;
891
- var notifyed = false;
892
- var fn = function fn() {
893
- notifyed = true;
894
- return listener.write({
895
- get: _this.get,
896
- set: _this.set
897
- });
898
- };
899
- if ((_this$options2 = _this.options) !== null && _this$options2 !== void 0 && (_this$options2 = _this$options2.interceptor) !== null && _this$options2 !== void 0 && _this$options2.notify) {
900
- _this.options.interceptor.notify(listener, fn);
901
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- notify must call fn sync
902
- if (!notifyed) {
903
- throw new Error('interceptor must call fn sync');
904
- }
905
- } else {
906
- fn();
907
- }
908
- };
909
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
910
- _loop();
911
- }
912
- } catch (err) {
913
- _iterator.e(err);
914
- } finally {
915
- _iterator.f();
916
- }
797
+ return get(atom, _this.context);
917
798
  });
918
799
  _defineProperty(this, "set", function (atom) {
919
- var _this$options3;
920
800
  for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
921
801
  args[_key2 - 1] = arguments[_key2];
922
802
  }
923
- var ret;
924
- var fn = function fn() {
925
- try {
926
- ret = _this.innerSet.apply(_this, [atom].concat(args));
927
- } finally {
928
- _this.notify();
929
- }
930
- return ret;
931
- };
932
- if ((_this$options3 = _this.options) !== null && _this$options3 !== void 0 && (_this$options3 = _this$options3.interceptor) !== null && _this$options3 !== void 0 && _this$options3.set) {
933
- if ('write' in atom) {
934
- var _this$options$interce;
935
- (_this$options$interce = _this.options.interceptor).set.apply(_this$options$interce, [atom, fn].concat(_toConsumableArray(args)));
936
- } else {
937
- _this.options.interceptor.set(atom, fn, args[0]);
938
- }
939
- } else {
940
- fn();
941
- }
942
- return ret;
803
+ return _set.apply(void 0, [atom, _this.context].concat(args));
943
804
  });
944
- this.atomManager = atomManager;
945
- this.listenerManager = listenerManager;
946
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
+ };
947
810
  }
948
811
  return _createClass(StoreImpl, [{
949
- key: "_subSingleAtom",
950
- value: function _subSingleAtom(target$, cb$, options) {
951
- var _this2 = this,
952
- _this$options4;
953
- var unsub;
954
- var _fn2 = function fn() {
955
- var _options$signal2;
956
- var subscribed = true;
957
- var mounted = _this2.atomManager.mount(target$);
958
- mounted.listeners.add(cb$);
959
- unsub = function unsub() {
960
- var _this2$options;
961
- if (!subscribed) {
962
- return;
963
- }
964
- var _fn = function fn() {
965
- var _options$signal;
966
- subscribed = false;
967
- mounted.listeners["delete"](cb$);
968
- if (mounted.readDepts.size === 0 && mounted.listeners.size === 0) {
969
- _this2.atomManager.tryUnmount(target$);
970
- }
971
- options === null || options === void 0 || (_options$signal = options.signal) === null || _options$signal === void 0 || _options$signal.addEventListener('abort', _fn);
972
- };
973
- if ((_this2$options = _this2.options) !== null && _this2$options !== void 0 && (_this2$options = _this2$options.interceptor) !== null && _this2$options !== void 0 && _this2$options.unsub) {
974
- _this2.options.interceptor.unsub(target$, cb$, _fn);
975
-
976
- // subscribed should be false if interceptor called fn sync
977
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
978
- if (subscribed) {
979
- throw new Error('interceptor must call fn sync');
980
- }
981
- } else {
982
- _fn();
983
- }
984
- };
985
- options === null || options === void 0 || (_options$signal2 = options.signal) === null || _options$signal2 === void 0 || _options$signal2.addEventListener('abort', unsub);
986
- };
987
- if ((_this$options4 = this.options) !== null && _this$options4 !== void 0 && (_this$options4 = _this$options4.interceptor) !== null && _this$options4 !== void 0 && _this$options4.sub) {
988
- this.options.interceptor.sub(target$, cb$, _fn2);
989
- } else {
990
- _fn2();
991
- }
992
- if (!unsub) {
993
- throw new Error('interceptor must call fn sync');
994
- }
995
- return unsub;
996
- }
997
- }, {
998
812
  key: "sub",
999
813
  value: function sub(targets$, cb$, options) {
1000
- var _this3 = this;
1001
- if (Array.isArray(targets$) && targets$.length === 0) {
1002
- return function () {
1003
- return void 0;
1004
- };
1005
- }
1006
- if (Array.isArray(targets$) && targets$.length === 1) {
1007
- return this._subSingleAtom(targets$[0], cb$, options);
1008
- } else if (!Array.isArray(targets$)) {
1009
- return this._subSingleAtom(targets$, cb$, options);
1010
- }
1011
- var unsubscribes = new Set();
1012
- targets$.forEach(function (atom) {
1013
- unsubscribes.add(_this3._subSingleAtom(atom, cb$, options));
1014
- });
1015
- var unsub = function unsub() {
1016
- var _iterator2 = _createForOfIteratorHelper(unsubscribes),
1017
- _step2;
1018
- try {
1019
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
1020
- var unsubscribe = _step2.value;
1021
- unsubscribe();
1022
- }
1023
- } catch (err) {
1024
- _iterator2.e(err);
1025
- } finally {
1026
- _iterator2.f();
1027
- }
1028
- };
1029
- return unsub;
814
+ return _sub(targets$, cb$, this.context, options);
1030
815
  }
1031
816
  }]);
1032
817
  }();
@@ -1068,7 +853,10 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1068
853
  };
1069
854
  });
1070
855
  _defineProperty(_this, "getReadDependencies", function (atom) {
1071
- var atomState = _this.atomManager.readAtomState(atom);
856
+ var atomState = _this.context.stateMap.get(atom);
857
+ if (!atomState) {
858
+ return [atom];
859
+ }
1072
860
  if (!('dependencies' in atomState)) {
1073
861
  return [atom];
1074
862
  }
@@ -1080,7 +868,10 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1080
868
  });
1081
869
  _defineProperty(_this, "getReadDependents", function (atom) {
1082
870
  var _atomState$mounted$re, _atomState$mounted;
1083
- var atomState = _this.atomManager.readAtomState(atom);
871
+ var atomState = _this.context.stateMap.get(atom);
872
+ if (!atomState) {
873
+ return [atom];
874
+ }
1084
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) {
1085
876
  return _this.getReadDependents(key);
1086
877
  })));
@@ -1088,15 +879,15 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1088
879
  _defineProperty(_this, "getSubscribeGraph", function () {
1089
880
  var subscribedAtoms = Array.from(_this.mountedAtomListenersCount.keys());
1090
881
  return subscribedAtoms.map(function (atom) {
1091
- var atomState = _this.atomManager.readAtomState(atom);
1092
- // 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
1093
884
  var listeners = Array.from(atomState.mounted.listeners);
1094
885
  return [atom].concat(listeners);
1095
886
  });
1096
887
  });
1097
888
  _defineProperty(_this, "isMounted", function (atom) {
1098
- var mountState = _this.atomManager.readAtomState(atom);
1099
- return mountState.mounted !== undefined;
889
+ var mountState = _this.stateMap.get(atom);
890
+ return (mountState === null || mountState === void 0 ? void 0 : mountState.mounted) !== undefined;
1100
891
  });
1101
892
  return _this;
1102
893
  }
@@ -1104,11 +895,7 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1104
895
  return _createClass(DebugStoreImpl);
1105
896
  }(StoreImpl);
1106
897
  function createDebugStoreInternal(interceptor) {
1107
- var atomManager = new AtomManager({
1108
- interceptor: interceptor
1109
- });
1110
- var listenerManager = new ListenerManager();
1111
- return new DebugStoreImpl(atomManager, listenerManager, {
898
+ return new DebugStoreImpl({
1112
899
  interceptor: interceptor
1113
900
  });
1114
901
  }
@@ -1151,15 +938,15 @@ var ConsoleInterceptor = /*#__PURE__*/_createClass(function ConsoleInterceptor(w
1151
938
  console.groupEnd();
1152
939
  });
1153
940
  _defineProperty(this, "set", function (atom$, fn) {
941
+ var _console;
1154
942
  if (!_this.shouldLog(atom$, 'set')) {
1155
943
  fn();
1156
944
  return;
1157
945
  }
1158
- console.group('[R][SET] ' + atom$.toString());
1159
946
  for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
1160
947
  args[_key - 2] = arguments[_key];
1161
948
  }
1162
- console.log('arg:', args);
949
+ (_console = console).group.apply(_console, ['[R][SET] ' + atom$.toString(), '('].concat(args, [')']));
1163
950
  console.log('ret:', fn());
1164
951
  console.groupEnd();
1165
952
  });
@@ -1228,3 +1015,4 @@ function createDebugStore() {
1228
1015
  }
1229
1016
 
1230
1017
  export { createDebugStore };
1018
+ //# sourceMappingURL=index.js.map