@but212/atom-effect 0.19.0 → 0.19.1

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/dist/index.mjs CHANGED
@@ -1,17 +1,19 @@
1
1
  const tt = {
2
2
  ONE_SECOND_MS: 1e3
3
- }, m = {
3
+ }, k = {
4
4
  IDLE: "idle",
5
5
  PENDING: "pending",
6
6
  RESOLVED: "resolved",
7
7
  REJECTED: "rejected"
8
- }, M = {
8
+ }, w = {
9
9
  DISPOSED: 1
10
- }, a = {
11
- ...M,
10
+ }, l = {
11
+ ...w,
12
12
  EXECUTING: 8
13
13
  }, o = {
14
- ...M,
14
+ ...w,
15
+ /** Marker bit: identifies this node as a computed. */
16
+ IS_COMPUTED: 2,
15
17
  DIRTY: 8,
16
18
  IDLE: 16,
17
19
  PENDING: 32,
@@ -20,10 +22,10 @@ const tt = {
20
22
  RECOMPUTING: 256,
21
23
  HAS_ERROR: 512
22
24
  }, d = {
23
- ...M,
25
+ ...w,
24
26
  SYNC: 8,
25
27
  NOTIFICATION_SCHEDULED: 16
26
- }, pt = {
28
+ }, It = {
27
29
  MAX_SIZE: 1e3,
28
30
  WARMUP_SIZE: 100,
29
31
  ENABLE_STATS: !1
@@ -40,7 +42,7 @@ const tt = {
40
42
  BATCH_QUEUE_SHRINK_THRESHOLD: 1e3
41
43
  }, et = {
42
44
  WARN_INFINITE_LOOP: !0
43
- }, O = 1073741823, _ = typeof process < "u" && process.env && process.env.NODE_ENV !== "production" || typeof __DEV__ < "u" && !!__DEV__, st = Object.freeze([]);
45
+ }, y = 1073741823, _ = typeof process < "u" && process.env && process.env.NODE_ENV !== "production" || typeof __DEV__ < "u" && !!__DEV__, st = Object.freeze([]);
44
46
  class T extends Error {
45
47
  constructor(t, e = null, s = !0) {
46
48
  super(t), this.cause = e, this.recoverable = s, this.timestamp = /* @__PURE__ */ new Date(), this.name = "AtomError";
@@ -56,7 +58,7 @@ class S extends T {
56
58
  super(t, e, !1), this.name = "EffectError";
57
59
  }
58
60
  }
59
- class N extends T {
61
+ class b extends T {
60
62
  constructor(t, e = null) {
61
63
  super(t, e, !1), this.name = "SchedulerError";
62
64
  }
@@ -104,7 +106,7 @@ function j(i, t, e) {
104
106
  const c = e[r];
105
107
  if (c && c.fn === n) return;
106
108
  }
107
- e.push(new k(n, void 0));
109
+ e.push(new P(n, void 0));
108
110
  return;
109
111
  }
110
112
  if ("addDependency" in t) {
@@ -116,33 +118,29 @@ function j(i, t, e) {
116
118
  const h = e[n];
117
119
  if (h && h.sub === s) return;
118
120
  }
119
- e.push(new k(void 0, s));
121
+ e.push(new P(void 0, s));
120
122
  }
121
123
  function ht(i, t, e) {
122
- for (let s = 0, n = t.length; s < n; s++) {
123
- const r = t[s];
124
- r && (r.node._tempUnsub = r.unsub);
125
- }
126
- for (let s = 0, n = i.length; s < n; s++) {
127
- const r = i[s];
128
- if (!r) continue;
129
- const h = r.node;
130
- h._tempUnsub !== void 0 ? (r.unsub = h._tempUnsub, h._tempUnsub = void 0) : (g.checkCircular(h, e), r.unsub = h.subscribe(e));
131
- }
132
- for (let s = 0, n = t.length; s < n; s++) {
133
- const r = t[s];
134
- if (r) {
135
- const h = r.node;
136
- h._tempUnsub !== void 0 && (h._tempUnsub(), h._tempUnsub = void 0), r.unsub = void 0;
137
- }
138
- }
124
+ const s = /* @__PURE__ */ new Map();
125
+ for (let n = 0, r = t.length; n < r; n++) {
126
+ const h = t[n];
127
+ h?.unsub && (s.set(h.node, h.unsub), h.unsub = void 0);
128
+ }
129
+ for (let n = 0, r = i.length; n < r; n++) {
130
+ const h = i[n];
131
+ if (!h) continue;
132
+ const c = h.node, E = s.get(c);
133
+ E !== void 0 ? (h.unsub = E, s.delete(c)) : (g.checkCircular(c, e), h.unsub = c.subscribe(e));
134
+ }
135
+ for (const n of s.values())
136
+ n();
139
137
  }
140
- class b {
138
+ class R {
141
139
  constructor(t, e, s = void 0) {
142
140
  this.node = t, this.version = e, this.unsub = s;
143
141
  }
144
142
  }
145
- class k {
143
+ class P {
146
144
  constructor(t, e) {
147
145
  this.fn = t, this.sub = e;
148
146
  }
@@ -181,7 +179,7 @@ function C(i, t, e) {
181
179
  }
182
180
  class q {
183
181
  constructor() {
184
- this.flags = 0, this.version = 0, this._lastSeenEpoch = -1, this._modifiedAtEpoch = -1, this.id = ot() & O, this._tempUnsub = void 0;
182
+ this.flags = 0, this.version = 0, this._lastSeenEpoch = -1, this._modifiedAtEpoch = -1, this.id = ot() & y;
185
183
  }
186
184
  }
187
185
  class $ extends q {
@@ -203,7 +201,7 @@ class $ extends q {
203
201
  return _ && console.warn("Duplicate subscription ignored."), () => {
204
202
  };
205
203
  }
206
- const n = new k(
204
+ const n = new P(
207
205
  e ? t : void 0,
208
206
  e ? void 0 : t
209
207
  );
@@ -235,144 +233,131 @@ class $ extends q {
235
233
  console.error(C(t, T, u.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED));
236
234
  }
237
235
  }
238
- let R = 0;
239
- const J = () => (R = R + 1 & O || 1, R), ct = () => R, P = (i) => i + 1 & O;
240
- let v = 0, w = 0, A = !1;
236
+ let A = 0;
237
+ const J = () => (A = A + 1 & y || 1, A), ct = () => A, v = (i) => i + 1 & y;
238
+ let M = 0, B = 0, L = !1;
241
239
  function H() {
242
- return A ? (_ && console.warn("startFlush() called during flush - ignored"), !1) : (A = !0, v = v + 1 & O || 1, w = 0, !0);
240
+ return L ? (_ && console.warn("startFlush() called during flush - ignored"), !1) : (L = !0, M = M + 1 & y || 1, B = 0, !0);
243
241
  }
244
242
  const X = () => {
245
- A = !1;
246
- }, ut = () => A ? ++w : 0, f = {
247
- /** Queue buffer */
248
- _queueBuffer: [[], []],
249
- _bufferIndex: 0,
250
- _size: 0,
251
- /** Epoch counter */
252
- _epoch: 0,
253
- /** State flags */
254
- _isProcessing: !1,
255
- _isBatching: !1,
256
- _isFlushingSync: !1,
257
- /** Batching state */
258
- _batchDepth: 0,
259
- _batchQueue: [],
260
- _batchQueueSize: 0,
261
- /** Config */
262
- _maxFlushIterations: I.MAX_FLUSH_ITERATIONS,
243
+ L = !1;
244
+ }, ut = () => L ? ++B : 0;
245
+ class _t {
246
+ constructor() {
247
+ this._queueBuffer = [[], []], this._bufferIndex = 0, this._size = 0, this._epoch = 0, this._isProcessing = !1, this._isBatching = !1, this._isFlushingSync = !1, this._batchDepth = 0, this._batchQueue = [], this._batchQueueSize = 0, this._maxFlushIterations = I.MAX_FLUSH_ITERATIONS, this.onOverflow = null, this._boundRunLoop = this._runLoop.bind(this);
248
+ }
263
249
  get phase() {
264
250
  return this._isProcessing || this._isFlushingSync ? 2 : this._isBatching ? 1 : 0;
265
- },
251
+ }
266
252
  get queueSize() {
267
253
  return this._size;
268
- },
254
+ }
269
255
  get isBatching() {
270
256
  return this._isBatching;
271
- },
257
+ }
272
258
  /**
273
259
  * Schedules job.
274
260
  */
275
- schedule(i) {
276
- if (_ && typeof i != "function")
277
- throw new N("Scheduler callback must be a function");
278
- if (i._nextEpoch !== this._epoch) {
279
- if (i._nextEpoch = this._epoch, this._isBatching || this._isFlushingSync) {
280
- this._batchQueue[this._batchQueueSize++] = i;
261
+ schedule(t) {
262
+ if (_ && typeof t != "function")
263
+ throw new b("Scheduler callback must be a function");
264
+ if (t._nextEpoch !== this._epoch) {
265
+ if (t._nextEpoch = this._epoch, this._isBatching || this._isFlushingSync) {
266
+ this._batchQueue[this._batchQueueSize++] = t;
281
267
  return;
282
268
  }
283
- this._queueBuffer[this._bufferIndex][this._size++] = i, this._isProcessing || this._flush();
269
+ this._queueBuffer[this._bufferIndex][this._size++] = t, this._isProcessing || this._flush();
284
270
  }
285
- },
271
+ }
286
272
  /**
287
273
  * Triggers flush.
288
274
  */
289
275
  _flush() {
290
- this._isProcessing || this._size === 0 || (this._isProcessing = !0, queueMicrotask(this._runLoop));
291
- },
276
+ this._isProcessing || this._size === 0 || (this._isProcessing = !0, queueMicrotask(this._boundRunLoop));
277
+ }
292
278
  /**
293
279
  * Scheduler loop.
294
280
  */
295
- _runLoop: () => {
281
+ _runLoop() {
296
282
  try {
297
- if (f._size === 0) return;
298
- const i = H();
299
- f._drainQueue(), i && X();
283
+ if (this._size === 0) return;
284
+ const t = H();
285
+ this._drainQueue(), t && X();
300
286
  } finally {
301
- f._isProcessing = !1, f._size > 0 && !f._isBatching && f._flush();
287
+ this._isProcessing = !1, this._size > 0 && !this._isBatching && this._flush();
302
288
  }
303
- },
289
+ }
304
290
  _flushSync() {
305
291
  this._isFlushingSync = !0;
306
- const i = H();
292
+ const t = H();
307
293
  try {
308
294
  this._mergeBatchQueue(), this._drainQueue();
309
295
  } finally {
310
- this._isFlushingSync = !1, i && X();
296
+ this._isFlushingSync = !1, t && X();
311
297
  }
312
- },
298
+ }
313
299
  _mergeBatchQueue() {
314
300
  if (this._batchQueueSize === 0) return;
315
- const i = ++this._epoch, t = this._batchQueue, e = this._queueBuffer[this._bufferIndex];
316
- let s = this._size;
317
- for (let n = 0; n < this._batchQueueSize; n++) {
318
- const r = t[n];
319
- r._nextEpoch !== i && (r._nextEpoch = i, e[s++] = r);
301
+ const t = ++this._epoch, e = this._batchQueue, s = this._queueBuffer[this._bufferIndex];
302
+ let n = this._size;
303
+ for (let r = 0; r < this._batchQueueSize; r++) {
304
+ const h = e[r];
305
+ h._nextEpoch !== t && (h._nextEpoch = t, s[n++] = h);
320
306
  }
321
- this._size = s, this._batchQueueSize = 0, t.length > I.BATCH_QUEUE_SHRINK_THRESHOLD && (t.length = 0);
322
- },
307
+ this._size = n, this._batchQueueSize = 0, e.length > I.BATCH_QUEUE_SHRINK_THRESHOLD && (e.length = 0);
308
+ }
323
309
  _drainQueue() {
324
- let i = 0;
310
+ let t = 0;
325
311
  for (; this._size > 0; ) {
326
- if (++i > this._maxFlushIterations) {
312
+ if (++t > this._maxFlushIterations) {
327
313
  this._handleFlushOverflow();
328
314
  return;
329
315
  }
330
316
  this._processQueue(), this._mergeBatchQueue();
331
317
  }
332
- },
318
+ }
333
319
  _processQueue() {
334
- const i = this._bufferIndex, t = this._queueBuffer[i], e = this._size;
335
- this._bufferIndex = i ^ 1, this._size = 0, this._epoch++;
336
- for (let s = 0; s < e; s++)
320
+ const t = this._bufferIndex, e = this._queueBuffer[t], s = this._size;
321
+ this._bufferIndex = t ^ 1, this._size = 0, this._epoch++;
322
+ for (let n = 0; n < s; n++)
337
323
  try {
338
- t[s]();
339
- } catch (n) {
340
- console.error(new N("Error occurred during scheduler execution", n));
324
+ e[n]();
325
+ } catch (r) {
326
+ console.error(new b("Error occurred during scheduler execution", r));
341
327
  }
342
- t.length = 0;
343
- },
344
- /** Overflow callback */
345
- onOverflow: null,
328
+ e.length = 0;
329
+ }
346
330
  _handleFlushOverflow() {
347
- const i = this._size + this._batchQueueSize;
331
+ const t = this._size + this._batchQueueSize;
348
332
  if (console.error(
349
- new N(
350
- u.SCHEDULER_FLUSH_OVERFLOW(this._maxFlushIterations, i)
333
+ new b(
334
+ u.SCHEDULER_FLUSH_OVERFLOW(this._maxFlushIterations, t)
351
335
  )
352
336
  ), this._size = 0, this._queueBuffer[this._bufferIndex].length = 0, this._batchQueueSize = 0, this.onOverflow)
353
337
  try {
354
- this.onOverflow(i);
338
+ this.onOverflow(t);
355
339
  } catch {
356
340
  }
357
- },
341
+ }
358
342
  startBatch() {
359
343
  this._batchDepth++, this._isBatching = !0;
360
- },
344
+ }
361
345
  endBatch() {
362
346
  if (this._batchDepth === 0) {
363
347
  _ && console.warn("endBatch() called without matching startBatch(). Ignoring.");
364
348
  return;
365
349
  }
366
350
  --this._batchDepth === 0 && (this._flushSync(), this._isBatching = !1);
367
- },
368
- setMaxFlushIterations(i) {
369
- if (i < I.MIN_FLUSH_ITERATIONS)
370
- throw new N(
351
+ }
352
+ setMaxFlushIterations(t) {
353
+ if (t < I.MIN_FLUSH_ITERATIONS)
354
+ throw new b(
371
355
  `Max flush iterations must be at least ${I.MIN_FLUSH_ITERATIONS}`
372
356
  );
373
- this._maxFlushIterations = i;
357
+ this._maxFlushIterations = t;
374
358
  }
375
- }, E = {
359
+ }
360
+ const O = new _t(), f = {
376
361
  /** Active listener. */
377
362
  current: null,
378
363
  /**
@@ -392,35 +377,35 @@ const X = () => {
392
377
  }
393
378
  }
394
379
  };
395
- function It(i) {
396
- const t = E.current;
380
+ function Dt(i) {
381
+ const t = f.current;
397
382
  if (t === null) return i();
398
- E.current = null;
383
+ f.current = null;
399
384
  try {
400
385
  return i();
401
386
  } finally {
402
- E.current = t;
387
+ f.current = t;
403
388
  }
404
389
  }
405
- class _t extends $ {
390
+ class lt extends $ {
406
391
  constructor(t, e) {
407
392
  super(), this._pendingOldValue = void 0, this._notifyTask = void 0, this._subscribers = [], this._value = t, e && (this.flags |= d.SYNC), g.attachDebugInfo(this, "atom", this.id);
408
393
  }
409
394
  get value() {
410
- const t = E.current;
395
+ const t = f.current;
411
396
  return t && j(this, t, this._subscribers), this._value;
412
397
  }
413
398
  set value(t) {
414
399
  const e = this._value;
415
400
  if (Object.is(e, t)) return;
416
- this._value = t, this.version = P(this.version);
401
+ this._value = t, this.version = v(this.version);
417
402
  const s = this.flags;
418
403
  if (!(this._subscribers.length === 0 || s & d.NOTIFICATION_SCHEDULED)) {
419
- if (this._pendingOldValue = e, this.flags = s | d.NOTIFICATION_SCHEDULED, s & d.SYNC && !f.isBatching) {
404
+ if (this._pendingOldValue = e, this.flags = s | d.NOTIFICATION_SCHEDULED, s & d.SYNC && !O.isBatching) {
420
405
  this._flushNotifications();
421
406
  return;
422
407
  }
423
- this._notifyTask || (this._notifyTask = () => this._flushNotifications()), f.schedule(this._notifyTask);
408
+ this._notifyTask || (this._notifyTask = () => this._flushNotifications()), O.schedule(this._notifyTask);
424
409
  }
425
410
  }
426
411
  /**
@@ -440,10 +425,10 @@ class _t extends $ {
440
425
  this.flags & d.DISPOSED || (this._subscribers.length = 0, this.flags |= d.DISPOSED, this._value = void 0, this._pendingOldValue = void 0, this._notifyTask = void 0);
441
426
  }
442
427
  }
443
- function Dt(i, t = {}) {
444
- return new _t(i, t.sync ?? !1);
428
+ function St(i, t = {}) {
429
+ return new lt(i, t.sync ?? !1);
445
430
  }
446
- class lt {
431
+ class at {
447
432
  /**
448
433
  * @param limit - Max unique arrays to hold (default: 50). Prevents the pool itself from consuming too much memory.
449
434
  * @param capacity - Max length of an array to accept (default: 256).
@@ -510,42 +495,42 @@ class lt {
510
495
  });
511
496
  }
512
497
  }
513
- const y = (i) => Object.freeze(i);
514
- y([]);
515
- y([]);
516
- y([]);
517
- y([]);
518
- const l = y([]), D = new lt();
519
- function at(i) {
498
+ const N = (i) => Object.freeze(i);
499
+ N([]);
500
+ N([]);
501
+ N([]);
502
+ N([]);
503
+ const a = N([]), D = new at();
504
+ function ft(i) {
520
505
  return i !== null && typeof i == "object" && "value" in i && typeof i.subscribe == "function";
521
506
  }
522
- function St(i) {
523
- return at(i) && typeof i.invalidate == "function";
524
- }
525
507
  function gt(i) {
508
+ return ft(i) && typeof i.invalidate == "function";
509
+ }
510
+ function Ct(i) {
526
511
  return i !== null && typeof i == "object" && typeof i.dispose == "function" && typeof i.run == "function";
527
512
  }
528
513
  function K(i) {
529
514
  return i !== null && typeof i == "object" && typeof i.then == "function";
530
515
  }
531
- const W = o.RESOLVED | o.PENDING | o.REJECTED, U = Array(W + 1).fill(m.IDLE);
532
- U[o.RESOLVED] = m.RESOLVED;
533
- U[o.PENDING] = m.PENDING;
534
- U[o.REJECTED] = m.REJECTED;
535
- const ft = 3, Y = Number.MAX_SAFE_INTEGER - 1, Et = o.REJECTED | o.HAS_ERROR;
516
+ const W = o.RESOLVED | o.PENDING | o.REJECTED, m = Array(W + 1).fill(k.IDLE);
517
+ m[o.RESOLVED] = k.RESOLVED;
518
+ m[o.PENDING] = k.PENDING;
519
+ m[o.REJECTED] = k.REJECTED;
520
+ const Et = 3, Y = Number.MAX_SAFE_INTEGER - 1, dt = o.REJECTED | o.HAS_ERROR;
536
521
  class Z extends $ {
537
522
  constructor(t, e = {}) {
538
523
  if (typeof t != "function") throw new p(u.COMPUTED_MUST_BE_FUNCTION);
539
- super(), this._error = null, this._promiseId = 0, this._subscribers = [], this._links = l, this._cachedErrors = null, this._errorCacheEpoch = -1, this._asyncStartAggregateVersion = 0, this._asyncRetryCount = 0, this._errorDepCount = 0, this._trackEpoch = -1, this._trackLinks = l, this._trackCount = 0, this._value = void 0, this.flags = o.DIRTY | o.IDLE, this._equal = e.equal ?? Object.is, this._fn = t, this._defaultValue = "defaultValue" in e ? e.defaultValue : x, this._onError = e.onError ?? null;
524
+ super(), this._error = null, this._promiseId = 0, this._subscribers = [], this._links = a, this._cachedErrors = null, this._errorCacheEpoch = -1, this._asyncStartAggregateVersion = 0, this._asyncRetryCount = 0, this._errorDepCount = 0, this._trackEpoch = -1, this._trackLinks = a, this._trackCount = 0, this._value = void 0, this.flags = o.IS_COMPUTED | o.DIRTY | o.IDLE, this._equal = e.equal ?? Object.is, this._fn = t, this._defaultValue = "defaultValue" in e ? e.defaultValue : x, this._onError = e.onError ?? null;
540
525
  const s = e.maxAsyncRetries;
541
- if (this._maxAsyncRetries = s != null && s >= 0 ? s : ft, g.attachDebugInfo(this, "computed", this.id), e.lazy === !1)
526
+ if (this._maxAsyncRetries = s != null && s >= 0 ? s : Et, g.attachDebugInfo(this, "computed", this.id), e.lazy === !1)
542
527
  try {
543
528
  this._recompute();
544
529
  } catch {
545
530
  }
546
531
  }
547
532
  _track() {
548
- const t = E.current;
533
+ const t = f.current;
549
534
  t && j(this, t, this._subscribers);
550
535
  }
551
536
  get value() {
@@ -576,7 +561,7 @@ class Z extends $ {
576
561
  return this._value;
577
562
  }
578
563
  get state() {
579
- return this._track(), U[this.flags & W];
564
+ return this._track(), m[this.flags & W];
580
565
  }
581
566
  get hasError() {
582
567
  if (this._track(), this.flags & (o.REJECTED | o.HAS_ERROR) || this._errorDepCount > 0) return !0;
@@ -601,12 +586,12 @@ class Z extends $ {
601
586
  for (let r = 0, h = s.length; r < h; r++) {
602
587
  const c = s[r].node;
603
588
  if (c.flags & o.HAS_ERROR) {
604
- const B = c;
605
- if (B.errors) {
606
- const V = B.errors;
607
- for (let L = 0; L < V.length; L++) {
608
- const F = V[L];
609
- F && e.indexOf(F) === -1 && e.push(F);
589
+ const E = c;
590
+ if (E.errors) {
591
+ const V = E.errors;
592
+ for (let F = 0; F < V.length; F++) {
593
+ const U = V[F];
594
+ U && e.indexOf(U) === -1 && e.push(U);
610
595
  }
611
596
  }
612
597
  }
@@ -629,10 +614,10 @@ class Z extends $ {
629
614
  dispose() {
630
615
  if (this.flags & o.DISPOSED) return;
631
616
  const t = this._links;
632
- if (t !== l) {
617
+ if (t !== a) {
633
618
  for (let e = 0, s = t.length; e < s; e++)
634
619
  t[e].unsub?.();
635
- D.release(t), this._links = l;
620
+ D.release(t), this._links = a;
636
621
  }
637
622
  this._subscribers.length = 0, this.flags = o.DISPOSED | o.DIRTY | o.IDLE, this._error = null, this._value = void 0, this._promiseId = (this._promiseId + 1) % Y, this._cachedErrors = null, this._errorCacheEpoch = -1;
638
623
  }
@@ -642,7 +627,7 @@ class Z extends $ {
642
627
  const e = this._trackLinks[this._trackCount];
643
628
  e.node = t, e.version = t.version;
644
629
  } else
645
- this._trackLinks.push(new b(t, t.version));
630
+ this._trackLinks.push(new R(t, t.version));
646
631
  this._trackCount++;
647
632
  }
648
633
  }
@@ -665,7 +650,7 @@ class Z extends $ {
665
650
  this._trackEpoch = J(), this._trackLinks = D.acquire(), this._trackCount = 0;
666
651
  let e = !1;
667
652
  try {
668
- const s = E.run(this, this._fn);
653
+ const s = f.run(this, this._fn);
669
654
  this._commitDeps(t), e = !0, this._updateErrorDepCount(), K(s) ? this._handleAsyncComputation(s) : this._finalizeResolution(s);
670
655
  } catch (s) {
671
656
  if (!e)
@@ -676,7 +661,7 @@ class Z extends $ {
676
661
  }
677
662
  this._handleError(s, u.COMPUTED_COMPUTATION_FAILED, !0);
678
663
  } finally {
679
- e && t !== l ? D.release(t) : e || D.release(this._trackLinks), this._trackEpoch = -1, this._trackLinks = l, this._trackCount = 0, this.flags &= -257;
664
+ e && t !== a ? D.release(t) : e || D.release(this._trackLinks), this._trackEpoch = -1, this._trackLinks = a, this._trackCount = 0, this.flags &= -257;
680
665
  }
681
666
  }
682
667
  _handleAsyncComputation(t) {
@@ -702,12 +687,12 @@ class Z extends $ {
702
687
  let t = 0;
703
688
  const e = this._links;
704
689
  for (let s = 0, n = e.length; s < n; s++)
705
- t = ((t << 5) - t | 0) + e[s].node.version & O;
690
+ t = ((t << 5) - t | 0) + e[s].node.version & y;
706
691
  return t;
707
692
  }
708
693
  _handleError(t, e, s = !1) {
709
694
  const n = C(t, p, e);
710
- if (!s && !(this.flags & o.REJECTED) && (this.version = P(this.version)), this._error = n, this.flags = this.flags & -121 | Et, this._onError)
695
+ if (!s && !(this.flags & o.REJECTED) && (this.version = v(this.version)), this._error = n, this.flags = this.flags & -121 | dt, this._onError)
711
696
  try {
712
697
  this._onError(n);
713
698
  } catch (r) {
@@ -717,7 +702,7 @@ class Z extends $ {
717
702
  this._notifySubscribers(void 0, void 0);
718
703
  }
719
704
  _finalizeResolution(t) {
720
- (!(this.flags & o.RESOLVED) || !this._equal(this._value, t)) && (this.version = P(this.version)), this._value = t, this._error = null, this.flags = (this.flags | o.RESOLVED) & -697, this._cachedErrors = null, this._errorCacheEpoch = -1;
705
+ (!(this.flags & o.RESOLVED) || !this._equal(this._value, t)) && (this.version = v(this.version)), this._value = t, this._error = null, this.flags = (this.flags | o.RESOLVED) & -697, this._cachedErrors = null, this._errorCacheEpoch = -1;
721
706
  }
722
707
  execute() {
723
708
  this._markDirty();
@@ -728,64 +713,67 @@ class Z extends $ {
728
713
  }
729
714
  }
730
715
  Object.freeze(Z.prototype);
731
- function Ct(i, t = {}) {
716
+ function Tt(i, t = {}) {
732
717
  return new Z(i, t);
733
718
  }
734
- class dt extends q {
719
+ class pt extends q {
735
720
  constructor(t, e = {}) {
736
- super(), this._cleanup = null, this._links = l, this._nextLinks = null, this._currentEpoch = -1, this._lastFlushEpoch = -1, this._executionsInEpoch = 0, this._executionCount = 0, this._historyPtr = 0, this._execId = 0, this._fn = t, this._onError = e.onError ?? null, this._sync = e.sync ?? !1, this._maxExecutions = e.maxExecutionsPerSecond ?? I.MAX_EXECUTIONS_PER_SECOND, this._maxExecutionsPerFlush = e.maxExecutionsPerFlush ?? I.MAX_EXECUTIONS_PER_EFFECT, this._trackModifications = e.trackModifications ?? !1;
721
+ super(), this._cleanup = null, this._links = a, this._nextLinks = null, this._parkedUnsubs = null, this._currentEpoch = -1, this._lastFlushEpoch = -1, this._executionsInEpoch = 0, this._executionCount = 0, this._historyPtr = 0, this._execId = 0, this._fn = t, this._onError = e.onError ?? null, this._sync = e.sync ?? !1, this._maxExecutions = e.maxExecutionsPerSecond ?? I.MAX_EXECUTIONS_PER_SECOND, this._maxExecutionsPerFlush = e.maxExecutionsPerFlush ?? I.MAX_EXECUTIONS_PER_EFFECT, this._trackModifications = e.trackModifications ?? !1;
737
722
  const s = Number.isFinite(this._maxExecutions), n = s ? Math.min(this._maxExecutions + 1, I.MAX_EXECUTIONS_PER_SECOND + 1) : 0;
738
723
  this._historyCapacity = n, this._history = _ && s && n > 0 ? new Array(n).fill(0) : null, g.attachDebugInfo(this, "effect", this.id);
739
724
  }
740
725
  run() {
741
- if (this.flags & a.DISPOSED)
726
+ if (this.flags & l.DISPOSED)
742
727
  throw new S(u.EFFECT_DISPOSED);
743
728
  this.execute(!0);
744
729
  }
745
730
  dispose() {
746
- this.flags & a.DISPOSED || (this.flags |= a.DISPOSED, this._execCleanup(), this._releaseLinks(this._links), this._links = l, this._executeTask = void 0);
731
+ this.flags & l.DISPOSED || (this.flags |= l.DISPOSED, this._execCleanup(), this._releaseLinks(this._links), this._links = a, this._executeTask = void 0);
747
732
  }
748
733
  addDependency(t) {
749
- if (!(this.flags & a.EXECUTING) || t._lastSeenEpoch === this._currentEpoch) return;
734
+ if (!(this.flags & l.EXECUTING) || t._lastSeenEpoch === this._currentEpoch) return;
750
735
  t._lastSeenEpoch = this._currentEpoch;
751
- const e = this._nextLinks;
752
- if (t._tempUnsub) {
753
- e.push(new b(t, t.version, t._tempUnsub)), t._tempUnsub = void 0;
736
+ const e = this._nextLinks, s = this._parkedUnsubs?.get(t);
737
+ if (s) {
738
+ e.push(new R(t, t.version, s)), this._parkedUnsubs.delete(t);
754
739
  return;
755
740
  }
756
741
  try {
757
- const s = t.subscribe(() => {
758
- if (this._trackModifications && this.flags & a.EXECUTING && (t._modifiedAtEpoch = this._currentEpoch), this._sync) return this.execute();
759
- this._executeTask || (this._executeTask = () => this.execute()), f.schedule(this._executeTask);
742
+ const n = t.subscribe(() => {
743
+ if (this._trackModifications && this.flags & l.EXECUTING && (t._modifiedAtEpoch = this._currentEpoch), this._sync) return this.execute();
744
+ this._executeTask || (this._executeTask = () => this.execute()), O.schedule(this._executeTask);
760
745
  });
761
- e.push(new b(t, t.version, s));
762
- } catch (s) {
763
- const n = C(s, S, u.EFFECT_EXECUTION_FAILED);
764
- if (console.error(n), this._onError)
746
+ e.push(new R(t, t.version, n));
747
+ } catch (n) {
748
+ const r = C(n, S, u.EFFECT_EXECUTION_FAILED);
749
+ if (console.error(r), this._onError)
765
750
  try {
766
- this._onError(n);
751
+ this._onError(r);
767
752
  } catch {
768
753
  }
769
- e.push(new b(t, t.version, void 0));
754
+ e.push(new R(t, t.version, void 0));
770
755
  }
771
756
  }
772
757
  /**
773
758
  * Executes effect with tracking.
774
759
  */
775
760
  execute(t = !1) {
776
- if (this.flags & (a.DISPOSED | a.EXECUTING) || !t && this._links.length > 0 && !this._isDirty()) return;
777
- this._checkInfiniteLoops(), this.flags |= a.EXECUTING, this._execCleanup();
761
+ if (this.flags & (l.DISPOSED | l.EXECUTING) || !t && this._links.length > 0 && !this._isDirty()) return;
762
+ this._checkInfiniteLoops(), this.flags |= l.EXECUTING, this._execCleanup();
778
763
  const e = this._links;
779
- if (e !== l)
780
- for (let r = 0, h = e.length; r < h; r++) {
781
- const c = e[r];
782
- c && (c.node._tempUnsub = c.unsub);
764
+ if (e !== a) {
765
+ const r = /* @__PURE__ */ new Map();
766
+ for (let h = 0, c = e.length; h < c; h++) {
767
+ const E = e[h];
768
+ E?.unsub && r.set(E.node, E.unsub);
783
769
  }
770
+ this._parkedUnsubs = r;
771
+ }
784
772
  const s = D.acquire();
785
773
  this._nextLinks = s, this._currentEpoch = J();
786
774
  let n = !1;
787
775
  try {
788
- const r = E.run(this, this._fn);
776
+ const r = f.run(this, this._fn);
789
777
  this._links = s, n = !0, this._checkLoopWarnings(), K(r) ? this._handleAsyncResult(r) : this._cleanup = typeof r == "function" ? r : null;
790
778
  } catch (r) {
791
779
  n = !0, this._handleExecutionError(r), this._cleanup = null;
@@ -797,7 +785,7 @@ class dt extends q {
797
785
  const e = ++this._execId;
798
786
  t.then(
799
787
  (s) => {
800
- if (e !== this._execId || this.flags & a.DISPOSED) {
788
+ if (e !== this._execId || this.flags & l.DISPOSED) {
801
789
  if (typeof s == "function")
802
790
  try {
803
791
  s();
@@ -813,31 +801,28 @@ class dt extends q {
813
801
  }
814
802
  _finalizeDependencies(t, e, s) {
815
803
  if (this._nextLinks = null, t) {
816
- if (e !== l) {
817
- for (let n = 0, r = e.length; n < r; n++) {
818
- const h = e[n], c = h?.node._tempUnsub;
819
- c && (c(), h && (h.node._tempUnsub = void 0));
820
- }
821
- D.release(e);
822
- }
823
- } else if (this._releaseLinks(s), D.release(s), e !== l)
824
- for (let n = 0, r = e.length; n < r; n++)
825
- e[n] && (e[n].node._tempUnsub = void 0);
804
+ if (this._parkedUnsubs)
805
+ for (const n of this._parkedUnsubs.values())
806
+ n();
807
+ e !== a && D.release(e);
808
+ } else
809
+ this._releaseLinks(s), D.release(s);
810
+ this._parkedUnsubs = null;
826
811
  }
827
812
  _releaseLinks(t) {
828
- if (t !== l) {
813
+ if (t !== a) {
829
814
  for (let e = 0, s = t.length; e < s; e++)
830
815
  t[e]?.unsub?.();
831
816
  D.release(t);
832
817
  }
833
818
  }
834
819
  _isDirty() {
835
- const t = this._links, e = E.current;
836
- E.current = null;
820
+ const t = this._links, e = f.current;
821
+ f.current = null;
837
822
  try {
838
823
  for (let s = 0, n = t.length; s < n; s++) {
839
824
  const r = t[s], h = r.node;
840
- if ("value" in h)
825
+ if (h.flags & o.IS_COMPUTED)
841
826
  try {
842
827
  h.value;
843
828
  } catch {
@@ -847,7 +832,7 @@ class dt extends q {
847
832
  }
848
833
  return !1;
849
834
  } finally {
850
- E.current = e;
835
+ f.current = e;
851
836
  }
852
837
  }
853
838
  _execCleanup() {
@@ -861,7 +846,7 @@ class dt extends q {
861
846
  }
862
847
  }
863
848
  _checkInfiniteLoops() {
864
- const t = v;
849
+ const t = M;
865
850
  if (this._lastFlushEpoch !== t && (this._lastFlushEpoch = t, this._executionsInEpoch = 0), ++this._executionsInEpoch > this._maxExecutionsPerFlush && this._throwInfiniteLoopError("per-effect"), ut() > I.MAX_EXECUTIONS_PER_FLUSH && this._throwInfiniteLoopError("global"), this._executionCount++, this._history) {
866
851
  const e = Date.now();
867
852
  this._history[this._historyPtr] = e, this._historyPtr = (this._historyPtr + 1) % this._historyCapacity;
@@ -873,17 +858,17 @@ class dt extends q {
873
858
  }
874
859
  }
875
860
  get isDisposed() {
876
- return (this.flags & a.DISPOSED) !== 0;
861
+ return (this.flags & l.DISPOSED) !== 0;
877
862
  }
878
863
  get executionCount() {
879
864
  return this._executionCount;
880
865
  }
881
866
  get isExecuting() {
882
- return (this.flags & a.EXECUTING) !== 0;
867
+ return (this.flags & l.EXECUTING) !== 0;
883
868
  }
884
869
  _throwInfiniteLoopError(t) {
885
870
  const e = new S(
886
- `Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${w}`
871
+ `Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${B}`
887
872
  );
888
873
  throw this.dispose(), console.error(e), e;
889
874
  }
@@ -909,40 +894,40 @@ class dt extends q {
909
894
  }
910
895
  }
911
896
  }
912
- function Tt(i, t = {}) {
897
+ function Ot(i, t = {}) {
913
898
  if (typeof i != "function")
914
899
  throw new S(u.EFFECT_MUST_BE_FUNCTION);
915
- const e = new dt(i, t);
900
+ const e = new pt(i, t);
916
901
  return e.execute(), e;
917
902
  }
918
- function Ot(i) {
903
+ function yt(i) {
919
904
  if (typeof i != "function")
920
905
  throw new TypeError("Batch callback must be a function");
921
- f.startBatch();
906
+ O.startBatch();
922
907
  try {
923
908
  return i();
924
909
  } finally {
925
- f.endBatch();
910
+ O.endBatch();
926
911
  }
927
912
  }
928
913
  export {
929
- m as AsyncState,
914
+ k as AsyncState,
930
915
  T as AtomError,
931
916
  p as ComputedError,
932
917
  et as DEBUG_CONFIG,
933
918
  g as DEBUG_RUNTIME,
934
919
  S as EffectError,
935
- pt as POOL_CONFIG,
920
+ It as POOL_CONFIG,
936
921
  I as SCHEDULER_CONFIG,
937
- N as SchedulerError,
938
- Dt as atom,
939
- Ot as batch,
940
- Ct as computed,
941
- Tt as effect,
942
- at as isAtom,
943
- St as isComputed,
944
- gt as isEffect,
945
- f as scheduler,
946
- It as untracked
922
+ b as SchedulerError,
923
+ St as atom,
924
+ yt as batch,
925
+ Tt as computed,
926
+ Ot as effect,
927
+ ft as isAtom,
928
+ gt as isComputed,
929
+ Ct as isEffect,
930
+ O as scheduler,
931
+ Dt as untracked
947
932
  };
948
933
  //# sourceMappingURL=index.mjs.map