ccstate 4.1.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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;
@@ -154,307 +159,6 @@ function _nonIterableRest() {
154
159
  function _nonIterableSpread() {
155
160
  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
161
  }
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
162
  function _slicedToArray(r, e) {
459
163
  return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
460
164
  }
@@ -483,536 +187,619 @@ function _unsupportedIterableToArray(r, a) {
483
187
  }
484
188
  }
485
189
 
190
+ function withSetInterceptor(fn, writable$, interceptor) {
191
+ if (!interceptor) {
192
+ return fn();
193
+ }
194
+ var result = {
195
+ called: false
196
+ };
197
+ var wrappedFn = function wrappedFn() {
198
+ result = {
199
+ called: true,
200
+ data: fn()
201
+ };
202
+ return result.data;
203
+ };
204
+ for (var _len = arguments.length, args = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
205
+ args[_key - 3] = arguments[_key];
206
+ }
207
+ if ('write' in writable$) {
208
+ interceptor.apply(void 0, [writable$, wrappedFn].concat(_toConsumableArray(args)));
209
+ } else {
210
+ interceptor(writable$, wrappedFn, args[0]);
211
+ }
212
+ if (!result.called) {
213
+ throw new Error('interceptor must call fn sync');
214
+ }
215
+ return result.data;
216
+ }
217
+ function withGetInterceptor(fn, signal, interceptor) {
218
+ if (!interceptor) {
219
+ return fn();
220
+ }
221
+ var result = {
222
+ called: false
223
+ };
224
+ interceptor(signal, function () {
225
+ result = {
226
+ called: true,
227
+ data: fn()
228
+ };
229
+ return result.data;
230
+ });
231
+ if (!result.called) {
232
+ throw new Error('interceptor must call fn sync');
233
+ }
234
+ return result.data;
235
+ }
236
+ function withComputedInterceptor(fn, signal, interceptor) {
237
+ if (!interceptor) {
238
+ return fn();
239
+ }
240
+ var result = {
241
+ called: false
242
+ };
243
+ interceptor(signal, function () {
244
+ result = {
245
+ called: true,
246
+ data: fn()
247
+ };
248
+ return result.data.val;
249
+ });
250
+ if (!result.called) {
251
+ throw new Error('interceptor must call fn sync');
252
+ }
253
+ return result.data;
254
+ }
255
+ function withGeValInterceptor(fn, signal, interceptor) {
256
+ if (!interceptor) {
257
+ return fn();
258
+ }
259
+ var result = {
260
+ called: false
261
+ };
262
+ interceptor(signal, function () {
263
+ result = {
264
+ called: true,
265
+ data: fn()
266
+ };
267
+ return result.data;
268
+ });
269
+ if (!result.called) {
270
+ throw new Error('interceptor must call fn sync');
271
+ }
272
+ return result.data;
273
+ }
274
+ function withSubInterceptor(fn, signal, callback$, interceptor) {
275
+ if (!interceptor) {
276
+ fn();
277
+ return;
278
+ }
279
+ var result = {
280
+ called: false
281
+ };
282
+ interceptor(signal, callback$, function () {
283
+ result = {
284
+ called: true,
285
+ data: undefined
286
+ };
287
+ fn();
288
+ });
289
+ if (!result.called) {
290
+ throw new Error('interceptor must call fn sync');
291
+ }
292
+ }
293
+ function withUnsubInterceptor(fn, signal, callback$, interceptor) {
294
+ if (!interceptor) {
295
+ fn();
296
+ return;
297
+ }
298
+ var result = {
299
+ called: false
300
+ };
301
+ interceptor(signal, callback$, function () {
302
+ result = {
303
+ called: true,
304
+ data: undefined
305
+ };
306
+ fn();
307
+ });
308
+ if (!result.called) {
309
+ throw new Error('interceptor must call fn sync');
310
+ }
311
+ }
312
+ function withNotifyInterceptor(fn, callback$, interceptor) {
313
+ if (!interceptor) {
314
+ return fn();
315
+ }
316
+ var result = {
317
+ called: false
318
+ };
319
+ interceptor(callback$, function () {
320
+ result = {
321
+ called: true,
322
+ data: fn()
323
+ };
324
+ return result.data;
325
+ });
326
+ if (!result.called) {
327
+ throw new Error('interceptor must call fn sync');
328
+ }
329
+ return result.data;
330
+ }
331
+
486
332
  function canReadAsCompute(atom) {
487
333
  return 'read' in atom;
488
334
  }
