ccstate 4.0.0 → 4.2.0

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