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