489
335
  function isComputedState(state) {
490
336
  return 'dependencies' in state;
491
337
  }
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();
338
+
339
+ function checkEpoch(readComputed, computedState, context, mutation) {
340
+ var _iterator = _createForOfIteratorHelper(computedState.dependencies.entries()),
341
+ _step;
342
+ try {
343
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
344
+ var _context$stateMap$get;
345
+ var _step$value = _slicedToArray(_step.value, 2),
346
+ dep = _step$value[0],
347
+ epoch = _step$value[1];
348
+ 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;
349
+ if (depEpoch !== epoch) {
350
+ return false;
521
351
  }
522
- return atomState;
523
- });
524
- this.options = options;
352
+ }
353
+ } catch (err) {
354
+ _iterator.e(err);
355
+ } finally {
356
+ _iterator.f();
525
357
  }
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);
358
+ return true;
359
+ }
360
+ function tryGetCached(readComputed, computed$, context, mutation) {
361
+ var signalState = context.stateMap.get(computed$);
362
+ if (!signalState) {
363
+ return undefined;
364
+ }
365
+ if (mutation !== null && mutation !== void 0 && mutation.dirtyMarkers.has(computed$.id)) {
366
+ return undefined;
367
+ }
368
+ if (signalState.mounted) {
369
+ return signalState;
370
+ }
371
+ if (checkEpoch(readComputed, signalState, context, mutation)) {
372
+ return signalState;
373
+ }
374
+ return undefined;
375
+ }
376
+ function wrapGet(readSignal, mount, callerComputed$, callerState, context, mutation) {
377
+ var readDeps = new Map();
378
+ return [function (dep$) {
379
+ var depState = readSignal(dep$, context, mutation);
380
+ if (callerState.dependencies === readDeps) {
381
+ readDeps.set(dep$, depState.epoch);
382
+ var callerMounted = !!callerState.mounted;
383
+ if (callerMounted && !depState.mounted) {
384
+ mount(dep$, context, mutation).readDepts.add(callerComputed$);
385
+ } else if (callerMounted && depState.mounted) {
386
+ depState.mounted.readDepts.add(callerComputed$);
539
387
  }
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');
388
+ }
389
+ return depState.val;
390
+ }, readDeps];
391
+ }
392
+ function getOrInitComputedState(computed$, context) {
393
+ var computedState = context.stateMap.get(computed$);
394
+ if (!computedState) {
395
+ computedState = {
396
+ dependencies: new Map(),
397
+ epoch: -1
398
+ };
399
+ context.stateMap.set(computed$, computedState);
400
+ }
401
+ return computedState;
402
+ }
403
+ function cleanupMissingDependencies(unmount, computed$, lastDeps, currDeps, context, mutation) {
404
+ var _iterator2 = _createForOfIteratorHelper(lastDeps.keys()),
405
+ _step2;
406
+ try {
407
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
408
+ var key = _step2.value;
409
+ if (!currDeps.has(key)) {
410
+ var _depState$mounted;
411
+ var depState = context.stateMap.get(key);
412
+ depState === null || depState === void 0 || (_depState$mounted = depState.mounted) === null || _depState$mounted === void 0 || _depState$mounted.readDepts["delete"](computed$);
413
+ unmount(key, context, mutation);
552
414
  }
553
- return result.data;
554
415
  }
416
+ } catch (err) {
417
+ _iterator2.e(err);
418
+ } finally {
419
+ _iterator2.f();
420
+ }
421
+ }
422
+ function evaluateComputed(readSignal, mount, unmount, computed$, context, mutation) {
423
+ var computedState = getOrInitComputedState(computed$, context);
424
+ var lastDeps = computedState.dependencies;
425
+ var _wrapGet = wrapGet(readSignal, mount, computed$, computedState, context, mutation),
426
+ _wrapGet2 = _slicedToArray(_wrapGet, 2),
427
+ _get = _wrapGet2[0],
428
+ dependencies = _wrapGet2[1];
429
+ computedState.dependencies = dependencies;
430
+ var evalVal = computed$.read(function (depAtom) {
431
+ var _context$interceptor;
432
+ return withGeValInterceptor(function () {
433
+ return _get(depAtom);
434
+ }, depAtom, (_context$interceptor = context.interceptor) === null || _context$interceptor === void 0 ? void 0 : _context$interceptor.get);
555
435
  }, {
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);
436
+ get signal() {
437
+ var _computedState$abortC, _computed$$debugLabel;
438
+ (_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"));
439
+ computedState.abortController = new AbortController();
440
+ return computedState.abortController.signal;
441
+ }
442
+ });
443
+ cleanupMissingDependencies(unmount, computed$, lastDeps, dependencies, context, mutation);
444
+ computedState.val = evalVal;
445
+ computedState.epoch += 1;
446
+ return computedState;
447
+ }
575
448
 
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);
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);
584
469
  }
