@but212/atom-effect 0.30.1 → 0.31.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/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- var u = {
1
+ var h = {
2
2
  DISPOSED: 1,
3
3
  IS_COMPUTED: 2,
4
4
  DIRTY: 256,
@@ -12,107 +12,108 @@ var u = {
12
12
  ATOM_SYNC: 16777216,
13
13
  ATOM_NOTIFICATION_SCHEDULED: 33554432,
14
14
  EFFECT_EXECUTING: 268435456
15
- }, At = Object.freeze({
16
- ASYNC_STATE: u.IDLE | u.PENDING | u.RESOLVED | u.REJECTED,
17
- COMPUTED_DIRTY_MASK: u.DIRTY | u.RECOMPUTING | u.FORCE_COMPUTE
18
- }), U = Object.freeze({
15
+ }, yt = Object.freeze({
16
+ ASYNC_STATE: h.IDLE | h.PENDING | h.RESOLVED | h.REJECTED,
17
+ COMPUTED_DIRTY_MASK: h.DIRTY | h.RECOMPUTING | h.FORCE_COMPUTE
18
+ }), P = Object.freeze({
19
19
  IDLE: "idle",
20
20
  PENDING: "pending",
21
21
  RESOLVED: "resolved",
22
22
  REJECTED: "rejected"
23
23
  }), S = Object.freeze({
24
- DISPOSED: u.DISPOSED,
25
- EXECUTING: u.EFFECT_EXECUTING
26
- }), Q = Object.freeze({
27
- DISPOSED: u.DISPOSED,
28
- IS_COMPUTED: u.IS_COMPUTED,
29
- DIRTY: u.DIRTY,
30
- IDLE: u.IDLE,
31
- PENDING: u.PENDING,
32
- RESOLVED: u.RESOLVED,
33
- REJECTED: u.REJECTED,
34
- RECOMPUTING: u.RECOMPUTING,
35
- HAS_ERROR: u.HAS_ERROR,
36
- FORCE_COMPUTE: u.FORCE_COMPUTE
37
- }), f = Object.freeze({
38
- DISPOSED: u.DISPOSED,
39
- SYNC: u.ATOM_SYNC,
40
- NOTIFICATION_SCHEDULED: u.ATOM_NOTIFICATION_SCHEDULED
24
+ DISPOSED: h.DISPOSED,
25
+ EXECUTING: h.EFFECT_EXECUTING
26
+ }), $ = Object.freeze({
27
+ DISPOSED: h.DISPOSED,
28
+ IS_COMPUTED: h.IS_COMPUTED,
29
+ DIRTY: h.DIRTY,
30
+ IDLE: h.IDLE,
31
+ PENDING: h.PENDING,
32
+ RESOLVED: h.RESOLVED,
33
+ REJECTED: h.REJECTED,
34
+ RECOMPUTING: h.RECOMPUTING,
35
+ HAS_ERROR: h.HAS_ERROR,
36
+ FORCE_COMPUTE: h.FORCE_COMPUTE
41
37
  }), C = Object.freeze({
38
+ DISPOSED: h.DISPOSED,
39
+ SYNC: h.ATOM_SYNC,
40
+ NOTIFICATION_SCHEDULED: h.ATOM_NOTIFICATION_SCHEDULED
41
+ }), g = Object.freeze({
42
42
  MAX_EXECUTIONS_PER_SECOND: 1e3,
43
43
  MAX_EXECUTIONS_PER_EFFECT: 100,
44
44
  MAX_EXECUTIONS_PER_FLUSH: 1e4,
45
45
  MAX_FLUSH_ITERATIONS: 1e3,
46
46
  MIN_FLUSH_ITERATIONS: 10,
47
47
  BATCH_QUEUE_SHRINK_THRESHOLD: 1e3
48
- }), k = 1073741823, Y = Object.freeze({
48
+ }), H = 1073741823, W = Object.freeze({
49
49
  WARN_INFINITE_LOOP: !0,
50
50
  EFFECT_FREQUENCY_WINDOW: 1e3,
51
51
  LOOP_THRESHOLD: 100
52
- }), ft = Object.freeze({ MAX_PROMISE_ID: k }), w = Object.freeze({
52
+ }), Et = Object.freeze({ MAX_PROMISE_ID: H }), U = Object.freeze({
53
53
  UNINITIALIZED: -1,
54
54
  MIN: 1
55
- }), ot = !1;
55
+ }), ht = !1;
56
56
  try {
57
- ot = !!(typeof globalThis < "u" && globalThis.__ATOM_DEBUG__ || typeof sessionStorage < "u" && sessionStorage.getItem("__ATOM_DEBUG__") === "true");
57
+ ht = !!(typeof globalThis < "u" && globalThis.__ATOM_DEBUG__ || typeof sessionStorage < "u" && sessionStorage.getItem("__ATOM_DEBUG__") === "true");
58
58
  } catch {
59
59
  }
60
- var _ = (typeof process < "u" && process.env, typeof __DEV__ < "u" && !!__DEV__ || ot), et = Object.freeze([]), g = class V extends Error {
60
+ var f = (typeof process < "u" && process.env, typeof __DEV__ < "u" && !!__DEV__ || ht), it = Object.freeze([]), y = class j extends Error {
61
61
  constructor(e, s = null, i = !0, n) {
62
62
  super(e), this.cause = s, this.recoverable = i, this.code = n, this.name = "AtomError", Error.captureStackTrace && Error.captureStackTrace(this, this.constructor);
63
63
  }
64
64
  getChain() {
65
- if (this.cause === null || this.cause === void 0) return [this];
66
- const e = [this], s = /* @__PURE__ */ new Set([this]);
67
- let i = this.cause;
68
- for (; i != null; ) {
69
- const n = s.has(i);
70
- if (e.push(i), n) break;
71
- if (s.add(i), i instanceof V) i = i.cause;
72
- else if (i instanceof Error && "cause" in i) i = i.cause;
65
+ const e = this.cause;
66
+ if (e == null) return [this];
67
+ const s = [this];
68
+ let i = e, n = null;
69
+ for (; i != null && (s.push(i), !(i === this || n?.has(i))); ) {
70
+ if (i instanceof j) i = i.cause;
71
+ else if (i instanceof Error) i = i.cause;
73
72
  else break;
73
+ s.length > 3 && (n === null ? n = new Set(s) : n.add(i));
74
74
  }
75
- return e;
75
+ return s;
76
76
  }
77
- toJSON(e = /* @__PURE__ */ new Set()) {
78
- if (e.has(this)) return {
77
+ toJSON(e) {
78
+ const s = e ?? /* @__PURE__ */ new Set();
79
+ if (s.has(this)) return {
79
80
  name: this.name,
80
81
  message: "[Circular Reference]",
81
82
  recoverable: this.recoverable,
82
83
  code: this.code
83
84
  };
84
- e.add(this);
85
- let s = this.cause;
86
- return this.cause instanceof V ? s = this.cause.toJSON(e) : this.cause instanceof Error && (s = {
87
- name: this.cause.name,
88
- message: this.cause.message,
89
- stack: this.cause.stack,
90
- cause: this.cause.cause
85
+ s.add(this);
86
+ let i = this.cause;
87
+ return i instanceof j ? i = i.toJSON(s) : i instanceof Error && (i = {
88
+ name: i.name,
89
+ message: i.message,
90
+ stack: i.stack,
91
+ cause: i.cause
91
92
  }), {
92
93
  name: this.name,
93
94
  message: this.message,
94
95
  code: this.code,
95
96
  recoverable: this.recoverable,
96
97
  stack: this.stack,
97
- cause: s
98
+ cause: i
98
99
  };
99
100
  }
100
101
  static format(e, s, i) {
101
102
  return `${e} (${s}): ${i}`;
102
103
  }
103
- }, A = class extends g {
104
+ }, w = class extends y {
104
105
  constructor(...t) {
105
106
  super(...t), this.name = "ComputedError";
106
107
  }
107
- }, m = class extends g {
108
+ }, N = class extends y {
108
109
  constructor(t, e = null, s = !1, i) {
109
110
  super(t, e, s, i), this.name = "EffectError";
110
111
  }
111
- }, L = class extends g {
112
+ }, x = class extends y {
112
113
  constructor(t, e = null, s = !1, i) {
113
114
  super(t, e, s, i), this.name = "SchedulerError";
114
115
  }
115
- }, h = {
116
+ }, l = {
116
117
  COMPUTED_MUST_BE_FUNCTION: "Computed target must be a function",
117
118
  COMPUTED_ASYNC_PENDING_NO_DEFAULT: "Async computation pending with no default value",
118
119
  COMPUTED_COMPUTATION_FAILED: "Computation execution failed",
@@ -132,39 +133,24 @@ var _ = (typeof process < "u" && process.env, typeof __DEV__ < "u" && !!__DEV__
132
133
  SCHEDULER_END_BATCH_WITHOUT_START: "endBatch() called without matching startBatch(). Ignoring.",
133
134
  BATCH_CALLBACK_MUST_BE_FUNCTION: "Batch callback must be a function"
134
135
  };
135
- function N(t, e, s) {
136
- if (t instanceof g) return new e(g.format(t.name, s, t.message), t, t.recoverable, t.code);
137
- if (t instanceof Error) {
138
- const i = t.name || t.constructor.name || "Error";
139
- return new e(g.format(i, s, t.message), t);
140
- }
141
- return new e(g.format("Unexpected error", s, String(t)), t);
136
+ function A(t, e, s) {
137
+ return t instanceof y ? new e(`${t.name} (${s}): ${t.message}`, t, t.recoverable, t.code) : t instanceof Error ? new e(`${t.name || "Error"} (${s}): ${t.message}`, t) : new e(`Unexpected error (${s}): ${String(t)}`, t);
142
138
  }
143
- var st = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), Et = /* @__PURE__ */ Symbol("AtomEffect.Id"), it = /* @__PURE__ */ Symbol("AtomEffect.Type"), B = /* @__PURE__ */ Symbol("AtomEffect.NoDefaultValue"), dt = "[Atom Effect]", pt = class {
139
+ var nt = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), dt = /* @__PURE__ */ Symbol("AtomEffect.Id"), rt = /* @__PURE__ */ Symbol("AtomEffect.Type"), G = /* @__PURE__ */ Symbol("AtomEffect.NoDefaultValue"), pt = "[Atom Effect]", M = () => {
140
+ }, It = class {
144
141
  constructor() {
145
- this.enabled = !0, this.warnInfiniteLoop = Y.WARN_INFINITE_LOOP, this._updateCounts = /* @__PURE__ */ new Map(), this._nodeRegistry = /* @__PURE__ */ new Map(), this._threshold = Y.LOOP_THRESHOLD, this._cleanupScheduled = !1, this.warn = (t, e) => {
146
- this.enabled && t && console.warn(`${dt} ${e}`);
142
+ this.enabled = !0, this.warnInfiniteLoop = W.WARN_INFINITE_LOOP, this._updateCounts = /* @__PURE__ */ new Map(), this._nodeRegistry = /* @__PURE__ */ new Map(), this._threshold = W.LOOP_THRESHOLD, this._cleanupScheduled = !1, this.warn = (t, e) => {
143
+ this.enabled && t && console.warn(`${pt} ${e}`);
147
144
  }, this.registerNode = (t) => {
148
145
  this._nodeRegistry.set(t.id, new WeakRef(t));
149
146
  }, this.attachDebugInfo = (t, e, s, i) => {
150
- this.enabled && (Object.defineProperties(t, {
151
- [st]: {
152
- value: i ?? `${e}_${s}`,
153
- configurable: !0
154
- },
155
- [Et]: {
156
- value: s,
157
- configurable: !0
158
- },
159
- [it]: {
160
- value: e,
161
- configurable: !0
162
- }
163
- }), this.registerNode(t));
147
+ if (!this.enabled) return;
148
+ const n = t;
149
+ n[nt] = i ?? `${e}_${s}`, n[dt] = s, n[rt] = e, this.registerNode(t);
164
150
  }, this.trackUpdate = (t, e) => {
165
151
  if (!this.enabled || !this.warnInfiniteLoop) return;
166
152
  const s = this._updateCounts, i = (s.get(t) ?? 0) + 1;
167
- i > this._threshold ? this.warn(!0, `Infinite loop detected for ${e ?? `dependency ${t}`}. Over ${this._threshold} updates in a single execution scope.`) : s.set(t, i), this._cleanupScheduled || (this._cleanupScheduled = !0, Promise.resolve().then(() => {
153
+ i > this._threshold ? this.warn(!0, `Infinite loop detected for ${e ?? `dependency ${t}`}. Over ${this._threshold} updates in a single execution scope.`) : s.set(t, i), this._cleanupScheduled || (this._cleanupScheduled = !0, queueMicrotask(() => {
168
154
  this._updateCounts.clear(), this._cleanupScheduled = !1;
169
155
  }));
170
156
  }, this.dumpGraph = () => {
@@ -180,39 +166,35 @@ var st = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), Et = /* @__PURE__ */ Sy
180
166
  }
181
167
  return t;
182
168
  }, this.getDebugName = (t) => {
183
- if (t)
184
- return t[st];
169
+ if (t != null)
170
+ return t[nt];
185
171
  }, this.getDebugType = (t) => {
186
- if (t)
187
- return t[it];
172
+ if (t != null)
173
+ return t[rt];
188
174
  };
189
175
  }
190
- }, It = {
176
+ }, Ct = {
191
177
  enabled: !1,
192
178
  warnInfiniteLoop: !1,
193
- warn: () => {
194
- },
195
- registerNode: () => {
196
- },
197
- attachDebugInfo: () => {
198
- },
199
- trackUpdate: () => {
200
- },
179
+ warn: M,
180
+ registerNode: M,
181
+ attachDebugInfo: M,
182
+ trackUpdate: M,
201
183
  dumpGraph: () => [],
202
184
  getDebugName: () => {
203
185
  },
204
186
  getDebugType: () => {
205
187
  }
206
- }, d = _ ? new pt() : It, Ct = 1, gt = () => Ct++ | 0, ut = class {
188
+ }, d = f ? new It() : Ct, Dt = 1, gt = () => Dt++ | 0, lt = class {
207
189
  constructor() {
208
- this._s0 = null, this._s1 = null, this._s2 = null, this._s3 = null, this._count = 0, this._actualCount = 0, this._overflow = null, this._freeIndices = null;
190
+ this._count = 0, this._actualCount = 0, this._s0 = null, this._s1 = null, this._s2 = null, this._s3 = null, this._overflow = null, this._freeIndices = null;
209
191
  }
210
192
  _rawWrite(t, e) {
211
193
  if (t < 4) t === 0 ? this._s0 = e : t === 1 ? this._s1 = e : t === 2 ? this._s2 = e : this._s3 = e;
212
194
  else {
213
195
  this._overflow === null && (this._overflow = []);
214
- const s = this._overflow, i = t - 4;
215
- s[i] = e;
196
+ const s = this._overflow;
197
+ s[t - 4] = e;
216
198
  }
217
199
  }
218
200
  _rawAdd(t) {
@@ -230,7 +212,7 @@ var st = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), Et = /* @__PURE__ */ Sy
230
212
  const i = s.pop();
231
213
  return e[i] = t, i + 4;
232
214
  }
233
- return e.push(t), 4 + e.length - 1;
215
+ return e.push(t), 3 + e.length;
234
216
  }
235
217
  _rawSwap(t, e) {
236
218
  if (t === e) return;
@@ -244,18 +226,26 @@ var st = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), Et = /* @__PURE__ */ Sy
244
226
  return this._count;
245
227
  }
246
228
  getAt(t) {
247
- return t < 4 ? t === 0 ? this._s0 : t === 1 ? this._s1 : t === 2 ? this._s2 : this._s3 : this._overflow?.[t - 4] ?? null;
229
+ if (t < 4)
230
+ return t === 0 ? this._s0 : t === 1 ? this._s1 : t === 2 ? this._s2 : t === 3 ? this._s3 : null;
231
+ const e = this._overflow;
232
+ return e === null ? null : e[t - 4] ?? null;
248
233
  }
249
234
  setAt(t, e) {
250
235
  const s = this.getAt(t);
251
- s !== e && (this._rawWrite(t, e), s === null ? this._actualCount++ : e === null && this._actualCount--, e !== null && t >= this._count ? this._count = t + 1 : e === null && this._shrinkPhysicalSizeFrom(t));
236
+ s !== e && (this._rawWrite(t, e), s === null ? this._actualCount++ : e === null && this._actualCount--, e !== null ? t >= this._count && (this._count = t + 1) : this._shrinkPhysicalSizeFrom(t));
252
237
  }
253
238
  _shrinkPhysicalSizeFrom(t) {
254
- if (t === this._count - 1)
255
- for (this._count--; this._count > 0 && this.getAt(this._count - 1) == null; ) this._count--;
239
+ if (t === this._count - 1) {
240
+ if (this._count--, this._count > 4) {
241
+ const e = this._overflow;
242
+ for (; this._count > 4 && e[this._count - 5] === null; ) this._count--;
243
+ }
244
+ this._count === 4 && this._s3 === null && (this._count = 3, this._s2 === null && (this._count = 2, this._s1 === null && (this._count = 1, this._s0 === null && (this._count = 0))));
245
+ }
256
246
  }
257
247
  truncateFrom(t) {
258
- t <= 3 && (t <= 0 && this._s0 !== null && (this._onItemRemoved(this._s0), this._s0 = null, this._actualCount--), t <= 1 && this._s1 !== null && (this._onItemRemoved(this._s1), this._s1 = null, this._actualCount--), t <= 2 && this._s2 !== null && (this._onItemRemoved(this._s2), this._s2 = null, this._actualCount--), t <= 3 && this._s3 !== null && (this._onItemRemoved(this._s3), this._s3 = null, this._actualCount--));
248
+ t <= 3 && (t <= 3 && this._s3 !== null && (this._onItemRemoved(this._s3), this._s3 = null, this._actualCount--), t <= 2 && this._s2 !== null && (this._onItemRemoved(this._s2), this._s2 = null, this._actualCount--), t <= 1 && this._s1 !== null && (this._onItemRemoved(this._s1), this._s1 = null, this._actualCount--), t <= 0 && this._s0 !== null && (this._onItemRemoved(this._s0), this._s0 = null, this._actualCount--));
259
249
  const e = this._overflow;
260
250
  if (e !== null) {
261
251
  const s = t > 4 ? t - 4 : 0, i = e.length;
@@ -295,31 +285,37 @@ var st = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), Et = /* @__PURE__ */ Sy
295
285
  const e = this._actualCount;
296
286
  if (e === 0) return;
297
287
  if (e === this._count) {
298
- this._s0 != null && t(this._s0), this._s1 != null && t(this._s1), this._s2 != null && t(this._s2), this._s3 != null && t(this._s3);
299
- const n = this._overflow;
300
- if (n !== null) for (let r = 0, o = n.length; r < o; r++) {
301
- const c = n[r];
302
- c != null && t(c);
288
+ if (t(this._s0), e > 1 && (t(this._s1), e > 2 && (t(this._s2), e > 3 && (t(this._s3), e > 4)))) {
289
+ const n = this._overflow;
290
+ for (let r = 0, o = n.length; r < o; r++) t(n[r]);
303
291
  }
304
292
  return;
305
293
  }
306
294
  let s = 0;
307
- const i = this._count;
308
- for (let n = 0; n < i; n++) {
309
- const r = this.getAt(n);
310
- if (r != null && (t(r), ++s >= e))
311
- break;
295
+ if (this._s0 !== null && (t(this._s0), ++s >= e) || this._s1 !== null && (t(this._s1), ++s >= e) || this._s2 !== null && (t(this._s2), ++s >= e) || this._s3 !== null && (t(this._s3), ++s >= e))
296
+ return;
297
+ const i = this._overflow;
298
+ if (i !== null) for (let n = 0, r = i.length; n < r; n++) {
299
+ const o = i[n];
300
+ if (o != null && (t(o), ++s >= e))
301
+ return;
312
302
  }
313
303
  }
314
304
  compact() {
315
- if (this._actualCount === this._count) return;
316
- let t = 0;
317
- const e = this._count;
318
- for (let s = 0; s < e; s++) {
319
- const i = this.getAt(s);
320
- i != null && (s !== t && (this._rawWrite(t, i), this._rawWrite(s, null)), t++);
305
+ const t = this._actualCount;
306
+ if (t === this._count) return;
307
+ if (t === 0) {
308
+ this.clear();
309
+ return;
310
+ }
311
+ let e = 0;
312
+ const s = this._count;
313
+ for (let i = 0; i < s; i++) {
314
+ const n = this.getAt(i);
315
+ if (n !== null && (i !== e && (this._rawWrite(e, n), this._rawWrite(i, null)), e++, e === t))
316
+ break;
321
317
  }
322
- this._count = this._actualCount, this._overflow !== null && (t <= 4 ? this._overflow = null : this._overflow.length = t - 4), this._freeIndices = null;
318
+ this._count = t, this._overflow !== null && (e <= 4 ? this._overflow = null : this._overflow.length = e - 4), this._freeIndices = null;
323
319
  }
324
320
  clear() {
325
321
  this._s0 = this._s1 = this._s2 = this._s3 = null, this._count = 0, this._actualCount = 0, this._overflow = null, this._freeIndices = null;
@@ -327,7 +323,7 @@ var st = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), Et = /* @__PURE__ */ Sy
327
323
  dispose() {
328
324
  this.clear();
329
325
  }
330
- }, ht = class extends ut {
326
+ }, ct = class extends lt {
331
327
  constructor(...t) {
332
328
  super(...t), this._map = null, this._SCAN_THRESHOLD = 32, this.hasComputeds = !1;
333
329
  }
@@ -339,28 +335,41 @@ var st = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), Et = /* @__PURE__ */ Sy
339
335
  }
340
336
  setAt(t, e) {
341
337
  const s = this.getAt(t);
342
- super.setAt(t, e), this._map !== null && (s?.unsub && this._map.delete(s.node), e?.unsub && this._map.set(e.node, t));
338
+ super.setAt(t, e), this._map !== null && (s !== null && this._map.delete(s.node), e !== null && this._map.set(e.node, t));
343
339
  }
344
340
  claimExisting(t, e) {
345
341
  const s = this._count;
346
342
  if (s <= e) return !1;
347
- const i = this.getAt(e);
348
- if (i && i.node === t && i.unsub)
343
+ let i = null;
344
+ if (e < 4 ? e === 0 ? i = this._s0 : e === 1 ? i = this._s1 : e === 2 ? i = this._s2 : i = this._s3 : i = this._overflow[e - 4] ?? null, i && i.node === t && i.unsub)
349
345
  return i.version = t.version, !0;
350
346
  if (this._map !== null || s - e > this._SCAN_THRESHOLD) return this._claimViaMap(t, e);
351
- for (let n = e + 1; n < s; n++) {
352
- const r = this.getAt(n);
353
- if (r && r.node === t && r.unsub)
354
- return r.version = t.version, this._rawSwap(n, e), !0;
347
+ let n = -1, r = null, o = e + 1;
348
+ for (; o < 4 && o < s; o++) {
349
+ const u = o === 1 ? this._s1 : o === 2 ? this._s2 : this._s3;
350
+ if (u && u.node === t && u.unsub) {
351
+ n = o, r = u;
352
+ break;
353
+ }
355
354
  }
356
- return !1;
355
+ if (n === -1 && o < s) {
356
+ const u = this._overflow;
357
+ for (let c = o - 4, I = s - 4; c < I; c++) {
358
+ const E = u[c];
359
+ if (E && E.node === t && E.unsub) {
360
+ n = c + 4, r = E;
361
+ break;
362
+ }
363
+ }
364
+ }
365
+ return n !== -1 ? (r.version = t.version, this._rawWrite(e, r), this._rawWrite(n, i), !0) : !1;
357
366
  }
358
367
  _claimViaMap(t, e) {
359
368
  this._map === null && (this._map = this._initMap());
360
369
  const s = this._map, i = s.get(t);
361
370
  if (i === void 0 || i < e) return !1;
362
371
  const n = this.getAt(i);
363
- if (n == null || !n.unsub) return !1;
372
+ if (n === null || !n.unsub) return !1;
364
373
  if (n.version = t.version, i !== e) {
365
374
  const r = this.getAt(e);
366
375
  this._rawSwap(i, e), s.set(t, e), r?.unsub && s.set(r.node, i);
@@ -369,19 +378,27 @@ var st = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), Et = /* @__PURE__ */ Sy
369
378
  }
370
379
  _initMap() {
371
380
  const t = /* @__PURE__ */ new Map();
372
- for (let e = 0; e < this._count; e++) {
373
- const s = this.getAt(e);
374
- s?.unsub && t.set(s.node, e);
381
+ this._s0?.unsub && t.set(this._s0.node, 0), this._s1?.unsub && t.set(this._s1.node, 1), this._s2?.unsub && t.set(this._s2.node, 2), this._s3?.unsub && t.set(this._s3.node, 3);
382
+ const e = this._overflow;
383
+ if (e !== null) for (let s = 0, i = e.length; s < i; s++) {
384
+ const n = e[s];
385
+ n?.unsub && t.set(n.node, s + 4);
375
386
  }
376
387
  return t;
377
388
  }
378
389
  insertNew(t, e) {
379
- const s = this.getAt(t);
390
+ let s = null;
391
+ if (t < 4) t === 0 ? (s = this._s0, this._s0 = e) : t === 1 ? (s = this._s1, this._s1 = e) : t === 2 ? (s = this._s2, this._s2 = e) : (s = this._s3, this._s3 = e);
392
+ else {
393
+ this._overflow === null && (this._overflow = []);
394
+ const i = this._overflow;
395
+ s = i[t - 4] ?? null, i[t - 4] = e;
396
+ }
380
397
  if (s !== null) {
381
398
  const i = this._rawAdd(s);
382
399
  i >= this._count && (this._count = i + 1), this._map !== null && s.unsub && this._map.set(s.node, i);
383
400
  }
384
- this._rawWrite(t, e), t >= this._count && (this._count = t + 1), this._actualCount++, this._map !== null && e.unsub && this._map.set(e.node, t);
401
+ t >= this._count && (this._count = t + 1), this._actualCount++, this._map !== null && e.unsub && this._map.set(e.node, t);
385
402
  }
386
403
  add(t) {
387
404
  const e = super.add(t);
@@ -398,49 +415,51 @@ var st = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), Et = /* @__PURE__ */ Sy
398
415
  disposeAll() {
399
416
  this.truncateFrom(0), this.hasComputeds = !1;
400
417
  }
401
- }, F = /* @__PURE__ */ Symbol.for("atom-effect/brand"), p = {
418
+ }, L = /* @__PURE__ */ Symbol.for("atom-effect/brand"), p = {
402
419
  Atom: 1,
403
420
  Writable: 2,
404
421
  Computed: 4,
405
422
  Effect: 8
406
423
  };
407
- function $(t, e) {
408
- if (!t) return !1;
409
- const s = typeof t;
410
- return (s === "object" || s === "function") && !!((t[F] ?? 0) & e);
424
+ function J(t, e) {
425
+ return !t || typeof t != "object" && typeof t != "function" ? !1 : !!(t[L] & e);
411
426
  }
412
- function yt(t) {
413
- return $(t, p.Atom);
427
+ function wt(t) {
428
+ return J(t, p.Atom);
414
429
  }
415
430
  function Rt(t) {
416
- return $(t, p.Computed);
431
+ return J(t, p.Computed);
417
432
  }
418
- function wt(t) {
419
- return $(t, p.Effect);
433
+ function Ft(t) {
434
+ return J(t, p.Effect);
420
435
  }
421
- function q(t) {
422
- if (t instanceof Promise) return !0;
423
- if (!t) return !1;
424
- const e = typeof t;
425
- return (e === "object" || e === "function") && typeof t.then == "function";
436
+ function Z(t) {
437
+ return t instanceof Promise ? !0 : t === null || typeof t != "object" ? !1 : typeof t.then == "function";
426
438
  }
427
- var W = class {
439
+ var q = class {
428
440
  constructor(t, e, s = void 0) {
429
441
  this.node = t, this.version = e, this.unsub = s;
430
442
  }
431
- }, Dt = class {
443
+ }, St = class {
432
444
  constructor(t = void 0, e = void 0) {
433
445
  this.fn = t, this.sub = e;
434
446
  }
435
447
  notify(t, e) {
436
- M(() => {
437
- const s = this.fn;
438
- s !== void 0 && s(t, e);
439
- const i = this.sub;
440
- i !== void 0 && i.execute();
441
- });
448
+ const s = this.fn, i = this.sub;
449
+ if (s === void 0 && i === void 0) return;
450
+ const n = _, r = n.current;
451
+ if (r === null) {
452
+ s !== void 0 && s(t, e), i !== void 0 && i.execute();
453
+ return;
454
+ }
455
+ n.current = null;
456
+ try {
457
+ s !== void 0 && s(t, e), i !== void 0 && i.execute();
458
+ } finally {
459
+ n.current = r;
460
+ }
442
461
  }
443
- }, St = class {
462
+ }, vt = class {
444
463
  constructor() {
445
464
  this.current = null;
446
465
  }
@@ -449,15 +468,16 @@ var W = class {
449
468
  const s = this.current;
450
469
  this.current = t;
451
470
  try {
471
+ if (!f) return e();
452
472
  const i = e();
453
- return _ && d.warn(q(i), 'Detected Promise returned within tracking context. Dependencies accessed after "await" will NOT be tracked. Consider using synchronous tracking before the async boundary.'), i;
473
+ return d.warn(Z(i), 'Detected Promise returned within tracking context. Dependencies accessed after "await" will NOT be tracked. Consider using synchronous tracking before the async boundary.'), i;
454
474
  } finally {
455
475
  this.current = s;
456
476
  }
457
477
  }
458
- }, l = new St();
459
- function M(t) {
460
- const e = l, s = e.current;
478
+ }, _ = new vt();
479
+ function X(t) {
480
+ const e = _, s = e.current;
461
481
  if (s === null) return t();
462
482
  e.current = null;
463
483
  try {
@@ -466,48 +486,56 @@ function M(t) {
466
486
  e.current = s;
467
487
  }
468
488
  }
469
- var K = class {
489
+ var tt = class {
470
490
  constructor() {
471
- this.flags = 0, this.version = 0, this._lastSeenEpoch = w.UNINITIALIZED, this._nextEpoch = void 0, this._notifying = 0, this._hotIndex = -1, this._slots = null, this._deps = null, this.id = gt() & k;
491
+ this.flags = 0, this.version = 0, this._lastSeenEpoch = U.UNINITIALIZED, this._notifying = 0, this._hotIndex = -1, this.id = gt() & H, this._nextEpoch = void 0, this._slots = null, this._deps = null;
472
492
  }
473
493
  get isDisposed() {
474
- return (this.flags & Q.DISPOSED) !== 0;
494
+ return (this.flags & $.DISPOSED) !== 0;
475
495
  }
476
496
  get isComputed() {
477
- return (this.flags & Q.IS_COMPUTED) !== 0;
497
+ return (this.flags & $.IS_COMPUTED) !== 0;
478
498
  }
479
499
  get hasError() {
480
500
  return !1;
481
501
  }
482
502
  subscribe(t) {
483
503
  const e = typeof t == "function";
484
- if (!e && (!t || typeof t.execute != "function")) throw N(/* @__PURE__ */ new TypeError("Invalid subscriber"), g, h.ATOM_SUBSCRIBER_MUST_BE_FUNCTION);
504
+ if (!e && (t === null || typeof t.execute != "function")) throw A(/* @__PURE__ */ new TypeError("Invalid subscriber"), y, l.ATOM_SUBSCRIBER_MUST_BE_FUNCTION);
485
505
  let s = this._slots;
486
- s || (s = new ut(), this._slots = s);
487
- let i = !1;
488
- if (s._s0 != null && (e ? s._s0.fn === t : s._s0.sub === t)) i = !0;
489
- else if (s._s1 != null && (e ? s._s1.fn === t : s._s1.sub === t)) i = !0;
490
- else if (s._s2 != null && (e ? s._s2.fn === t : s._s2.sub === t)) i = !0;
491
- else if (s._s3 != null && (e ? s._s3.fn === t : s._s3.sub === t)) i = !0;
492
- else {
493
- const r = s._overflow;
494
- if (r != null) for (let o = 0, c = r.length; o < c; o++) {
495
- const a = r[o];
496
- if (a != null && (e ? a.fn === t : a.sub === t)) {
497
- i = !0;
498
- break;
506
+ if (s === null && (s = new lt(), this._slots = s), s.size > 0) {
507
+ let n = !1;
508
+ if (s._s0 !== null && (s._s0.fn === t || s._s0.sub === t) || s._s1 !== null && (s._s1.fn === t || s._s1.sub === t) || s._s2 !== null && (s._s2.fn === t || s._s2.sub === t) || s._s3 !== null && (s._s3.fn === t || s._s3.sub === t)) n = !0;
509
+ else {
510
+ const r = s._overflow;
511
+ if (r !== null) {
512
+ const o = r.length;
513
+ if (e) for (let u = 0; u < o; u++) {
514
+ const c = r[u];
515
+ if (c !== null && c?.fn === t) {
516
+ n = !0;
517
+ break;
518
+ }
519
+ }
520
+ else for (let u = 0; u < o; u++) {
521
+ const c = r[u];
522
+ if (c !== null && c?.sub === t) {
523
+ n = !0;
524
+ break;
525
+ }
526
+ }
499
527
  }
500
528
  }
529
+ if (n)
530
+ return f && console.warn(`[atom-effect] Duplicate subscription ignored on node ${this.id}`), () => {
531
+ };
501
532
  }
502
- if (i)
503
- return _ && console.warn(`[atom-effect] Duplicate subscription ignored on node ${this.id}`), () => {
504
- };
505
- const n = new Dt(e ? t : void 0, e ? void 0 : t);
506
- return s.add(n), () => this._unsubscribe(n);
533
+ const i = new St(e ? t : void 0, e ? void 0 : t);
534
+ return s.add(i), () => this._unsubscribe(i);
507
535
  }
508
536
  _unsubscribe(t) {
509
537
  const e = this._slots;
510
- e && (e.remove(t), this._notifying === 0 && e.compact());
538
+ e !== null && (e.remove(t), this._notifying === 0 && e.compact());
511
539
  }
512
540
  subscriberCount() {
513
541
  const t = this._slots;
@@ -518,34 +546,33 @@ var K = class {
518
546
  if (!(s === null || s.size === 0)) {
519
547
  this._notifying++;
520
548
  try {
521
- let i = s._s0;
522
- if (i != null) try {
523
- i.notify(t, e);
524
- } catch (r) {
525
- this._logNotifyError(r);
549
+ if (s._s0 !== null) try {
550
+ s._s0.notify(t, e);
551
+ } catch (n) {
552
+ this._logNotifyError(n);
526
553
  }
527
- if (i = s._s1, i != null) try {
528
- i.notify(t, e);
529
- } catch (r) {
530
- this._logNotifyError(r);
554
+ if (s._s1 !== null) try {
555
+ s._s1.notify(t, e);
556
+ } catch (n) {
557
+ this._logNotifyError(n);
531
558
  }
532
- if (i = s._s2, i != null) try {
533
- i.notify(t, e);
534
- } catch (r) {
535
- this._logNotifyError(r);
559
+ if (s._s2 !== null) try {
560
+ s._s2.notify(t, e);
561
+ } catch (n) {
562
+ this._logNotifyError(n);
536
563
  }
537
- if (i = s._s3, i != null) try {
538
- i.notify(t, e);
539
- } catch (r) {
540
- this._logNotifyError(r);
564
+ if (s._s3 !== null) try {
565
+ s._s3.notify(t, e);
566
+ } catch (n) {
567
+ this._logNotifyError(n);
541
568
  }
542
- const n = s._overflow;
543
- if (n != null) for (let r = 0, o = n.length; r < o; r++) {
544
- const c = n[r];
545
- if (c != null) try {
546
- c.notify(t, e);
547
- } catch (a) {
548
- this._logNotifyError(a);
569
+ const i = s._overflow;
570
+ if (i !== null) for (let n = 0, r = i.length; n < r; n++) {
571
+ const o = i[n];
572
+ if (o !== null) try {
573
+ o?.notify(t, e);
574
+ } catch (u) {
575
+ this._logNotifyError(u);
549
576
  }
550
577
  }
551
578
  } finally {
@@ -554,7 +581,7 @@ var K = class {
554
581
  }
555
582
  }
556
583
  _logNotifyError(t) {
557
- console.error(N(t, g, h.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED));
584
+ console.error(A(t, y, l.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED));
558
585
  }
559
586
  _isDirty() {
560
587
  const t = this._deps;
@@ -562,38 +589,38 @@ var K = class {
562
589
  const e = this._hotIndex;
563
590
  if (e !== -1) {
564
591
  const s = t.getAt(e);
565
- if (s != null && s.node.version !== s.version) return !0;
592
+ if (s !== null && s.node.version !== s.version) return !0;
566
593
  }
567
594
  return this._deepDirtyCheck();
568
595
  }
569
- }, H = 0;
570
- function J() {
571
- const t = H + 1 & k;
572
- return H = t === 0 ? 1 : t, H;
596
+ }, Q = 0;
597
+ function et() {
598
+ const t = Q + 1 & H;
599
+ return Q = t === 0 ? 1 : t, Q;
573
600
  }
574
- function j(t) {
575
- const e = t + 1 & k;
601
+ function K(t) {
602
+ const e = t + 1 & H;
576
603
  return e === 0 ? 1 : e;
577
604
  }
578
- var Z = 0, z = !1, ct = 0;
605
+ var st = 0, B = !1, _t = 0;
579
606
  function Tt() {
580
- return ct;
607
+ return _t;
581
608
  }
582
- function nt() {
583
- return z ? (_ && console.warn("startFlush() called during flush - ignored"), !1) : (z = !0, ct = J(), Z = 0, !0);
609
+ function ot() {
610
+ return B ? (f && console.warn("startFlush() called during flush - ignored"), !1) : (B = !0, _t = et(), st = 0, !0);
584
611
  }
585
- function rt() {
586
- z = !1;
612
+ function ut() {
613
+ B = !1;
587
614
  }
588
615
  function mt() {
589
- if (!z) return 0;
590
- const t = ++Z;
591
- if (t <= C.MAX_EXECUTIONS_PER_FLUSH) return t;
592
- throw new Error(`[atom-effect] Infinite loop detected: flush execution count exceeded ${C.MAX_EXECUTIONS_PER_FLUSH}`);
616
+ if (!B) return 0;
617
+ const t = ++st;
618
+ if (t <= g.MAX_EXECUTIONS_PER_FLUSH) return t;
619
+ throw new Error(`[atom-effect] Infinite loop detected: flush execution count exceeded ${g.MAX_EXECUTIONS_PER_FLUSH}`);
593
620
  }
594
- var vt = class {
621
+ var Ot = class {
595
622
  constructor() {
596
- this._queueBuffer = [[], []], this._bufferIndex = 0, this._size = 0, this._epoch = 0, this._isProcessing = !1, this._isFlushingSync = !1, this._batchDepth = 0, this._batchQueue = [], this._batchQueueSize = 0, this._maxFlushIterations = C.MAX_FLUSH_ITERATIONS, this.onOverflow = null, this._boundRunLoop = this._runLoop.bind(this);
623
+ this._bufferIndex = 0, this._size = 0, this._epoch = 0, this._batchDepth = 0, this._batchQueueSize = 0, this._maxFlushIterations = g.MAX_FLUSH_ITERATIONS, this._isProcessing = !1, this._isFlushingSync = !1, this._buffer0 = [], this._buffer1 = [], this._batchQueue = [], this.onOverflow = null, this._boundRunLoop = this._runLoop.bind(this);
597
624
  }
598
625
  get queueSize() {
599
626
  return this._size + this._batchQueueSize;
@@ -602,25 +629,25 @@ var vt = class {
602
629
  return this._batchDepth > 0;
603
630
  }
604
631
  schedule(t) {
605
- if (_ && typeof t != "function" && (!t || typeof t.execute != "function"))
606
- throw new L(h.SCHEDULER_CALLBACK_MUST_BE_FUNCTION);
632
+ if (f && typeof t != "function" && (!t || typeof t.execute != "function"))
633
+ throw new x(l.SCHEDULER_CALLBACK_MUST_BE_FUNCTION);
607
634
  const e = this._epoch;
608
635
  if (t._nextEpoch === e) return;
609
636
  if (t._nextEpoch = e, this._batchDepth > 0 || this._isFlushingSync) {
610
637
  this._batchQueue[this._batchQueueSize++] = t;
611
638
  return;
612
639
  }
613
- const s = this._queueBuffer[this._bufferIndex];
640
+ const s = this._bufferIndex === 0 ? this._buffer0 : this._buffer1;
614
641
  s[this._size++] = t, this._isProcessing || this._flush();
615
642
  }
616
643
  _flush() {
617
- this._isProcessing || this._size === 0 || (this._isProcessing = !0, queueMicrotask(this._boundRunLoop));
644
+ this._isProcessing || this._size === 0 && this._batchQueueSize === 0 || (this._isProcessing = !0, queueMicrotask(this._boundRunLoop));
618
645
  }
619
646
  _runLoop() {
620
647
  try {
621
648
  if (this._size === 0 && this._batchQueueSize === 0) return;
622
- const t = nt();
623
- this._drainQueue(), t && rt();
649
+ const t = ot();
650
+ this._drainQueue(), t && ut();
624
651
  } finally {
625
652
  this._isProcessing = !1;
626
653
  }
@@ -629,24 +656,23 @@ var vt = class {
629
656
  if (this._size === 0 && this._batchQueueSize === 0) return;
630
657
  const t = this._isFlushingSync;
631
658
  this._isFlushingSync = !0;
632
- const e = nt();
659
+ const e = ot();
633
660
  try {
634
661
  this._mergeBatchQueue(), this._drainQueue();
635
662
  } finally {
636
- this._isFlushingSync = t, e && rt();
663
+ this._isFlushingSync = t, e && ut();
637
664
  }
638
665
  }
639
666
  _mergeBatchQueue() {
640
667
  const t = this._batchQueueSize;
641
668
  if (t === 0) return;
642
- this._epoch = this._epoch + 1 | 0;
643
- const e = this._epoch, s = this._batchQueue, i = this._queueBuffer[this._bufferIndex];
669
+ const e = ++this._epoch | 0, s = this._batchQueue, i = this._bufferIndex === 0 ? this._buffer0 : this._buffer1;
644
670
  let n = this._size;
645
671
  for (let r = 0; r < t; r++) {
646
672
  const o = s[r];
647
673
  o._nextEpoch !== e && (o._nextEpoch = e, i[n++] = o), s[r] = void 0;
648
674
  }
649
- this._size = n, this._batchQueueSize = 0, s.length > C.BATCH_QUEUE_SHRINK_THRESHOLD && (s.length = 0);
675
+ this._size = n, this._batchQueueSize = 0, s.length > g.BATCH_QUEUE_SHRINK_THRESHOLD && (s.length = 0);
650
676
  }
651
677
  _drainQueue() {
652
678
  let t = 0;
@@ -659,7 +685,7 @@ var vt = class {
659
685
  }
660
686
  }
661
687
  _processQueue() {
662
- const t = this._bufferIndex, e = this._queueBuffer[t], s = this._size;
688
+ const t = this._bufferIndex, e = t === 0 ? this._buffer0 : this._buffer1, s = this._size;
663
689
  this._bufferIndex = t ^ 1, this._size = 0, this._epoch = this._epoch + 1 | 0;
664
690
  for (let i = 0; i < s; i++) {
665
691
  const n = e[i];
@@ -667,13 +693,13 @@ var vt = class {
667
693
  try {
668
694
  typeof n == "function" ? n() : n.execute();
669
695
  } catch (r) {
670
- console.error(new L("Error occurred during scheduler execution", r));
696
+ console.error(new x("Error occurred during scheduler execution", r));
671
697
  }
672
698
  }
673
699
  }
674
700
  _handleFlushOverflow() {
675
701
  const t = this._size + this._batchQueueSize;
676
- console.error(new L(h.SCHEDULER_FLUSH_OVERFLOW(this._maxFlushIterations, t))), this._size = 0, this._queueBuffer[0].length = 0, this._queueBuffer[1].length = 0, this._batchQueueSize = 0, this._batchQueue.length = 0;
702
+ console.error(new x(l.SCHEDULER_FLUSH_OVERFLOW(this._maxFlushIterations, t))), this._size = 0, this._buffer0.length = 0, this._buffer1.length = 0, this._batchQueueSize = 0, this._batchQueue.length = 0;
677
703
  const e = this.onOverflow;
678
704
  if (e) try {
679
705
  e(t);
@@ -685,116 +711,136 @@ var vt = class {
685
711
  }
686
712
  endBatch() {
687
713
  if (this._batchDepth === 0) {
688
- _ && console.warn(h.SCHEDULER_END_BATCH_WITHOUT_START);
714
+ f && console.warn(l.SCHEDULER_END_BATCH_WITHOUT_START);
689
715
  return;
690
716
  }
691
717
  --this._batchDepth === 0 && (this._isFlushingSync || this._flushSync());
692
718
  }
693
719
  setMaxFlushIterations(t) {
694
- if (t < C.MIN_FLUSH_ITERATIONS) throw new L(`Max iterations must be at least ${C.MIN_FLUSH_ITERATIONS}`);
720
+ if (t < g.MIN_FLUSH_ITERATIONS) throw new x(`Max iterations must be at least ${g.MIN_FLUSH_ITERATIONS}`);
695
721
  this._maxFlushIterations = t;
696
722
  }
697
- }, b = new vt();
698
- function Ft(t) {
699
- if (_ && typeof t != "function") throw new TypeError(h.BATCH_CALLBACK_MUST_BE_FUNCTION);
700
- b.startBatch();
723
+ }, T = new Ot();
724
+ function Ut(t) {
725
+ if (f && typeof t != "function") throw new TypeError(l.BATCH_CALLBACK_MUST_BE_FUNCTION);
726
+ T.startBatch();
701
727
  try {
702
728
  return t();
703
729
  } finally {
704
- b.endBatch();
730
+ T.endBatch();
705
731
  }
706
732
  }
707
- var Ot = class extends K {
733
+ var R = null;
734
+ function Lt(t) {
735
+ return t ? new Promise((e, s) => {
736
+ T.schedule(() => {
737
+ try {
738
+ t(), e();
739
+ } catch (i) {
740
+ s(i);
741
+ }
742
+ });
743
+ }) : R || (R = new Promise((e) => {
744
+ T.schedule(() => {
745
+ R = null, e();
746
+ });
747
+ }), R);
748
+ }
749
+ var Nt = class extends tt {
708
750
  constructor(t, e) {
709
- super(), this[F] = p.Atom | p.Writable, this._value = t, this._equal = e.equal ?? Object.is, e.sync && (this.flags |= f.SYNC), d.attachDebugInfo(this, "atom", this.id, e.name);
751
+ super(), this[L] = p.Atom | p.Writable, this._value = t, this._equal = e.equal ?? Object.is, e.sync && (this.flags |= C.SYNC), f && d.attachDebugInfo(this, "atom", this.id, e.name);
710
752
  }
711
753
  get isNotificationScheduled() {
712
- return (this.flags & f.NOTIFICATION_SCHEDULED) !== 0;
754
+ return (this.flags & C.NOTIFICATION_SCHEDULED) !== 0;
713
755
  }
714
756
  get isSync() {
715
- return (this.flags & f.SYNC) !== 0;
757
+ return (this.flags & C.SYNC) !== 0;
716
758
  }
717
759
  get value() {
718
- const t = l.current;
760
+ const t = _.current;
719
761
  return t?.addDependency(this), this._value;
720
762
  }
721
763
  set value(t) {
722
764
  const e = this._value;
723
- if (this._equal(e, t) || (this._value = t, this.version = j(this.version), d.trackUpdate(this.id, d.getDebugName(this)), (this.flags & f.NOTIFICATION_SCHEDULED) !== 0)) return;
724
- const s = this._slots;
725
- s == null || s.size === 0 || (this._pendingOldValue = e, this.flags |= f.NOTIFICATION_SCHEDULED, (this.flags & f.SYNC) !== 0 && !b.isBatching ? this._notifying === 0 && this._flushNotifications() : b.schedule(this));
765
+ if (this._equal(e, t)) return;
766
+ this._value = t, this.version = K(this.version), f && d.trackUpdate(this.id, d.getDebugName(this));
767
+ const s = this.flags, i = C.NOTIFICATION_SCHEDULED;
768
+ if ((s & i) !== 0) return;
769
+ const n = this._slots;
770
+ if (n === null || n.size === 0) return;
771
+ this._pendingOldValue = e;
772
+ const r = s | i;
773
+ if (this.flags = r, (r & C.SYNC) !== 0 && !T.isBatching) {
774
+ this._notifying === 0 && this._flushNotifications();
775
+ return;
776
+ }
777
+ T.schedule(this);
726
778
  }
727
779
  execute() {
728
780
  this._flushNotifications();
729
781
  }
730
782
  _flushNotifications() {
731
- const t = f.NOTIFICATION_SCHEDULED, e = f.DISPOSED, s = f.SYNC;
732
- for (; (this.flags & (t | e)) === t; ) {
733
- const i = this._pendingOldValue;
734
- if (this._pendingOldValue = void 0, this.flags &= ~t, this._equal(this._value, i) || this._notifySubscribers(this._value, i), (this.flags & s) === 0 || b.isBatching) break;
783
+ const t = C.NOTIFICATION_SCHEDULED, e = C.DISPOSED, s = C.SYNC, i = t | e;
784
+ let n = this.flags;
785
+ for (; (n & i) === t; ) {
786
+ const r = this._pendingOldValue;
787
+ this._pendingOldValue = void 0, this.flags = n &= ~t;
788
+ const o = this._value;
789
+ if (this._equal(o, r) || this._notifySubscribers(o, r), n = this.flags, (n & s) === 0 || T.isBatching) break;
735
790
  }
736
791
  }
737
792
  peek() {
738
793
  return this._value;
739
794
  }
740
795
  dispose() {
741
- const t = this.flags;
742
- (t & f.DISPOSED) === 0 && (this._slots?.clear(), this.flags = t | f.DISPOSED, this._value = void 0, this._pendingOldValue = void 0, this._equal = Object.is);
796
+ const t = this.flags, e = C.DISPOSED;
797
+ (t & e) === 0 && (this.flags = t | e, this._slots?.clear(), this._value = void 0, this._pendingOldValue = void 0, this._equal = Object.is);
743
798
  }
744
799
  _deepDirtyCheck() {
745
800
  return !1;
746
801
  }
747
- [Symbol.dispose]() {
748
- this.dispose();
749
- }
750
802
  };
751
- function Ut(t, e = {}) {
752
- return new Ot(t, e);
803
+ function Pt(t, e = {}) {
804
+ return new Nt(t, e);
753
805
  }
754
- var { IDLE: D, DIRTY: E, PENDING: O, RESOLVED: I, REJECTED: T, HAS_ERROR: P, RECOMPUTING: y, DISPOSED: G, IS_COMPUTED: R, FORCE_COMPUTE: X } = Q, Nt = class extends K {
806
+ var { IDLE: v, DIRTY: a, PENDING: b, RESOLVED: D, REJECTED: m, HAS_ERROR: V, RECOMPUTING: O, DISPOSED: z, IS_COMPUTED: F, FORCE_COMPUTE: Y } = $, bt = class extends tt {
755
807
  constructor(t, e = {}) {
756
- if (typeof t != "function") throw new A(h.COMPUTED_MUST_BE_FUNCTION);
757
- if (super(), this[F] = p.Atom | p.Computed, this._error = null, this._promiseId = 0, this._deps = new ht(), this._trackEpoch = w.UNINITIALIZED, this._trackCount = 0, this._value = void 0, this.flags = R | E | D, this._equal = e.equal ?? Object.is, this._fn = t, this._defaultValue = "defaultValue" in e ? e.defaultValue : B, this._onError = e.onError ?? null, d.attachDebugInfo(this, "computed", this.id, e.name), e.lazy === !1) try {
808
+ if (typeof t != "function") throw new w(l.COMPUTED_MUST_BE_FUNCTION);
809
+ if (super(), this[L] = p.Atom | p.Computed, this._promiseId = 0, this._trackEpoch = U.UNINITIALIZED, this._trackCount = 0, this._error = null, this._deps = new ct(), this._value = void 0, this.flags = F | a | v, this._equal = e.equal ?? Object.is, this._computation = t, this._defaultValue = "defaultValue" in e ? e.defaultValue : G, this._onError = e.onError ?? null, d.attachDebugInfo(this, "computed", this.id, e.name), e.lazy === !1) try {
758
810
  this._recompute();
759
811
  } catch {
760
812
  }
761
813
  }
762
814
  get isDirty() {
763
- return (this.flags & E) !== 0;
815
+ return (this.flags & a) !== 0;
764
816
  }
765
817
  get isRejected() {
766
- return (this.flags & T) !== 0;
818
+ return (this.flags & m) !== 0;
767
819
  }
768
820
  get isRecomputing() {
769
- return (this.flags & y) !== 0;
770
- }
771
- get _hasErrorInternal() {
772
- return (this.flags & P) !== 0;
773
- }
774
- _track() {
775
- l.current?.addDependency(this);
821
+ return (this.flags & O) !== 0;
776
822
  }
777
823
  get value() {
778
- const t = l.current;
779
- t?.addDependency(this);
780
- let e = this.flags;
781
- if ((e & (I | E | D)) === I) return this._value;
782
- if ((e & G) !== 0) throw new A(h.COMPUTED_DISPOSED);
783
- if ((e & y) !== 0) {
824
+ const t = _.current;
825
+ t !== null && t.addDependency(this);
826
+ const e = this.flags;
827
+ if ((e & (D | a | v | z | O)) === D) return this._value;
828
+ if ((e & z) !== 0) throw new w(l.COMPUTED_DISPOSED);
829
+ if ((e & O) !== 0) {
784
830
  const n = this._defaultValue;
785
- if (n !== B) return n;
786
- throw new A(h.COMPUTED_CIRCULAR_DEPENDENCY);
831
+ if (n !== G) return n;
832
+ throw new w(l.COMPUTED_CIRCULAR_DEPENDENCY);
787
833
  }
788
- if ((e & (E | D)) !== 0) {
834
+ if ((e & (a | v)) !== 0) {
789
835
  const n = this._deps;
790
- if ((e & D) === 0 && (e & X) === 0 && n.size > 0 && !this._isDirty() ? e = this.flags &= ~E : (this._recompute(), e = this.flags), (e & I) !== 0) return this._value;
836
+ if ((e & (v | Y)) !== 0 || n.size === 0 || this._isDirty() ? this._recompute() : this.flags &= ~a, (this.flags & D) !== 0) return this._value;
791
837
  }
792
- const s = this._defaultValue, i = s !== B;
793
- if ((e & O) !== 0) {
838
+ const s = this._defaultValue, i = s !== G;
839
+ if ((this.flags & b) !== 0) {
794
840
  if (i) return s;
795
- throw new A(h.COMPUTED_ASYNC_PENDING_NO_DEFAULT);
841
+ throw new w(l.COMPUTED_ASYNC_PENDING_NO_DEFAULT);
796
842
  }
797
- if ((e & T) !== 0) {
843
+ if ((this.flags & m) !== 0) {
798
844
  if (i) return s;
799
845
  throw this._error;
800
846
  }
@@ -804,16 +850,16 @@ var { IDLE: D, DIRTY: E, PENDING: O, RESOLVED: I, REJECTED: T, HAS_ERROR: P, REC
804
850
  return this._value;
805
851
  }
806
852
  get state() {
807
- const t = l.current;
808
- t?.addDependency(this);
853
+ const t = _.current;
854
+ t !== null && t.addDependency(this);
809
855
  const e = this.flags;
810
- return (e & I) !== 0 ? U.RESOLVED : (e & O) !== 0 ? U.PENDING : (e & T) !== 0 ? U.REJECTED : U.IDLE;
856
+ return (e & D) !== 0 ? P.RESOLVED : (e & b) !== 0 ? P.PENDING : (e & m) !== 0 ? P.REJECTED : P.IDLE;
811
857
  }
812
858
  get hasError() {
813
- const t = l.current;
814
- if (t?.addDependency(this), (this.flags & (T | P)) !== 0) return !0;
859
+ const t = _.current;
860
+ if (t !== null && t.addDependency(this), (this.flags & (m | V)) !== 0) return !0;
815
861
  const e = this._deps;
816
- return e.hasComputeds ? M(() => {
862
+ return e.hasComputeds ? X(() => {
817
863
  const s = e.size;
818
864
  for (let i = 0; i < s; i++) if (e.getAt(i)?.node.hasError) return !0;
819
865
  return !1;
@@ -823,204 +869,196 @@ var { IDLE: D, DIRTY: E, PENDING: O, RESOLVED: I, REJECTED: T, HAS_ERROR: P, REC
823
869
  return !this.hasError;
824
870
  }
825
871
  get errors() {
826
- const t = l.current;
827
- t?.addDependency(this);
872
+ const t = _.current;
873
+ t !== null && t.addDependency(this);
828
874
  const e = this._error, s = this._deps;
829
875
  if (!s.hasComputeds)
830
- return e == null ? et : Object.freeze([e]);
876
+ return e === null ? it : Object.freeze([e]);
831
877
  const i = [];
832
- return e != null && i.push(e), M(() => {
878
+ return e !== null && i.push(e), X(() => {
833
879
  const n = s.size;
834
880
  for (let r = 0; r < n; r++) {
835
881
  const o = s.getAt(r)?.node;
836
- o != null && (o.flags & R) !== 0 && this._accumulateErrors(o, i);
882
+ o !== void 0 && (o.flags & F) !== 0 && this._accumulateErrors(o, i);
837
883
  }
838
- }), i.length === 0 ? et : Object.freeze(i);
884
+ }), i.length === 0 ? it : Object.freeze(i);
839
885
  }
840
886
  _accumulateErrors(t, e) {
841
887
  const s = t._error;
842
- s != null && !e.includes(s) && e.push(s);
888
+ s !== null && !e.includes(s) && e.push(s);
843
889
  const i = t._deps;
844
890
  if (!i.hasComputeds) return;
845
891
  const n = i.size;
846
892
  for (let r = 0; r < n; r++) {
847
893
  const o = i.getAt(r)?.node;
848
- o != null && (o.flags & R) !== 0 && this._accumulateErrors(o, e);
894
+ o !== void 0 && (o.flags & F) !== 0 && this._accumulateErrors(o, e);
849
895
  }
850
896
  }
851
897
  get lastError() {
852
- const t = l.current;
853
- return t?.addDependency(this), this._error;
898
+ const t = _.current;
899
+ return t !== null && t.addDependency(this), this._error;
854
900
  }
855
901
  get isPending() {
856
- const t = l.current;
857
- return t?.addDependency(this), (this.flags & O) !== 0;
902
+ const t = _.current;
903
+ return t !== null && t.addDependency(this), (this.flags & b) !== 0;
858
904
  }
859
905
  get isResolved() {
860
- const t = l.current;
861
- return t?.addDependency(this), (this.flags & I) !== 0;
906
+ const t = _.current;
907
+ return t !== null && t.addDependency(this), (this.flags & D) !== 0;
862
908
  }
863
909
  invalidate() {
864
- this.flags |= X, this._markDirty();
910
+ this.flags |= Y, this._markDirty();
865
911
  }
866
912
  dispose() {
867
- (this.flags & G) === 0 && (this._deps.disposeAll(), this._slots != null && this._slots.clear(), this.flags = G | E | D, this._error = null, this._value = void 0, this._hotIndex = -1);
868
- }
869
- [Symbol.dispose]() {
870
- this.dispose();
913
+ (this.flags & z) === 0 && (this._deps.disposeAll(), this._slots !== null && this._slots.clear(), this.flags = z | a | v, this._error = null, this._value = void 0, this._hotIndex = -1);
871
914
  }
872
915
  addDependency(t) {
873
916
  const e = this._trackEpoch;
874
917
  if (t._lastSeenEpoch === e) return;
875
918
  t._lastSeenEpoch = e;
876
- const s = this._trackCount++, i = this._deps, n = i.getAt(s);
877
- if (n != null && n.node === t) n.version = t.version;
919
+ const s = this._trackCount++, i = this._deps;
920
+ let n = null;
921
+ if (s < 4) s === 0 ? n = i._s0 : s === 1 ? n = i._s1 : s === 2 ? n = i._s2 : n = i._s3;
922
+ else {
923
+ const r = i._overflow;
924
+ r !== null && (n = r[s - 4] ?? null);
925
+ }
926
+ if (n !== null && n.node === t) n.version = t.version;
878
927
  else if (!i.claimExisting(t, s)) {
879
- const r = new W(t, t.version, t.subscribe(this));
928
+ const r = new q(t, t.version, t.subscribe(this));
880
929
  i.insertNew(s, r);
881
930
  }
882
- (t.flags & R) !== 0 && (i.hasComputeds = !0);
931
+ (t.flags & F) !== 0 && (i.hasComputeds = !0);
883
932
  }
884
933
  _recompute() {
885
- if (this.isRecomputing) return;
886
- this.flags = (this.flags | y) & ~X, this._trackEpoch = J(), this._trackCount = 0, this._deps.prepareTracking(), this._hotIndex = -1;
934
+ if ((this.flags & O) !== 0) return;
935
+ this.flags = (this.flags | O) & ~Y, this._trackEpoch = et(), this._trackCount = 0, this._deps.prepareTracking(), this._hotIndex = -1;
887
936
  let t = !1;
888
937
  try {
889
- const e = l.run(this, this._fn);
890
- this._deps.truncateFrom(this._trackCount), t = !0, q(e) ? this._handleAsyncComputation(e) : this._finalizeResolution(e);
938
+ const e = _.run(this, this._computation);
939
+ this._deps.truncateFrom(this._trackCount), t = !0, Z(e) ? this._handleAsyncComputation(e) : this._finalizeResolution(e);
891
940
  } catch (e) {
892
941
  if (!t) try {
893
942
  this._deps.truncateFrom(this._trackCount);
894
943
  } catch (s) {
895
- _ && console.warn("[atom-effect] _commitDeps failed during error recovery:", s);
944
+ f && console.warn("[atom-effect] _commitDeps failed during error recovery:", s);
896
945
  }
897
- this._handleError(e, h.COMPUTED_COMPUTATION_FAILED, !0);
946
+ this._handleError(e, l.COMPUTED_COMPUTATION_FAILED, !0);
898
947
  } finally {
899
- this._trackEpoch = w.UNINITIALIZED, this._trackCount = 0, this.flags &= ~y;
948
+ this._trackEpoch = U.UNINITIALIZED, this._trackCount = 0, this.flags &= ~O;
900
949
  }
901
950
  }
902
951
  _handleAsyncComputation(t) {
903
- this.flags = (this.flags | O) & ~(D | E | I | T), this._notifySubscribers(void 0, void 0), this._promiseId = (this._promiseId + 1) % ft.MAX_PROMISE_ID;
952
+ this.flags = (this.flags | b) & ~(v | a | D | m), this._notifySubscribers(void 0, void 0), this._promiseId = (this._promiseId + 1) % Et.MAX_PROMISE_ID;
904
953
  const e = this._promiseId;
905
954
  t.then((s) => {
906
955
  if (e === this._promiseId) {
907
956
  if (this._isDirty()) return this._markDirty();
908
957
  this._finalizeResolution(s), this._notifySubscribers(s, void 0);
909
958
  }
910
- }, (s) => e === this._promiseId && this._handleError(s, h.COMPUTED_ASYNC_COMPUTATION_FAILED));
959
+ }, (s) => e === this._promiseId && this._handleError(s, l.COMPUTED_ASYNC_COMPUTATION_FAILED));
911
960
  }
912
961
  _handleError(t, e, s = !1) {
913
- const i = N(t, A, e);
914
- if ((!this.isRejected || this._error !== i) && (this.version = j(this.version)), this._error = i, this.flags = this.flags & ~(D | E | O | I) | T | P, this._onError) try {
962
+ const i = A(t, w, e);
963
+ if ((!this.isRejected || this._error !== i) && (this.version = K(this.version)), this._error = i, this.flags = this.flags & ~(v | a | b | D) | m | V, this._onError) try {
915
964
  this._onError(i);
916
965
  } catch (n) {
917
- console.error(h.CALLBACK_ERROR_IN_ERROR_HANDLER, n);
966
+ console.error(l.CALLBACK_ERROR_IN_ERROR_HANDLER, n);
918
967
  }
919
968
  if (this._notifySubscribers(void 0, void 0), s) throw i;
920
969
  }
921
970
  _finalizeResolution(t) {
922
971
  const e = this.flags;
923
- ((e & I) === 0 || !this._equal(this._value, t)) && (this.version = j(this.version)), this._value = t, this._error = null, this.flags = (e | I) & ~(D | E | O | T | P);
972
+ ((e & D) === 0 || !this._equal(this._value, t)) && (this.version = K(this.version)), this._value = t, this._error = null, this.flags = (e | D) & ~(v | a | b | m | V);
924
973
  }
925
974
  execute() {
926
975
  this._markDirty();
927
976
  }
928
977
  _markDirty() {
929
978
  const t = this.flags;
930
- (t & (y | E)) === 0 && (this.flags = t | E, d.trackUpdate(this.id, d.getDebugName(this)), this._notifySubscribers(void 0, void 0));
979
+ (t & (O | a)) === 0 && (this.flags = t | a, d.trackUpdate(this.id, d.getDebugName(this)), this._notifySubscribers(void 0, void 0));
931
980
  }
932
981
  _deepDirtyCheck() {
933
- const t = this._deps;
934
- return M(() => {
935
- const e = t.size;
936
- for (let s = 0; s < e; s++) {
982
+ const t = this._deps, e = t.size, s = this._hotIndex;
983
+ return X(() => {
984
+ if (s !== -1 && s < e) {
937
985
  const i = t.getAt(s);
938
- if (i == null) continue;
939
- const n = i.node;
940
- if ((n.flags & R) !== 0) try {
941
- n.value;
942
- } catch {
943
- _ && console.warn(`[atom-effect] Dependency #${n.id} threw during dirty check`);
944
- }
945
- if (n.version !== i.version)
946
- return this._hotIndex = s, !0;
986
+ if (i !== null && this._checkLinkDirty(i)) return !0;
987
+ }
988
+ for (let i = 0; i < e; i++) {
989
+ if (i === s) continue;
990
+ const n = t.getAt(i);
991
+ if (n !== null && this._checkLinkDirty(n))
992
+ return this._hotIndex = i, !0;
947
993
  }
948
994
  return this._hotIndex = -1, !1;
949
995
  });
950
996
  }
997
+ _checkLinkDirty(t) {
998
+ const e = t.node;
999
+ if ((e.flags & F) !== 0) try {
1000
+ e.value;
1001
+ } catch {
1002
+ f && console.warn(`[atom-effect] Dependency #${e.id} threw during check`);
1003
+ }
1004
+ return e.version !== t.version;
1005
+ }
951
1006
  };
952
- function Lt(t, e = {}) {
953
- return new Nt(t, e);
1007
+ function xt(t, e = {}) {
1008
+ return new bt(t, e);
954
1009
  }
955
- var bt = class extends K {
1010
+ var At = class extends tt {
956
1011
  constructor(t, e = {}) {
957
- super(), this[F] = p.Effect, this._cleanup = null, this._deps = new ht(), this._currentEpoch = w.UNINITIALIZED, this._lastFlushEpoch = w.UNINITIALIZED, this._fn = t, this._onError = e.onError ?? null, this._sync = e.sync ?? !1, this._maxExecutions = e.maxExecutionsPerSecond ?? C.MAX_EXECUTIONS_PER_SECOND, this._maxExecutionsPerFlush = e.maxExecutionsPerFlush ?? C.MAX_EXECUTIONS_PER_EFFECT, this._executionsInEpoch = 0, this._executionCount = 0, this._windowStart = 0, this._windowCount = 0, this._execId = 0, this._trackCount = 0, this._sync ? this._notifyCallback = () => this.execute() : this._notifyCallback = () => b.schedule(this), d.attachDebugInfo(this, "effect", this.id, e.name);
1012
+ super(), this[L] = p.Effect, this._currentEpoch = U.UNINITIALIZED, this._lastFlushEpoch = U.UNINITIALIZED, this._executionsInEpoch = 0, this._executionCount = 0, this._windowStart = 0, this._windowCount = 0, this._execId = 0, this._trackCount = 0, this._cleanup = null, this._deps = new ct(), this._fn = t, this._onError = e.onError ?? null, this._sync = e.sync ?? !1, this._maxExecutions = e.maxExecutionsPerSecond ?? g.MAX_EXECUTIONS_PER_SECOND, this._maxExecutionsPerFlush = e.maxExecutionsPerFlush ?? g.MAX_EXECUTIONS_PER_EFFECT, this._sync ? this._notifyCallback = () => this.execute() : this._notifyCallback = () => T.schedule(this), d.attachDebugInfo(this, "effect", this.id, e.name);
958
1013
  }
959
1014
  run() {
960
- if (this.isDisposed) throw new m(h.EFFECT_DISPOSED);
1015
+ if (this.isDisposed) throw new N(l.EFFECT_DISPOSED);
961
1016
  this.execute(!0);
962
1017
  }
963
1018
  dispose() {
964
1019
  this.isDisposed || (this.flags |= S.DISPOSED, this._execCleanup(), this._deps?.disposeAll());
965
1020
  }
966
- [Symbol.dispose]() {
967
- this.dispose();
968
- }
969
1021
  addDependency(t) {
970
- if ((this.flags & S.EXECUTING) === 0) return;
971
- const e = this._currentEpoch;
972
- if (t._lastSeenEpoch === e) return;
973
- t._lastSeenEpoch = e;
974
- const s = this._trackCount++, i = this._deps;
975
- let n;
976
- switch (s) {
977
- case 0:
978
- n = i._s0;
979
- break;
980
- case 1:
981
- n = i._s1;
982
- break;
983
- case 2:
984
- n = i._s2;
985
- break;
986
- case 3:
987
- n = i._s3;
988
- break;
989
- default:
990
- n = i.getAt(s);
1022
+ if ((this.flags & S.EXECUTING) === 0 || t._lastSeenEpoch === this._currentEpoch) return;
1023
+ t._lastSeenEpoch = this._currentEpoch;
1024
+ const e = this._trackCount++, s = this._deps, i = t.version;
1025
+ let n = null;
1026
+ if (e < 4) e === 0 ? n = s._s0 : e === 1 ? n = s._s1 : e === 2 ? n = s._s2 : n = s._s3;
1027
+ else {
1028
+ const r = s._overflow;
1029
+ r !== null && (n = r[e - 4] ?? null);
991
1030
  }
992
- n != null && n.node === t ? n.version = t.version : i.claimExisting(t, s) || this._insertNewDependency(t, s), t.isComputed && (i.hasComputeds = !0);
1031
+ n !== null && n.node === t ? n.version = i : s.claimExisting(t, e) || this._insertNewDependency(t, e, i), t.isComputed && !s.hasComputeds && (s.hasComputeds = !0);
993
1032
  }
994
- _insertNewDependency(t, e) {
995
- let s;
1033
+ _insertNewDependency(t, e, s) {
1034
+ let i;
996
1035
  try {
997
- const i = t.subscribe(this._notifyCallback);
998
- s = new W(t, t.version, i);
999
- } catch (i) {
1000
- const n = N(i, m, h.EFFECT_EXECUTION_FAILED);
1001
- if (console.error(n), this._onError) try {
1002
- this._onError(n);
1036
+ i = new q(t, s, t.subscribe(this._notifyCallback));
1037
+ } catch (n) {
1038
+ const r = A(n, N, l.EFFECT_EXECUTION_FAILED);
1039
+ if (console.error(r), this._onError) try {
1040
+ this._onError(r);
1003
1041
  } catch {
1004
1042
  }
1005
- s = new W(t, t.version, void 0);
1043
+ i = new q(t, s, void 0);
1006
1044
  }
1007
- this._deps.insertNew(e, s);
1045
+ this._deps.insertNew(e, i);
1008
1046
  }
1009
1047
  execute(t = !1) {
1010
1048
  const e = this.flags;
1011
1049
  if ((e & (S.DISPOSED | S.EXECUTING)) !== 0) return;
1012
1050
  const s = this._deps;
1013
- if (!t && s.size > 0 && !this._isDirty()) return;
1014
- this._checkInfiniteLoops(), d.trackUpdate(this.id, d.getDebugName(this)), this.flags = e | S.EXECUTING, this._execCleanup(), this._currentEpoch = J(), this._trackCount = 0, s.prepareTracking(), this._hotIndex = -1;
1051
+ if (!t && s.physicalSize > 0 && !this._isDirty()) return;
1052
+ this._checkInfiniteLoops(), d.trackUpdate(this.id, d.getDebugName(this)), this.flags = e | S.EXECUTING, this._execCleanup(), this._currentEpoch = et(), this._trackCount = 0, s.prepareTracking(), this._hotIndex = -1;
1015
1053
  let i = !1;
1016
1054
  try {
1017
- const n = l.run(this, this._fn);
1018
- s.truncateFrom(this._trackCount), i = !0, q(n) ? this._handleAsyncResult(n) : this._cleanup = typeof n == "function" ? n : null;
1055
+ const n = _.run(this, this._fn);
1056
+ s.truncateFrom(this._trackCount), i = !0, typeof n == "function" ? this._cleanup = n : Z(n) ? this._handleAsyncResult(n) : this._cleanup = null;
1019
1057
  } catch (n) {
1020
1058
  if (!i) try {
1021
1059
  s.truncateFrom(this._trackCount);
1022
1060
  } catch (r) {
1023
- _ && console.warn("[atom-effect] _commitDeps failed during error recovery:", r);
1061
+ f && console.warn("[atom-effect] _commitDeps failed during error recovery:", r);
1024
1062
  }
1025
1063
  this._handleExecutionError(n), this._cleanup = null;
1026
1064
  } finally {
@@ -1030,40 +1068,50 @@ var bt = class extends K {
1030
1068
  _handleAsyncResult(t) {
1031
1069
  const e = ++this._execId;
1032
1070
  t.then((s) => {
1033
- if (e !== this._execId || this.isDisposed) {
1071
+ if (e !== this._execId || (this.flags & S.DISPOSED) !== 0) {
1034
1072
  if (typeof s == "function") try {
1035
1073
  s();
1036
1074
  } catch (i) {
1037
- this._handleExecutionError(i, h.EFFECT_CLEANUP_FAILED);
1075
+ this._handleExecutionError(i, l.EFFECT_CLEANUP_FAILED);
1038
1076
  }
1039
1077
  return;
1040
1078
  }
1041
1079
  typeof s == "function" && (this._cleanup = s);
1042
1080
  }, (s) => e === this._execId && this._handleExecutionError(s));
1043
1081
  }
1044
- _deepDirtyCheck() {
1045
- const t = l.current;
1046
- l.current = null;
1047
- const e = this._deps;
1048
- try {
1049
- const s = e.size;
1050
- for (let i = 0; i < s; i++) {
1051
- const n = e.getAt(i);
1052
- if (n == null) continue;
1053
- const r = n.node;
1054
- if (r.isComputed && this._tryPullComputed(r), r.version !== n.version)
1055
- return this._hotIndex = i, !0;
1082
+ _isDirty() {
1083
+ const t = this._deps, e = t.size;
1084
+ if (e === 0) return !1;
1085
+ const s = this._hotIndex;
1086
+ if (s !== -1 && s < e) {
1087
+ const i = t.getAt(s);
1088
+ if (i !== null) {
1089
+ const n = i.node;
1090
+ if (!n.isComputed && n.version !== i.version) return !0;
1056
1091
  }
1057
- return !1;
1058
- } finally {
1059
- l.current = t;
1060
1092
  }
1093
+ return this._deepDirtyCheck();
1061
1094
  }
1062
- _tryPullComputed(t) {
1095
+ _deepDirtyCheck() {
1096
+ const t = this._deps, e = t.size, s = this._hotIndex, i = _.current;
1097
+ _.current = null;
1063
1098
  try {
1064
- t.value;
1065
- } catch {
1066
- _ && console.warn(`[atom-effect] Dependency #${t.id} threw during dirty check`);
1099
+ for (let n = 0; n < e; n++) {
1100
+ if (n === s) continue;
1101
+ const r = t.getAt(n);
1102
+ if (r === null) continue;
1103
+ const o = r.node;
1104
+ if (o.isComputed) try {
1105
+ o.value;
1106
+ } catch {
1107
+ f && console.warn(`[atom-effect] Dependency #${o.id} error in check`);
1108
+ }
1109
+ if (o.version !== r.version)
1110
+ return this._hotIndex = n, !0;
1111
+ }
1112
+ return this._hotIndex = -1, !1;
1113
+ } finally {
1114
+ _.current = i;
1067
1115
  }
1068
1116
  }
1069
1117
  _execCleanup() {
@@ -1073,23 +1121,23 @@ var bt = class extends K {
1073
1121
  try {
1074
1122
  t();
1075
1123
  } catch (e) {
1076
- this._handleExecutionError(e, h.EFFECT_CLEANUP_FAILED);
1124
+ this._handleExecutionError(e, l.EFFECT_CLEANUP_FAILED);
1077
1125
  }
1078
1126
  }
1079
1127
  }
1080
1128
  _checkInfiniteLoops() {
1081
1129
  const t = Tt();
1082
- this._lastFlushEpoch !== t && (this._lastFlushEpoch = t, this._executionsInEpoch = 0), ++this._executionsInEpoch > this._maxExecutionsPerFlush && this._throwInfiniteLoopError("per-effect"), mt() > C.MAX_EXECUTIONS_PER_FLUSH && this._throwInfiniteLoopError("global"), this._executionCount++, _ && this._checkFrequencyLimit();
1130
+ this._lastFlushEpoch !== t && (this._lastFlushEpoch = t, this._executionsInEpoch = 0), ++this._executionsInEpoch > this._maxExecutionsPerFlush && this._throwInfiniteLoopError("per-effect"), mt() > g.MAX_EXECUTIONS_PER_FLUSH && this._throwInfiniteLoopError("global"), this._executionCount++, f && this._checkFrequencyLimit();
1083
1131
  }
1084
1132
  _checkFrequencyLimit() {
1085
1133
  if (!Number.isFinite(this._maxExecutions)) return;
1086
1134
  const t = Date.now();
1087
- if (t - this._windowStart >= Y.EFFECT_FREQUENCY_WINDOW) {
1135
+ if (t - this._windowStart >= W.EFFECT_FREQUENCY_WINDOW) {
1088
1136
  this._windowStart = t, this._windowCount = 1;
1089
1137
  return;
1090
1138
  }
1091
1139
  if (++this._windowCount > this._maxExecutions) {
1092
- const e = new m(h.EFFECT_FREQUENCY_LIMIT_EXCEEDED);
1140
+ const e = new N(l.EFFECT_FREQUENCY_LIMIT_EXCEEDED);
1093
1141
  throw this.dispose(), this._handleExecutionError(e), e;
1094
1142
  }
1095
1143
  }
@@ -1100,102 +1148,103 @@ var bt = class extends K {
1100
1148
  return (this.flags & S.EXECUTING) !== 0;
1101
1149
  }
1102
1150
  _throwInfiniteLoopError(t) {
1103
- const e = new m(`Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${Z}`);
1151
+ const e = new N(`Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${st}`);
1104
1152
  throw this.dispose(), console.error(e), e;
1105
1153
  }
1106
- _handleExecutionError(t, e = h.EFFECT_EXECUTION_FAILED) {
1107
- const s = N(t, m, e);
1154
+ _handleExecutionError(t, e = l.EFFECT_EXECUTION_FAILED) {
1155
+ const s = A(t, N, e);
1108
1156
  if (console.error(s), this._onError) try {
1109
1157
  this._onError(s);
1110
1158
  } catch (i) {
1111
- console.error(N(i, m, h.CALLBACK_ERROR_IN_ERROR_HANDLER));
1159
+ console.error(A(i, N, l.CALLBACK_ERROR_IN_ERROR_HANDLER));
1112
1160
  }
1113
1161
  }
1114
1162
  };
1115
- function Pt(t, e = {}) {
1116
- if (typeof t != "function") throw new m(h.EFFECT_MUST_BE_FUNCTION);
1117
- const s = new bt(t, e);
1163
+ function Mt(t, e = {}) {
1164
+ if (typeof t != "function") throw new N(l.EFFECT_MUST_BE_FUNCTION);
1165
+ const s = new At(t, e);
1118
1166
  return s.execute(), s;
1119
1167
  }
1120
- var lt = /^(?:__proto__|constructor|prototype)$/;
1121
- function _t(t, e, s, i) {
1168
+ function ft(t, e, s, i) {
1122
1169
  if (s === e.length) return i;
1123
1170
  const n = e[s];
1124
- if (lt.test(n)) return t;
1125
- const r = t != null && typeof t == "object" ? t : {}, o = r[n], c = _t(o, e, s + 1, i);
1126
- if (Object.is(o, c)) return t;
1171
+ if (n === "__proto__" || n === "constructor" || n === "prototype") return t;
1172
+ const r = t != null && typeof t == "object" ? t : {}, o = r[n], u = ft(o, e, s + 1, i);
1173
+ if (Object.is(o, u)) return t;
1127
1174
  if (Array.isArray(r)) {
1128
- const a = r.slice(), v = Number(n);
1129
- return n.trim() !== "" && Number.isInteger(v) && v >= 0 ? a[v] = c : a[n] = c, a;
1175
+ const I = r.slice(), E = +n;
1176
+ return n.trim() !== "" && E >= 0 && E % 1 === 0 ? I[E] = u : I[n] = u, I;
1130
1177
  }
1131
- return {
1132
- ...r,
1133
- [n]: c
1134
- };
1178
+ const c = { ...r };
1179
+ return c[n] = u, c;
1135
1180
  }
1136
- function x(t, e) {
1181
+ function k(t, e) {
1137
1182
  let s = t;
1138
1183
  const i = e.length;
1139
1184
  for (let n = 0; n < i; n++) {
1140
1185
  if (s == null) return;
1141
1186
  const r = e[n];
1142
- if (lt.test(r)) return;
1187
+ if (r === "__proto__" || r === "constructor" || r === "prototype") return;
1143
1188
  s = s[r];
1144
1189
  }
1145
1190
  return s;
1146
1191
  }
1147
1192
  function at(t, e) {
1148
- const s = e.includes(".") ? e.split(".") : [e], i = /* @__PURE__ */ new Set(), n = () => {
1149
- i.forEach((r) => r()), i.clear();
1150
- };
1193
+ const s = e.includes(".") ? e.split(".") : [e], i = /* @__PURE__ */ new Set();
1151
1194
  return {
1152
1195
  get value() {
1153
- return x(t.value, s);
1196
+ return k(t.value, s);
1154
1197
  },
1155
1198
  set value(r) {
1156
- const o = t.peek(), c = _t(o, s, 0, r);
1157
- c !== o && (t.value = c);
1199
+ const o = t.peek(), u = ft(o, s, 0, r);
1200
+ u !== o && (t.value = u);
1158
1201
  },
1159
- peek: () => x(t.peek(), s),
1202
+ peek: () => k(t.peek(), s),
1160
1203
  subscribe(r) {
1161
- const o = t.subscribe((c, a) => {
1162
- const v = x(c, s), tt = x(a, s);
1163
- Object.is(v, tt) || r(v, tt);
1204
+ let o = k(t.peek(), s);
1205
+ const u = t.subscribe((c) => {
1206
+ const I = k(c, s);
1207
+ if (!Object.is(I, o)) {
1208
+ const E = o;
1209
+ o = I, r(I, E);
1210
+ }
1164
1211
  });
1165
- return i.add(o), () => {
1166
- o(), i.delete(o);
1212
+ return i.add(u), () => {
1213
+ u(), i.delete(u);
1167
1214
  };
1168
1215
  },
1169
1216
  subscriberCount: () => i.size,
1170
- dispose: n,
1171
- [Symbol.dispose]: n,
1172
- [F]: p.Atom | p.Writable
1217
+ dispose: () => {
1218
+ i.forEach((r) => r()), i.clear();
1219
+ },
1220
+ [L]: p.Atom | p.Writable
1173
1221
  };
1174
1222
  }
1175
- var xt = (t, e) => at(t, e), Mt = (t) => (e) => at(t, e);
1223
+ var zt = (t, e) => at(t, e), kt = (t) => (e) => at(t, e);
1176
1224
  export {
1177
- U as AsyncState,
1178
- g as AtomError,
1179
- A as ComputedError,
1180
- Y as DEBUG_CONFIG,
1181
- m as EffectError,
1182
- C as SCHEDULER_CONFIG,
1183
- L as SchedulerError,
1184
- Ut as atom,
1225
+ P as AsyncState,
1226
+ y as AtomError,
1227
+ w as ComputedError,
1228
+ W as DEBUG_CONFIG,
1229
+ N as EffectError,
1230
+ g as SCHEDULER_CONFIG,
1231
+ x as SchedulerError,
1232
+ Lt as aeNextTick,
1233
+ Pt as atom,
1185
1234
  at as atomLens,
1186
- Ft as batch,
1187
- xt as composeLens,
1188
- Lt as computed,
1189
- Pt as effect,
1190
- x as getPathValue,
1191
- b as globalScheduler,
1192
- yt as isAtom,
1235
+ Ut as batch,
1236
+ zt as composeLens,
1237
+ xt as computed,
1238
+ Mt as effect,
1239
+ k as getPathValue,
1240
+ T as globalScheduler,
1241
+ wt as isAtom,
1193
1242
  Rt as isComputed,
1194
- wt as isEffect,
1195
- Mt as lensFor,
1243
+ Ft as isEffect,
1244
+ kt as lensFor,
1196
1245
  d as runtimeDebug,
1197
- _t as setDeepValue,
1198
- M as untracked
1246
+ ft as setDeepValue,
1247
+ X as untracked
1199
1248
  };
1200
1249
 
1201
1250
  //# sourceMappingURL=index.mjs.map