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