470
+ } catch (err) {
471
+ _iterator2.e(err);
472
+ } finally {
473
+ _iterator2.f();
585
474
  }
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
475
  }
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);
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);
630
515
  }
516
+ } catch (err) {
517
+ _iterator5.e(err);
518
+ } finally {
519
+ _iterator5.f();
631
520
  }
632
521
  }
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);
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
+ }
689
528
  }
690
529
  }
691
- return atomState.mounted;
530
+ } catch (err) {
531
+ _iterator4.e(err);
532
+ } finally {
533
+ _iterator4.f();
692
534
  }
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);
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 _ref, _context$stateMap$get;
544
+ var newValue = typeof val === 'function' ? val((_ref = (_context$stateMap$get = context.stateMap.get(signal$)) === null || _context$stateMap$get === void 0 ? void 0 : _context$stateMap$get.val) !== null && _ref !== void 0 ? _ref : signal$.init) : val;
545
+ var signalState = context.stateMap.get(signal$);
546
+ if (!signalState) {
547
+ context.stateMap.set(signal$, {
548
+ val: newValue,
549
+ epoch: 0
550
+ });
551
+ return;
552
+ }
553
+ signalState.val = newValue;
554
+ signalState.epoch += 1;
555
+ propagationChanges(readComputed, signalState, context, mutation);
556
+ return undefined;
557
+ }
558
+ function set(readComputed, writable$, context, mutation) {
559
+ if ('read' in writable$) {
560
+ return;
561
+ }
562
+ for (var _len = arguments.length, args = new Array(_len > 4 ? _len - 4 : 0), _key = 4; _key < _len; _key++) {
563
+ args[_key - 4] = arguments[_key];
564
+ }
565
+ if ('write' in writable$) {
566
+ return writable$.write.apply(writable$, [mutation.visitor].concat(_toConsumableArray(args)));
567
+ }
568
+ innerSetState(readComputed, writable$, context, mutation, args[0]);
569
+ return;
570
+ }
571
+
572
+ /**
573
+ * Creates a mutation operation context. The Mutation remains unique throughout
574
+ * the mutation cycle and can track side effects produced by this mutation operation
575
+ *
576
+ * This tracking is implemented by coloring the visitor function, so the Mutation
577
+ * needs to wrap get & set functions and ensure that all get & set operations
578
+ * executed in the mutation context come from the same Mutation
579
+ *
580
+ * @param context
581
+ * @param get
582
+ * @param set
583
+ * @returns
584
+ */
585
+ function createMutation(context, _get, _set) {
586
+ var mutation = {
587
+ dirtyMarkers: new Set(),
588
+ pendingListeners: new Set(),
589
+ visitor: {
590
+ get: function get(signal$) {
591
+ return _get(signal$, context, mutation);
592
+ },
593
+ set: function set(signal$) {
594
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
595
+ args[_key2 - 1] = arguments[_key2];
710
596
  }
597
+ return _set.apply(void 0, [signal$, context].concat(args));
711
598
  }
712
- atomState.mounted = undefined;
713
599
  }
714
- }, {
715
- key: "inited",
716
- value: function inited(atom) {
717
- return this.atomStateMap.has(atom);
718
- }
719
- }]);
720
- }();
721
- var ListenerManager = /*#__PURE__*/function () {
722
- function ListenerManager() {
723
- _classCallCheck(this, ListenerManager);
724
- _defineProperty(this, "pendingListeners", new Set());
600
+ };
601
+ return mutation;
602
+ }
603
+
604
+ function readState(state, context) {
605
+ var atomState = context.stateMap.get(state);
606
+ if (!atomState) {
607
+ var initState = {
608
+ val: state.init,
609
+ epoch: 0
610
+ };
611
+ context.stateMap.set(state, initState);
612
+ return initState;
725
613
  }
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();
614
+ return atomState;
615
+ }
616
+
617
+ function unmountComputedDependencies(computed$, computedState, context, mutation) {
618
+ for (var _i = 0, _Array$from = Array.from(computedState.dependencies); _i < _Array$from.length; _i++) {
619
+ var _context$stateMap$get;
620
+ var _Array$from$_i = _slicedToArray(_Array$from[_i], 1),
621
+ dep = _Array$from$_i[0];
622
+ (_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$);
623
+ unmount(dep, context);
624
+ }
625
+ }
626
+ function mountComputedDependencies(readSignal, computed$, computedState, context, mutation) {
627
+ for (var _i2 = 0, _Array$from2 = Array.from(computedState.dependencies); _i2 < _Array$from2.length; _i2++) {
628
+ var _Array$from2$_i = _slicedToArray(_Array$from2[_i2], 1),
629
+ dep = _Array$from2$_i[0];
630
+ var mounted = mount$1(readSignal, dep, context, mutation);
631
+ mounted.readDepts.add(computed$);
632
+ }
633
+ }
634
+ function initMount(readSignal, signal$, context, mutation) {
635
+ var _context$interceptor, _context$interceptor$, _signalState$mounted;
636
+ (_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$);
637
+ var signalState = readSignal(signal$, context, mutation);
638
+ signalState.mounted = (_signalState$mounted = signalState.mounted) !== null && _signalState$mounted !== void 0 ? _signalState$mounted : {
639
+ listeners: new Set(),
640
+ readDepts: new Set()
641
+ };
642
+ if (isComputedState(signalState)) {
643
+ mountComputedDependencies(readSignal, signal$, signalState, context, mutation);
644
+ }
645
+ return signalState.mounted;
646
+ }
647
+ function mount$1(readSignal, signal$, context, mutation) {
648
+ var _context$stateMap$get2;
649
+ var mounted = (_context$stateMap$get2 = context.stateMap.get(signal$)) === null || _context$stateMap$get2 === void 0 ? void 0 : _context$stateMap$get2.mounted;
650
+ if (mounted) {
651
+ return mounted;
652
+ }
653
+ return initMount(readSignal, signal$, context, mutation);
654
+ }
655
+ function doUnmount(signal$, signalState, context, mutation) {
656
+ var _context$interceptor2, _context$interceptor3;
657
+ (_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$);
658
+ if (isComputedState(signalState)) {
659
+ unmountComputedDependencies(signal$, signalState, context);
660
+ }
661
+ signalState.mounted = undefined;
662
+ }
663
+ function unmount(signal$, context, mutation) {
664
+ var signalState = context.stateMap.get(signal$);
665
+ if (!(signalState !== null && signalState !== void 0 && signalState.mounted) || signalState.mounted.listeners.size || signalState.mounted.readDepts.size) {
666
+ return;
667
+ }
668
+ doUnmount(signal$, signalState, context);
669
+ }
670
+ function subSingleSignal(readSignal, signal$, callback$, context, signal) {
671
+ var _context$interceptor5;
672
+ withSubInterceptor(function () {
673
+ var mounted = mount$1(readSignal, signal$, context);
674
+ mounted.listeners.add(callback$);
675
+ var unsub = function unsub() {
676
+ var _context$interceptor4;
677
+ withUnsubInterceptor(function () {
678
+ mounted.listeners["delete"](callback$);
679
+ if (mounted.readDepts.size === 0 && mounted.listeners.size === 0) {
680
+ unmount(signal$, context);
765
681
  }
766
- queue = nextQueue;
767
- }
682
+ }, signal$, callback$, (_context$interceptor4 = context.interceptor) === null || _context$interceptor4 === void 0 ? void 0 : _context$interceptor4.unsub);
683
+ };
684
+ signal.addEventListener('abort', unsub, {
685
+ once: true
686
+ });
687
+ }, signal$, callback$, (_context$interceptor5 = context.interceptor) === null || _context$interceptor5 === void 0 ? void 0 : _context$interceptor5.sub);
688
+ }
689
+ function notify(context, mutation) {
690
+ var pendingListeners = mutation.pendingListeners;
691
+ mutation.pendingListeners = new Set();
692
+ var _iterator = _createForOfIteratorHelper(pendingListeners),
693
+ _step;
694
+ try {
695
+ var _loop = function _loop() {
696
+ var _context$interceptor6;
697
+ var listener = _step.value;
698
+ withNotifyInterceptor(function () {
699
+ return listener.write(mutation.visitor);
700
+ }, listener, (_context$interceptor6 = context.interceptor) === null || _context$interceptor6 === void 0 ? void 0 : _context$interceptor6.notify);
701
+ };
702
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
703
+ _loop();
768
704
  }
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
- }();
705
+ } catch (err) {
706
+ _iterator.e(err);
707
+ } finally {
708
+ _iterator.f();
709
+ }
710
+ }
811
711
 
