ccstate 4.1.0 → 4.3.0

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