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