712
+ var _readComputed = function readComputed(computed$, context, mutation) {
713
+ var _context$interceptor;
714
+ var cachedState = tryGetCached(_readComputed, computed$, context, mutation);
715
+ if (cachedState) {
716
+ return cachedState;
717
+ }
718
+ mutation === null || mutation === void 0 || mutation.dirtyMarkers["delete"](computed$.id);
719
+ return withComputedInterceptor(function () {
720
+ return evaluateComputed(readSignal, mount, unmount, computed$, context, mutation);
721
+ }, computed$, (_context$interceptor = context.interceptor) === null || _context$interceptor === void 0 ? void 0 : _context$interceptor.computed);
722
+ };
723
+ function readSignal(signal$, context, mutation) {
724
+ if (canReadAsCompute(signal$)) {
725
+ return _readComputed(signal$, context, mutation);
726
+ }
727
+ return readState(signal$, context);
728
+ }
729
+ function mount(signal$, context, mutation) {
730
+ return mount$1(readSignal, signal$, context, mutation);
731
+ }
732
+ function _sub(signals$, callback$, context, options) {
733
+ if (Array.isArray(signals$) && signals$.length === 0) {
734
+ return function () {
735
+ return void 0;
736
+ };
737
+ }
738
+ var controller = new AbortController();
739
+ var signal = options !== null && options !== void 0 && options.signal ? AbortSignal.any([controller.signal, options.signal]) : controller.signal;
740
+ if (!Array.isArray(signals$)) {
741
+ subSingleSignal(readSignal, signals$, callback$, context, signal);
742
+ } else {
743
+ signals$.forEach(function (atom) {
744
+ subSingleSignal(readSignal, atom, callback$, context, signal);
745
+ });
746
+ }
747
+ return function () {
748
+ controller.abort();
749
+ };
750
+ }
751
+ var get = function get(signal, context, mutation) {
752
+ var _context$interceptor2;
753
+ return withGetInterceptor(function () {
754
+ return readSignal(signal, context, mutation).val;
755
+ }, signal, (_context$interceptor2 = context.interceptor) === null || _context$interceptor2 === void 0 ? void 0 : _context$interceptor2.get);
756
+ };
757
+ var _set = function set$1(atom, context) {
758
+ var _context$interceptor3;
759
+ for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
760
+ args[_key - 2] = arguments[_key];
761
+ }
762
+ return withSetInterceptor.apply(void 0, [function () {
763
+ var mutation = createMutation(context, get, _set);
764
+ var ret;
765
+ try {
766
+ ret = set.apply(void 0, [_readComputed, atom, context, mutation].concat(args));
767
+ } finally {
768
+ notify(context, mutation);
769
+ }
770
+ return ret;
771
+ }, atom, (_context$interceptor3 = context.interceptor) === null || _context$interceptor3 === void 0 ? void 0 : _context$interceptor3.set].concat(args));
772
+ };
812
773
  var StoreImpl = /*#__PURE__*/function () {
813
- function StoreImpl(atomManager, listenerManager, options) {
814
- var _this = this;
774
+ function StoreImpl(options) {
775
+ var _this = this,
776
+ _this$options;
815
777
  _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
- });
778
+ _defineProperty(this, "stateMap", new WeakMap());
842
779
  _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
- }
780
+ return get(atom, _this.context);
896
781
  });
897
782
  _defineProperty(this, "set", function (atom) {
898
- var _this$options3;
899
783
  for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
900
784
  args[_key2 - 1] = arguments[_key2];
901
785
  }
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;
786
+ return _set.apply(void 0, [atom, _this.context].concat(args));
922
787
  });
923
- this.atomManager = atomManager;
924
- this.listenerManager = listenerManager;
925
788
  this.options = options;
789
+ this.context = {
790
+ stateMap: this.stateMap,
791
+ interceptor: (_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.interceptor
792
+ };
926
793
  }
927
794
  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
795
  key: "sub",
978
796
  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;
797
+ return _sub(targets$, cb$, this.context, options);
1009
798
  }
1010
799
  }]);
1011
800
  }();
1012
801
  function createStore() {
1013
- var atomManager = new AtomManager();
1014
- var listenerManager = new ListenerManager();
1015
- return new StoreImpl(atomManager, listenerManager);
802
+ return new StoreImpl();
1016
803
  }
1017
804
  var defaultStore = undefined;
1018
805
  function getDefaultStore() {