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