@but212/atom-effect 0.8.3 → 0.9.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/README.md +81 -121
- package/dist/atom-effect.min.js +1 -1
- package/dist/atom-effect.min.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +254 -283
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const Z = {
|
|
2
2
|
/** One second in milliseconds */
|
|
3
3
|
ONE_SECOND_MS: 1e3
|
|
4
|
-
},
|
|
4
|
+
}, P = {
|
|
5
5
|
IDLE: "idle",
|
|
6
6
|
PENDING: "pending",
|
|
7
7
|
RESOLVED: "resolved",
|
|
@@ -31,7 +31,7 @@ const Z = {
|
|
|
31
31
|
MAX_SIZE: 1e3,
|
|
32
32
|
/** Number of objects to pre-allocate for performance-critical paths */
|
|
33
33
|
WARMUP_SIZE: 100
|
|
34
|
-
},
|
|
34
|
+
}, D = {
|
|
35
35
|
/** Maximum effect executions per second to detect infinite loops (Legacy/Fallback) */
|
|
36
36
|
MAX_EXECUTIONS_PER_SECOND: 1e3,
|
|
37
37
|
/** Threshold for cleaning up old execution timestamps */
|
|
@@ -50,24 +50,24 @@ const Z = {
|
|
|
50
50
|
MAX_FLUSH_ITERATIONS: 1e3,
|
|
51
51
|
/** Minimum allowed value for max flush iterations */
|
|
52
52
|
MIN_FLUSH_ITERATIONS: 10
|
|
53
|
-
},
|
|
53
|
+
}, q = {
|
|
54
54
|
/** Maximum dependencies before warning about large dependency graphs */
|
|
55
55
|
MAX_DEPENDENCIES: 1e3,
|
|
56
56
|
/** Enable infinite loop detection warnings */
|
|
57
57
|
WARN_INFINITE_LOOP: !0
|
|
58
|
-
}, I = 1073741823,
|
|
59
|
-
class
|
|
58
|
+
}, I = 1073741823, l = typeof process < "u" && process.env && process.env.NODE_ENV !== "production", ee = Object.freeze([]);
|
|
59
|
+
class b extends Error {
|
|
60
60
|
/**
|
|
61
61
|
* Creates a new AtomError
|
|
62
62
|
* @param message - Error message describing what went wrong
|
|
63
63
|
* @param cause - Original error that caused this error
|
|
64
64
|
* @param recoverable - Whether the operation can be retried
|
|
65
65
|
*/
|
|
66
|
-
constructor(e, t = null,
|
|
67
|
-
super(e), this.name = "AtomError", this.cause = t, this.recoverable =
|
|
66
|
+
constructor(e, t = null, i = !0) {
|
|
67
|
+
super(e), this.name = "AtomError", this.cause = t, this.recoverable = i, this.timestamp = /* @__PURE__ */ new Date();
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
-
class N extends
|
|
70
|
+
class N extends b {
|
|
71
71
|
/**
|
|
72
72
|
* Creates a new ComputedError
|
|
73
73
|
* @param message - Error message
|
|
@@ -77,7 +77,7 @@ class N extends p {
|
|
|
77
77
|
super(e, t, !0), this.name = "ComputedError";
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
|
-
class
|
|
80
|
+
class g extends b {
|
|
81
81
|
/**
|
|
82
82
|
* Creates a new EffectError
|
|
83
83
|
* @param message - Error message
|
|
@@ -87,7 +87,7 @@ class b extends p {
|
|
|
87
87
|
super(e, t, !1), this.name = "EffectError";
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
-
class O extends
|
|
90
|
+
class O extends b {
|
|
91
91
|
/**
|
|
92
92
|
* Creates a new SchedulerError
|
|
93
93
|
* @param message - Error message
|
|
@@ -105,10 +105,6 @@ const h = {
|
|
|
105
105
|
* Error thrown when computed() receives a non-function argument.
|
|
106
106
|
*/
|
|
107
107
|
COMPUTED_MUST_BE_FUNCTION: "Computed function must be a function",
|
|
108
|
-
/**
|
|
109
|
-
* Error thrown when subscribe() receives an invalid listener.
|
|
110
|
-
*/
|
|
111
|
-
COMPUTED_SUBSCRIBER_MUST_BE_FUNCTION: "Subscriber listener must be a function or Subscriber object",
|
|
112
108
|
/**
|
|
113
109
|
* Error thrown when accessing a pending async computed without a default value.
|
|
114
110
|
*/
|
|
@@ -121,10 +117,6 @@ const h = {
|
|
|
121
117
|
* Error thrown when an asynchronous computed computation fails.
|
|
122
118
|
*/
|
|
123
119
|
COMPUTED_ASYNC_COMPUTATION_FAILED: "Async computed computation failed",
|
|
124
|
-
/**
|
|
125
|
-
* Error thrown when subscribing to a dependency fails.
|
|
126
|
-
*/
|
|
127
|
-
COMPUTED_DEPENDENCY_SUBSCRIPTION_FAILED: "Failed to subscribe to dependency",
|
|
128
120
|
// ─────────────────────────────────────────────────────────────────
|
|
129
121
|
// Atom errors
|
|
130
122
|
// ─────────────────────────────────────────────────────────────────
|
|
@@ -132,10 +124,6 @@ const h = {
|
|
|
132
124
|
* Error thrown when atom.subscribe() receives an invalid listener.
|
|
133
125
|
*/
|
|
134
126
|
ATOM_SUBSCRIBER_MUST_BE_FUNCTION: "Subscription listener must be a function or Subscriber object",
|
|
135
|
-
/**
|
|
136
|
-
* Error thrown when the atom subscriber notification process fails.
|
|
137
|
-
*/
|
|
138
|
-
ATOM_SUBSCRIBER_EXECUTION_FAILED: "Error occurred while executing atom subscribers",
|
|
139
127
|
/**
|
|
140
128
|
* Error logged when an individual subscriber throws during notification.
|
|
141
129
|
* @remarks This error is caught and logged to prevent cascading failures.
|
|
@@ -156,79 +144,59 @@ const h = {
|
|
|
156
144
|
* Error thrown when an effect's cleanup function fails.
|
|
157
145
|
*/
|
|
158
146
|
EFFECT_CLEANUP_FAILED: "Effect cleanup function execution failed",
|
|
159
|
-
// ─────────────────────────────────────────────────────────────────
|
|
160
|
-
// Debug warnings
|
|
161
|
-
// ─────────────────────────────────────────────────────────────────
|
|
162
|
-
/**
|
|
163
|
-
* Warning message for large dependency graphs.
|
|
164
|
-
*
|
|
165
|
-
* @param count - The number of dependencies detected
|
|
166
|
-
* @returns Formatted warning message with dependency count
|
|
167
|
-
*
|
|
168
|
-
* @example
|
|
169
|
-
* ```ts
|
|
170
|
-
* console.warn(ERROR_MESSAGES.LARGE_DEPENDENCY_GRAPH(150));
|
|
171
|
-
* // Output: "Large dependency graph detected: 150 dependencies"
|
|
172
|
-
* ```
|
|
173
|
-
*/
|
|
174
|
-
LARGE_DEPENDENCY_GRAPH: (i) => `Large dependency graph detected: ${i} dependencies`,
|
|
175
|
-
/**
|
|
176
|
-
* Warning logged when attempting to unsubscribe a non-existent listener.
|
|
177
|
-
*/
|
|
178
|
-
UNSUBSCRIBE_NON_EXISTENT: "Attempted to unsubscribe a non-existent listener",
|
|
179
147
|
/**
|
|
180
148
|
* Error logged when the onError callback itself throws an error.
|
|
181
149
|
* @remarks This prevents cascading failures from masking the original error.
|
|
182
150
|
*/
|
|
183
151
|
CALLBACK_ERROR_IN_ERROR_HANDLER: "Error occurred during onError callback execution"
|
|
184
|
-
}, L = /* @__PURE__ */ Symbol("debugName"), te = /* @__PURE__ */ Symbol("id"), V = /* @__PURE__ */ Symbol("type"),
|
|
185
|
-
function se(
|
|
186
|
-
return "dependencies" in
|
|
152
|
+
}, L = /* @__PURE__ */ Symbol("debugName"), te = /* @__PURE__ */ Symbol("id"), V = /* @__PURE__ */ Symbol("type"), B = /* @__PURE__ */ Symbol("noDefaultValue");
|
|
153
|
+
function se(s) {
|
|
154
|
+
return "dependencies" in s && Array.isArray(s.dependencies);
|
|
187
155
|
}
|
|
188
156
|
let j = 0;
|
|
189
|
-
function G(
|
|
190
|
-
if (
|
|
191
|
-
if (
|
|
157
|
+
function G(s, e, t) {
|
|
158
|
+
if (s._visitedEpoch !== t) {
|
|
159
|
+
if (s._visitedEpoch = t, s === e)
|
|
192
160
|
throw new N("Indirect circular dependency detected");
|
|
193
|
-
if (se(
|
|
194
|
-
const
|
|
195
|
-
for (let n = 0; n <
|
|
196
|
-
const r =
|
|
161
|
+
if (se(s)) {
|
|
162
|
+
const i = s.dependencies;
|
|
163
|
+
for (let n = 0; n < i.length; n++) {
|
|
164
|
+
const r = i[n];
|
|
197
165
|
r && G(r, e, t);
|
|
198
166
|
}
|
|
199
167
|
}
|
|
200
168
|
}
|
|
201
169
|
}
|
|
202
|
-
const
|
|
170
|
+
const E = {
|
|
203
171
|
enabled: typeof process < "u" && process.env?.NODE_ENV === "development",
|
|
204
|
-
maxDependencies:
|
|
205
|
-
warnInfiniteLoop:
|
|
206
|
-
warn(
|
|
207
|
-
this.enabled &&
|
|
172
|
+
maxDependencies: q.MAX_DEPENDENCIES,
|
|
173
|
+
warnInfiniteLoop: q.WARN_INFINITE_LOOP,
|
|
174
|
+
warn(s, e) {
|
|
175
|
+
this.enabled && s && console.warn(`[Atom Effect] ${e}`);
|
|
208
176
|
},
|
|
209
177
|
/**
|
|
210
178
|
* Checks for circular dependencies.
|
|
211
179
|
* Direct check runs always; indirect check only in dev mode.
|
|
212
180
|
* @throws {ComputedError} When circular dependency detected
|
|
213
181
|
*/
|
|
214
|
-
checkCircular(
|
|
215
|
-
if (
|
|
182
|
+
checkCircular(s, e) {
|
|
183
|
+
if (s === e)
|
|
216
184
|
throw new N("Direct circular dependency detected");
|
|
217
|
-
this.enabled && (j++, G(
|
|
185
|
+
this.enabled && (j++, G(s, e, j));
|
|
218
186
|
},
|
|
219
|
-
attachDebugInfo(
|
|
187
|
+
attachDebugInfo(s, e, t) {
|
|
220
188
|
if (!this.enabled)
|
|
221
189
|
return;
|
|
222
|
-
const
|
|
223
|
-
|
|
190
|
+
const i = s;
|
|
191
|
+
i[L] = `${e}_${t}`, i[te] = t, i[V] = e;
|
|
224
192
|
},
|
|
225
|
-
getDebugName(
|
|
226
|
-
if (
|
|
227
|
-
return
|
|
193
|
+
getDebugName(s) {
|
|
194
|
+
if (s != null && L in s)
|
|
195
|
+
return s[L];
|
|
228
196
|
},
|
|
229
|
-
getDebugType(
|
|
230
|
-
if (
|
|
231
|
-
return
|
|
197
|
+
getDebugType(s) {
|
|
198
|
+
if (s != null && V in s)
|
|
199
|
+
return s[V];
|
|
232
200
|
}
|
|
233
201
|
};
|
|
234
202
|
let ie = 1;
|
|
@@ -238,7 +206,7 @@ class Y {
|
|
|
238
206
|
this.id = ne() & I, this.flags = 0;
|
|
239
207
|
}
|
|
240
208
|
}
|
|
241
|
-
class
|
|
209
|
+
class J extends Y {
|
|
242
210
|
constructor() {
|
|
243
211
|
super(), this.version = 0, this._lastSeenEpoch = -1;
|
|
244
212
|
}
|
|
@@ -253,7 +221,7 @@ class $ extends Y {
|
|
|
253
221
|
if (typeof e == "object" && e !== null && "execute" in e)
|
|
254
222
|
return this._objectSubscribers.add(e);
|
|
255
223
|
if (typeof e != "function")
|
|
256
|
-
throw new
|
|
224
|
+
throw new b(h.ATOM_SUBSCRIBER_MUST_BE_FUNCTION);
|
|
257
225
|
return this._functionSubscribers.add(e);
|
|
258
226
|
}
|
|
259
227
|
/**
|
|
@@ -270,26 +238,26 @@ class $ extends Y {
|
|
|
270
238
|
*/
|
|
271
239
|
_notifySubscribers(e, t) {
|
|
272
240
|
this._functionSubscribers.forEachSafe(
|
|
273
|
-
(
|
|
274
|
-
(
|
|
241
|
+
(i) => i(e, t),
|
|
242
|
+
(i) => console.error(new b(h.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED, i))
|
|
275
243
|
), this._objectSubscribers.forEachSafe(
|
|
276
|
-
(
|
|
277
|
-
(
|
|
244
|
+
(i) => i.execute(),
|
|
245
|
+
(i) => console.error(new b(h.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED, i))
|
|
278
246
|
);
|
|
279
247
|
}
|
|
280
248
|
}
|
|
281
|
-
let
|
|
282
|
-
function
|
|
283
|
-
return
|
|
249
|
+
let x = 0;
|
|
250
|
+
function Q() {
|
|
251
|
+
return x = x + 1 & I || 1, x;
|
|
284
252
|
}
|
|
285
253
|
function re() {
|
|
286
|
-
return
|
|
254
|
+
return x;
|
|
287
255
|
}
|
|
288
|
-
let
|
|
256
|
+
let U = 0, w = 0, F = !1;
|
|
289
257
|
function z() {
|
|
290
|
-
return F ? (
|
|
258
|
+
return F ? (l && console.warn(
|
|
291
259
|
"Warning: startFlush() called during flush - ignored to prevent infinite loop detection bypass"
|
|
292
|
-
), !1) : (F = !0,
|
|
260
|
+
), !1) : (F = !0, U = U + 1 & I, w = 0, !0);
|
|
293
261
|
}
|
|
294
262
|
function X() {
|
|
295
263
|
F = !1;
|
|
@@ -299,7 +267,7 @@ function oe() {
|
|
|
299
267
|
}
|
|
300
268
|
class ce {
|
|
301
269
|
constructor() {
|
|
302
|
-
this.queueA = [], this.queueB = [], this.queue = this.queueA, this.queueSize = 0, this._epoch = 0, this.isProcessing = !1, this.isBatching = !1, this.batchDepth = 0, this.batchQueue = [], this.batchQueueSize = 0, this.isFlushingSync = !1, this.maxFlushIterations =
|
|
270
|
+
this.queueA = [], this.queueB = [], this.queue = this.queueA, this.queueSize = 0, this._epoch = 0, this.isProcessing = !1, this.isBatching = !1, this.batchDepth = 0, this.batchQueue = [], this.batchQueueSize = 0, this.isFlushingSync = !1, this.maxFlushIterations = D.MAX_FLUSH_ITERATIONS;
|
|
303
271
|
}
|
|
304
272
|
get phase() {
|
|
305
273
|
return this.isProcessing || this.isFlushingSync ? 2 : this.isBatching ? 1 : 0;
|
|
@@ -320,8 +288,8 @@ class ce {
|
|
|
320
288
|
this.isProcessing = !0;
|
|
321
289
|
const e = this.queue, t = this.queueSize;
|
|
322
290
|
this.queue = this.queue === this.queueA ? this.queueB : this.queueA, this.queueSize = 0, this._epoch++, queueMicrotask(() => {
|
|
323
|
-
const
|
|
324
|
-
this._processJobs(e, t), this.isProcessing = !1,
|
|
291
|
+
const i = z();
|
|
292
|
+
this._processJobs(e, t), this.isProcessing = !1, i && X(), this.queueSize > 0 && !this.isBatching && this.flush();
|
|
325
293
|
});
|
|
326
294
|
}
|
|
327
295
|
flushSync() {
|
|
@@ -364,9 +332,9 @@ class ce {
|
|
|
364
332
|
), this.queueSize = 0, this.queue.length = 0, this.batchQueueSize = 0;
|
|
365
333
|
}
|
|
366
334
|
_processJobs(e, t) {
|
|
367
|
-
for (let
|
|
335
|
+
for (let i = 0; i < t; i++)
|
|
368
336
|
try {
|
|
369
|
-
e[
|
|
337
|
+
e[i]?.();
|
|
370
338
|
} catch (n) {
|
|
371
339
|
console.error(
|
|
372
340
|
new O("Error occurred during scheduler execution", n)
|
|
@@ -390,29 +358,29 @@ class ce {
|
|
|
390
358
|
* @param max - Maximum iterations count.
|
|
391
359
|
*/
|
|
392
360
|
setMaxFlushIterations(e) {
|
|
393
|
-
if (e <
|
|
361
|
+
if (e < D.MIN_FLUSH_ITERATIONS)
|
|
394
362
|
throw new O(
|
|
395
|
-
`Max flush iterations must be at least ${
|
|
363
|
+
`Max flush iterations must be at least ${D.MIN_FLUSH_ITERATIONS}`
|
|
396
364
|
);
|
|
397
365
|
this.maxFlushIterations = e;
|
|
398
366
|
}
|
|
399
367
|
}
|
|
400
368
|
const T = new ce();
|
|
401
|
-
function Se(
|
|
402
|
-
if (typeof
|
|
403
|
-
throw new
|
|
369
|
+
function Se(s) {
|
|
370
|
+
if (typeof s != "function")
|
|
371
|
+
throw new b("Batch callback must be a function");
|
|
404
372
|
T.startBatch();
|
|
405
373
|
try {
|
|
406
|
-
return
|
|
374
|
+
return s();
|
|
407
375
|
} finally {
|
|
408
376
|
T.endBatch();
|
|
409
377
|
}
|
|
410
378
|
}
|
|
411
|
-
const
|
|
379
|
+
const m = {
|
|
412
380
|
current: null,
|
|
413
|
-
run(
|
|
381
|
+
run(s, e) {
|
|
414
382
|
const t = this.current;
|
|
415
|
-
this.current =
|
|
383
|
+
this.current = s;
|
|
416
384
|
try {
|
|
417
385
|
return e();
|
|
418
386
|
} finally {
|
|
@@ -423,18 +391,18 @@ const y = {
|
|
|
423
391
|
return this.current;
|
|
424
392
|
}
|
|
425
393
|
};
|
|
426
|
-
function he(
|
|
427
|
-
if (typeof
|
|
428
|
-
throw new
|
|
429
|
-
const e =
|
|
430
|
-
|
|
394
|
+
function he(s) {
|
|
395
|
+
if (typeof s != "function")
|
|
396
|
+
throw new b("Untracked callback must be a function");
|
|
397
|
+
const e = m.current;
|
|
398
|
+
m.current = null;
|
|
431
399
|
try {
|
|
432
|
-
return
|
|
400
|
+
return s();
|
|
433
401
|
} finally {
|
|
434
|
-
|
|
402
|
+
m.current = e;
|
|
435
403
|
}
|
|
436
404
|
}
|
|
437
|
-
class
|
|
405
|
+
class v {
|
|
438
406
|
constructor() {
|
|
439
407
|
this.subscribers = null;
|
|
440
408
|
}
|
|
@@ -456,8 +424,8 @@ class P {
|
|
|
456
424
|
const t = this.subscribers.indexOf(e);
|
|
457
425
|
if (t === -1)
|
|
458
426
|
return !1;
|
|
459
|
-
const
|
|
460
|
-
return t !==
|
|
427
|
+
const i = this.subscribers.length - 1;
|
|
428
|
+
return t !== i && (this.subscribers[t] = this.subscribers[i]), this.subscribers.pop(), !0;
|
|
461
429
|
}
|
|
462
430
|
has(e) {
|
|
463
431
|
return this.subscribers ? this.subscribers.indexOf(e) !== -1 : !1;
|
|
@@ -470,9 +438,9 @@ class P {
|
|
|
470
438
|
/** Iterates with error handling to prevent one failure from breaking the chain */
|
|
471
439
|
forEachSafe(e, t) {
|
|
472
440
|
if (this.subscribers)
|
|
473
|
-
for (let
|
|
441
|
+
for (let i = 0; i < this.subscribers.length; i++)
|
|
474
442
|
try {
|
|
475
|
-
e(this.subscribers[
|
|
443
|
+
e(this.subscribers[i], i);
|
|
476
444
|
} catch (n) {
|
|
477
445
|
t ? t(n) : console.error("[SubscriberManager] Error in subscriber callback:", n);
|
|
478
446
|
}
|
|
@@ -490,9 +458,9 @@ class P {
|
|
|
490
458
|
return this.subscribers ? [...this.subscribers] : [];
|
|
491
459
|
}
|
|
492
460
|
}
|
|
493
|
-
class
|
|
461
|
+
class k {
|
|
494
462
|
constructor() {
|
|
495
|
-
this.pool = [], this.maxPoolSize = 50, this.maxReusableCapacity = 256, this.stats =
|
|
463
|
+
this.pool = [], this.maxPoolSize = 50, this.maxReusableCapacity = 256, this.stats = l ? {
|
|
496
464
|
acquired: 0,
|
|
497
465
|
released: 0,
|
|
498
466
|
rejected: { frozen: 0, tooLarge: 0, poolFull: 0 }
|
|
@@ -500,7 +468,7 @@ class B {
|
|
|
500
468
|
}
|
|
501
469
|
/** Acquires an array from the pool or creates a new one if the pool is empty. */
|
|
502
470
|
acquire() {
|
|
503
|
-
return
|
|
471
|
+
return l && this.stats && this.stats.acquired++, this.pool.pop() ?? [];
|
|
504
472
|
}
|
|
505
473
|
/**
|
|
506
474
|
* Releases an array back to the pool.
|
|
@@ -511,102 +479,102 @@ class B {
|
|
|
511
479
|
release(e, t) {
|
|
512
480
|
if (!(t && e === t)) {
|
|
513
481
|
if (Object.isFrozen(e)) {
|
|
514
|
-
|
|
482
|
+
l && this.stats && this.stats.rejected.frozen++;
|
|
515
483
|
return;
|
|
516
484
|
}
|
|
517
485
|
if (e.length > this.maxReusableCapacity) {
|
|
518
|
-
|
|
486
|
+
l && this.stats && this.stats.rejected.tooLarge++;
|
|
519
487
|
return;
|
|
520
488
|
}
|
|
521
489
|
if (this.pool.length >= this.maxPoolSize) {
|
|
522
|
-
|
|
490
|
+
l && this.stats && this.stats.rejected.poolFull++;
|
|
523
491
|
return;
|
|
524
492
|
}
|
|
525
|
-
e.length = 0, this.pool.push(e),
|
|
493
|
+
e.length = 0, this.pool.push(e), l && this.stats && this.stats.released++;
|
|
526
494
|
}
|
|
527
495
|
}
|
|
528
496
|
/** Returns current stats for the pool (dev mode only). */
|
|
529
497
|
getStats() {
|
|
530
|
-
if (!
|
|
531
|
-
const { acquired: e, released: t, rejected:
|
|
498
|
+
if (!l || !this.stats) return null;
|
|
499
|
+
const { acquired: e, released: t, rejected: i } = this.stats, n = i.frozen + i.tooLarge + i.poolFull;
|
|
532
500
|
return {
|
|
533
501
|
acquired: e,
|
|
534
502
|
released: t,
|
|
535
|
-
rejected:
|
|
503
|
+
rejected: i,
|
|
536
504
|
leaked: e - t - n,
|
|
537
505
|
poolSize: this.pool.length
|
|
538
506
|
};
|
|
539
507
|
}
|
|
540
508
|
/** Resets the pool and its stats. */
|
|
541
509
|
reset() {
|
|
542
|
-
this.pool.length = 0,
|
|
510
|
+
this.pool.length = 0, l && this.stats && (this.stats.acquired = 0, this.stats.released = 0, this.stats.rejected = { frozen: 0, tooLarge: 0, poolFull: 0 });
|
|
543
511
|
}
|
|
544
512
|
}
|
|
545
|
-
const u = Object.freeze([]),
|
|
546
|
-
function ue(
|
|
547
|
-
return
|
|
513
|
+
const u = Object.freeze([]), f = Object.freeze([]), _ = Object.freeze([]), S = new k(), y = new k(), d = new k();
|
|
514
|
+
function ue(s) {
|
|
515
|
+
return s !== null && typeof s == "object" && "value" in s && "subscribe" in s && typeof s.subscribe == "function";
|
|
548
516
|
}
|
|
549
|
-
function ge(
|
|
550
|
-
if (
|
|
551
|
-
const e =
|
|
517
|
+
function ge(s) {
|
|
518
|
+
if (E.enabled && (s == null || typeof s == "object")) {
|
|
519
|
+
const e = E.getDebugType(s);
|
|
552
520
|
if (e)
|
|
553
521
|
return e === "computed";
|
|
554
522
|
}
|
|
555
|
-
return ue(
|
|
523
|
+
return ue(s) && "invalidate" in s && typeof s.invalidate == "function";
|
|
556
524
|
}
|
|
557
|
-
function De(
|
|
558
|
-
return
|
|
525
|
+
function De(s) {
|
|
526
|
+
return s !== null && typeof s == "object" && "dispose" in s && "run" in s && typeof s.dispose == "function" && typeof s.run == "function";
|
|
559
527
|
}
|
|
560
|
-
function
|
|
561
|
-
return
|
|
528
|
+
function $(s) {
|
|
529
|
+
return s != null && typeof s.then == "function";
|
|
562
530
|
}
|
|
563
|
-
function _e(
|
|
564
|
-
return typeof
|
|
531
|
+
function _e(s) {
|
|
532
|
+
return typeof s == "object" && s !== null;
|
|
565
533
|
}
|
|
566
|
-
function ae(
|
|
567
|
-
return (typeof
|
|
534
|
+
function ae(s) {
|
|
535
|
+
return (typeof s == "object" || typeof s == "function") && s !== null && typeof s.addDependency == "function";
|
|
568
536
|
}
|
|
569
|
-
function le(
|
|
570
|
-
return typeof
|
|
537
|
+
function le(s) {
|
|
538
|
+
return typeof s == "function" && typeof s.addDependency != "function";
|
|
571
539
|
}
|
|
572
|
-
function fe(
|
|
573
|
-
return _e(
|
|
540
|
+
function fe(s) {
|
|
541
|
+
return _e(s) && typeof s.execute == "function";
|
|
574
542
|
}
|
|
575
|
-
function
|
|
543
|
+
function H(s, e, t, i) {
|
|
576
544
|
if (e) {
|
|
577
545
|
if (ae(e)) {
|
|
578
|
-
e.addDependency(
|
|
546
|
+
e.addDependency(s);
|
|
579
547
|
return;
|
|
580
548
|
}
|
|
581
549
|
if (le(e)) {
|
|
582
550
|
t.add(e);
|
|
583
551
|
return;
|
|
584
552
|
}
|
|
585
|
-
fe(e) &&
|
|
553
|
+
fe(e) && i.add(e);
|
|
586
554
|
}
|
|
587
555
|
}
|
|
588
|
-
function de(
|
|
589
|
-
if (e !== u && t !==
|
|
556
|
+
function de(s, e, t, i) {
|
|
557
|
+
if (e !== u && t !== f)
|
|
590
558
|
for (let r = 0; r < e.length; r++) {
|
|
591
559
|
const o = e[r];
|
|
592
560
|
o && (o._tempUnsub = t[r]);
|
|
593
561
|
}
|
|
594
|
-
const n =
|
|
595
|
-
n.length =
|
|
596
|
-
for (let r = 0; r <
|
|
597
|
-
const o =
|
|
598
|
-
o && (o._tempUnsub ? (n[r] = o._tempUnsub, o._tempUnsub = void 0) : (
|
|
562
|
+
const n = y.acquire();
|
|
563
|
+
n.length = s.length;
|
|
564
|
+
for (let r = 0; r < s.length; r++) {
|
|
565
|
+
const o = s[r];
|
|
566
|
+
o && (o._tempUnsub ? (n[r] = o._tempUnsub, o._tempUnsub = void 0) : (E.checkCircular(o, i), n[r] = o.subscribe(i)));
|
|
599
567
|
}
|
|
600
568
|
if (e !== u)
|
|
601
569
|
for (let r = 0; r < e.length; r++) {
|
|
602
570
|
const o = e[r];
|
|
603
571
|
o?._tempUnsub && (o._tempUnsub(), o._tempUnsub = void 0);
|
|
604
572
|
}
|
|
605
|
-
return t !==
|
|
573
|
+
return t !== f && y.release(t), n;
|
|
606
574
|
}
|
|
607
|
-
class Ee extends
|
|
575
|
+
class Ee extends J {
|
|
608
576
|
constructor(e, t) {
|
|
609
|
-
super(), this._isNotificationScheduled = !1, this._value = e, this._functionSubscribersStore = new
|
|
577
|
+
super(), this._isNotificationScheduled = !1, this._value = e, this._functionSubscribersStore = new v(), this._objectSubscribersStore = new v(), this._sync = t, this._notifyTask = this._flushNotifications.bind(this), E.attachDebugInfo(this, "atom", this.id);
|
|
610
578
|
}
|
|
611
579
|
/** Gets the manager for function-based subscribers. */
|
|
612
580
|
get _functionSubscribers() {
|
|
@@ -620,7 +588,7 @@ class Ee extends $ {
|
|
|
620
588
|
* Returns the current value and registers the atom as a dependency in the current tracking context.
|
|
621
589
|
*/
|
|
622
590
|
get value() {
|
|
623
|
-
const e =
|
|
591
|
+
const e = m.getCurrent();
|
|
624
592
|
return e && this._track(e), this._value;
|
|
625
593
|
}
|
|
626
594
|
/**
|
|
@@ -633,7 +601,7 @@ class Ee extends $ {
|
|
|
633
601
|
this.version = this.version + 1 & I, this._value = e, !(!this._functionSubscribersStore.hasSubscribers && !this._objectSubscribersStore.hasSubscribers) && this._scheduleNotification(t);
|
|
634
602
|
}
|
|
635
603
|
_track(e) {
|
|
636
|
-
|
|
604
|
+
H(this, e, this._functionSubscribersStore, this._objectSubscribersStore);
|
|
637
605
|
}
|
|
638
606
|
_scheduleNotification(e) {
|
|
639
607
|
this._isNotificationScheduled || (this._pendingOldValue = e, this._isNotificationScheduled = !0), this._sync && !T.isBatching ? this._flushNotifications() : T.schedule(this._notifyTask);
|
|
@@ -656,41 +624,41 @@ class Ee extends $ {
|
|
|
656
624
|
this._functionSubscribersStore.clear(), this._objectSubscribersStore.clear(), this._value = void 0;
|
|
657
625
|
}
|
|
658
626
|
}
|
|
659
|
-
function
|
|
660
|
-
return new Ee(
|
|
627
|
+
function ye(s, e = {}) {
|
|
628
|
+
return new Ee(s, e.sync ?? !1);
|
|
661
629
|
}
|
|
662
|
-
function R(
|
|
663
|
-
if (
|
|
664
|
-
return new e(`Type error (${t}): ${
|
|
665
|
-
if (
|
|
666
|
-
return new e(`Reference error (${t}): ${
|
|
667
|
-
if (
|
|
668
|
-
return
|
|
669
|
-
const
|
|
670
|
-
return new e(`Unexpected error (${t}): ${
|
|
630
|
+
function R(s, e, t) {
|
|
631
|
+
if (s instanceof TypeError)
|
|
632
|
+
return new e(`Type error (${t}): ${s.message}`, s);
|
|
633
|
+
if (s instanceof ReferenceError)
|
|
634
|
+
return new e(`Reference error (${t}): ${s.message}`, s);
|
|
635
|
+
if (s instanceof b)
|
|
636
|
+
return s;
|
|
637
|
+
const i = s instanceof Error ? s.message : String(s), n = s instanceof Error ? s : null;
|
|
638
|
+
return new e(`Unexpected error (${t}): ${i}`, n);
|
|
671
639
|
}
|
|
672
|
-
const W = c.RESOLVED | c.PENDING | c.REJECTED, M = Array(W + 1).fill(
|
|
673
|
-
M[c.RESOLVED] =
|
|
674
|
-
M[c.PENDING] =
|
|
675
|
-
M[c.REJECTED] =
|
|
676
|
-
class K extends
|
|
640
|
+
const W = c.RESOLVED | c.PENDING | c.REJECTED, M = Array(W + 1).fill(P.IDLE);
|
|
641
|
+
M[c.RESOLVED] = P.RESOLVED;
|
|
642
|
+
M[c.PENDING] = P.PENDING;
|
|
643
|
+
M[c.REJECTED] = P.REJECTED;
|
|
644
|
+
class K extends J {
|
|
677
645
|
constructor(e, t = {}) {
|
|
678
646
|
if (typeof e != "function")
|
|
679
647
|
throw new N(h.COMPUTED_MUST_BE_FUNCTION);
|
|
680
|
-
if (super(), this._cachedErrors = null, this._errorCacheEpoch = -1, this._value = void 0, this.flags = c.DIRTY | c.IDLE, this._error = null, this._promiseId = 0, this._equal = t.equal ?? Object.is, this._fn = e, this._defaultValue = "defaultValue" in t ? t.defaultValue :
|
|
648
|
+
if (super(), this._cachedErrors = null, this._errorCacheEpoch = -1, this._value = void 0, this.flags = c.DIRTY | c.IDLE, this._error = null, this._promiseId = 0, this._equal = t.equal ?? Object.is, this._fn = e, this._defaultValue = "defaultValue" in t ? t.defaultValue : B, this._hasDefaultValue = this._defaultValue !== B, this._onError = t.onError ?? null, this.MAX_PROMISE_ID = Number.MAX_SAFE_INTEGER - 1, this._functionSubscribersStore = new v(), this._objectSubscribersStore = new v(), this._dependencies = u, this._dependencyVersions = _, this._unsubscribes = f, this._notifyJob = () => {
|
|
681
649
|
this._functionSubscribersStore.forEachSafe(
|
|
682
|
-
(
|
|
683
|
-
(
|
|
650
|
+
(i) => i(),
|
|
651
|
+
(i) => console.error(i)
|
|
684
652
|
), this._objectSubscribersStore.forEachSafe(
|
|
685
|
-
(
|
|
686
|
-
(
|
|
653
|
+
(i) => i.execute(),
|
|
654
|
+
(i) => console.error(i)
|
|
687
655
|
);
|
|
688
656
|
}, this._trackable = Object.assign(() => this._markDirty(), {
|
|
689
|
-
addDependency: (
|
|
657
|
+
addDependency: (i) => {
|
|
690
658
|
}
|
|
691
|
-
}),
|
|
692
|
-
const
|
|
693
|
-
|
|
659
|
+
}), E.attachDebugInfo(this, "computed", this.id), E.enabled) {
|
|
660
|
+
const i = this;
|
|
661
|
+
i.subscriberCount = () => this._functionSubscribersStore.size + this._objectSubscribersStore.size, i.isDirty = () => this._isDirty(), i.dependencies = this._dependencies, i.stateFlags = this._getFlagsAsString();
|
|
694
662
|
}
|
|
695
663
|
if (t.lazy === !1)
|
|
696
664
|
try {
|
|
@@ -734,13 +702,13 @@ class K extends $ {
|
|
|
734
702
|
return this._cachedErrors;
|
|
735
703
|
const t = /* @__PURE__ */ new Set();
|
|
736
704
|
this._error && t.add(this._error);
|
|
737
|
-
for (let
|
|
738
|
-
const n = this._dependencies[
|
|
705
|
+
for (let i = 0; i < this._dependencies.length; i++) {
|
|
706
|
+
const n = this._dependencies[i];
|
|
739
707
|
if (n && "errors" in n) {
|
|
740
708
|
const r = n.errors;
|
|
741
709
|
for (let o = 0; o < r.length; o++) {
|
|
742
|
-
const
|
|
743
|
-
|
|
710
|
+
const a = r[o];
|
|
711
|
+
a && t.add(a);
|
|
744
712
|
}
|
|
745
713
|
}
|
|
746
714
|
}
|
|
@@ -756,17 +724,17 @@ class K extends $ {
|
|
|
756
724
|
return this._registerTracking(), this._isResolved();
|
|
757
725
|
}
|
|
758
726
|
invalidate() {
|
|
759
|
-
this._markDirty(), this._dependencyVersions !== _ && (
|
|
727
|
+
this._markDirty(), this._dependencyVersions !== _ && (d.release(this._dependencyVersions), this._dependencyVersions = _), this._errorCacheEpoch = -1, this._cachedErrors = null;
|
|
760
728
|
}
|
|
761
729
|
dispose() {
|
|
762
|
-
if (this._unsubscribes !==
|
|
730
|
+
if (this._unsubscribes !== f) {
|
|
763
731
|
for (let e = 0; e < this._unsubscribes.length; e++) {
|
|
764
732
|
const t = this._unsubscribes[e];
|
|
765
733
|
t && t();
|
|
766
734
|
}
|
|
767
|
-
|
|
735
|
+
y.release(this._unsubscribes), this._unsubscribes = f;
|
|
768
736
|
}
|
|
769
|
-
this._dependencies !== u && (S.release(this._dependencies), this._dependencies = u), this._dependencyVersions !== _ && (
|
|
737
|
+
this._dependencies !== u && (S.release(this._dependencies), this._dependencies = u), this._dependencyVersions !== _ && (d.release(this._dependencyVersions), this._dependencyVersions = _), this._functionSubscribersStore.clear(), this._objectSubscribersStore.clear(), this.flags = c.DIRTY | c.IDLE, this._error = null, this._value = void 0, this._promiseId = (this._promiseId + 1) % this.MAX_PROMISE_ID, this._cachedErrors = null, this._errorCacheEpoch = -1;
|
|
770
738
|
}
|
|
771
739
|
// State flag operations
|
|
772
740
|
_isDirty() {
|
|
@@ -825,32 +793,32 @@ class K extends $ {
|
|
|
825
793
|
const e = this._prepareComputationContext();
|
|
826
794
|
let t = !1;
|
|
827
795
|
try {
|
|
828
|
-
const
|
|
829
|
-
this._commitDependencies(e), t = !0,
|
|
830
|
-
} catch (
|
|
796
|
+
const i = m.run(this._trackable, this._fn);
|
|
797
|
+
this._commitDependencies(e), t = !0, $(i) ? this._handleAsyncComputation(i) : this._handleSyncResult(i);
|
|
798
|
+
} catch (i) {
|
|
831
799
|
if (!t)
|
|
832
800
|
try {
|
|
833
801
|
this._commitDependencies(e), t = !0;
|
|
834
802
|
} catch (n) {
|
|
835
803
|
this._handleComputationError(n);
|
|
836
804
|
}
|
|
837
|
-
this._handleComputationError(
|
|
805
|
+
this._handleComputationError(i);
|
|
838
806
|
} finally {
|
|
839
807
|
this._cleanupContext(e, t), this._setRecomputing(!1);
|
|
840
808
|
}
|
|
841
809
|
}
|
|
842
810
|
_prepareComputationContext() {
|
|
843
|
-
const e = this._dependencies, t = this._dependencyVersions,
|
|
844
|
-
|
|
811
|
+
const e = this._dependencies, t = this._dependencyVersions, i = S.acquire(), n = d.acquire(), r = Q(), o = { depCount: 0 }, a = (p) => {
|
|
812
|
+
p._lastSeenEpoch !== r && (p._lastSeenEpoch = r, o.depCount < i.length ? (i[o.depCount] = p, n[o.depCount] = p.version) : (i.push(p), n.push(p.version)), o.depCount++);
|
|
845
813
|
}, C = this._trackable.addDependency;
|
|
846
|
-
return this._trackable.addDependency =
|
|
814
|
+
return this._trackable.addDependency = a, { prevDeps: e, prevVersions: t, nextDeps: i, nextVersions: n, originalAdd: C, state: o };
|
|
847
815
|
}
|
|
848
816
|
_commitDependencies(e) {
|
|
849
|
-
const { nextDeps: t, nextVersions:
|
|
850
|
-
t.length = n.depCount,
|
|
817
|
+
const { nextDeps: t, nextVersions: i, state: n, prevDeps: r } = e;
|
|
818
|
+
t.length = n.depCount, i.length = n.depCount, this._unsubscribes = de(t, r, this._unsubscribes, this), this._dependencies = t, this._dependencyVersions = i;
|
|
851
819
|
}
|
|
852
820
|
_cleanupContext(e, t) {
|
|
853
|
-
this._trackable.addDependency = e.originalAdd, t ? (e.prevDeps !== u && S.release(e.prevDeps), e.prevVersions !== _ &&
|
|
821
|
+
this._trackable.addDependency = e.originalAdd, t ? (e.prevDeps !== u && S.release(e.prevDeps), e.prevVersions !== _ && d.release(e.prevVersions)) : (S.release(e.nextDeps), d.release(e.nextVersions));
|
|
854
822
|
}
|
|
855
823
|
_handleSyncResult(e) {
|
|
856
824
|
const t = !this._isResolved() || !this._equal(this._value, e);
|
|
@@ -859,10 +827,10 @@ class K extends $ {
|
|
|
859
827
|
_handleAsyncComputation(e) {
|
|
860
828
|
this._setPending(), this._clearDirty(), this._notifyJob(), this._promiseId = this._promiseId >= this.MAX_PROMISE_ID ? 1 : this._promiseId + 1;
|
|
861
829
|
const t = this._promiseId;
|
|
862
|
-
e.then((
|
|
863
|
-
t === this._promiseId && this._handleAsyncResolution(
|
|
864
|
-
}).catch((
|
|
865
|
-
t === this._promiseId && this._handleAsyncRejection(
|
|
830
|
+
e.then((i) => {
|
|
831
|
+
t === this._promiseId && this._handleAsyncResolution(i);
|
|
832
|
+
}).catch((i) => {
|
|
833
|
+
t === this._promiseId && this._handleAsyncRejection(i);
|
|
866
834
|
});
|
|
867
835
|
}
|
|
868
836
|
_handleAsyncResolution(e) {
|
|
@@ -870,8 +838,8 @@ class K extends $ {
|
|
|
870
838
|
this.version = this.version + Number(t) & I, this._value = e, this._clearDirty(), this._setResolved(), this._error = null, this._setRecomputing(!1), this._cachedErrors = null, this._errorCacheEpoch = -1, this._notifyJob();
|
|
871
839
|
}
|
|
872
840
|
_handleAsyncRejection(e) {
|
|
873
|
-
const t = R(e, N, h.COMPUTED_ASYNC_COMPUTATION_FAILED),
|
|
874
|
-
if (this.version = this.version + Number(
|
|
841
|
+
const t = R(e, N, h.COMPUTED_ASYNC_COMPUTATION_FAILED), i = !this._isRejected();
|
|
842
|
+
if (this.version = this.version + Number(i) & I, this._error = t, this._setRejected(), this._clearDirty(), this._setRecomputing(!1), this._onError)
|
|
875
843
|
try {
|
|
876
844
|
this._onError(t);
|
|
877
845
|
} catch (n) {
|
|
@@ -884,8 +852,8 @@ class K extends $ {
|
|
|
884
852
|
if (this._error = t, this._setRejected(), this._clearDirty(), this._setRecomputing(!1), this._onError)
|
|
885
853
|
try {
|
|
886
854
|
this._onError(t);
|
|
887
|
-
} catch (
|
|
888
|
-
console.error(h.CALLBACK_ERROR_IN_ERROR_HANDLER,
|
|
855
|
+
} catch (i) {
|
|
856
|
+
console.error(h.CALLBACK_ERROR_IN_ERROR_HANDLER, i);
|
|
889
857
|
}
|
|
890
858
|
throw t;
|
|
891
859
|
}
|
|
@@ -907,17 +875,17 @@ class K extends $ {
|
|
|
907
875
|
this._isRecomputing() || this._isDirty() || (this._setDirty(), this._notifyJob());
|
|
908
876
|
}
|
|
909
877
|
_registerTracking() {
|
|
910
|
-
|
|
878
|
+
H(
|
|
911
879
|
this,
|
|
912
|
-
|
|
880
|
+
m.getCurrent(),
|
|
913
881
|
this._functionSubscribersStore,
|
|
914
882
|
this._objectSubscribersStore
|
|
915
883
|
);
|
|
916
884
|
}
|
|
917
885
|
}
|
|
918
886
|
Object.freeze(K.prototype);
|
|
919
|
-
function
|
|
920
|
-
return new K(
|
|
887
|
+
function Ie(s, e = {}) {
|
|
888
|
+
return new K(s, e);
|
|
921
889
|
}
|
|
922
890
|
class pe extends Y {
|
|
923
891
|
/**
|
|
@@ -928,56 +896,58 @@ class pe extends Y {
|
|
|
928
896
|
constructor(e, t = {}) {
|
|
929
897
|
super(), this.run = () => {
|
|
930
898
|
if (this.isDisposed)
|
|
931
|
-
throw new
|
|
932
|
-
this._dependencyVersions !== _ && (
|
|
899
|
+
throw new g(h.EFFECT_MUST_BE_FUNCTION);
|
|
900
|
+
this._dependencyVersions !== _ && (d.release(this._dependencyVersions), this._dependencyVersions = _), this.execute();
|
|
933
901
|
}, this.dispose = () => {
|
|
934
902
|
if (!this.isDisposed) {
|
|
935
|
-
if (this._setDisposed(), this._safeCleanup(), this._unsubscribes !==
|
|
936
|
-
for (let
|
|
937
|
-
const
|
|
938
|
-
|
|
903
|
+
if (this._setDisposed(), this._safeCleanup(), this._unsubscribes !== f) {
|
|
904
|
+
for (let n = 0; n < this._unsubscribes.length; n++) {
|
|
905
|
+
const r = this._unsubscribes[n];
|
|
906
|
+
r && r();
|
|
939
907
|
}
|
|
940
|
-
|
|
908
|
+
y.release(this._unsubscribes), this._unsubscribes = f;
|
|
941
909
|
}
|
|
942
|
-
this._dependencies !== u && (S.release(this._dependencies), this._dependencies = u), this._dependencyVersions !== _ && (
|
|
910
|
+
this._dependencies !== u && (S.release(this._dependencies), this._dependencies = u), this._dependencyVersions !== _ && (d.release(this._dependencyVersions), this._dependencyVersions = _);
|
|
943
911
|
}
|
|
944
|
-
}, this.addDependency = (
|
|
912
|
+
}, this.addDependency = (n) => {
|
|
945
913
|
if (this.isExecuting && this._nextDeps && this._nextUnsubs && this._nextVersions) {
|
|
946
|
-
const
|
|
947
|
-
if (
|
|
948
|
-
|
|
914
|
+
const r = this._currentEpoch;
|
|
915
|
+
if (n._lastSeenEpoch === r) return;
|
|
916
|
+
n._lastSeenEpoch = r, this._nextDeps.push(n), this._nextVersions.push(n.version), n._tempUnsub ? (this._nextUnsubs.push(n._tempUnsub), n._tempUnsub = void 0) : this._subscribeTo(n);
|
|
949
917
|
}
|
|
950
918
|
}, this.execute = () => {
|
|
951
919
|
if (this.isDisposed || this.isExecuting || !this._shouldExecute()) return;
|
|
952
920
|
this._checkInfiniteLoop(), this._setExecuting(!0), this._safeCleanup();
|
|
953
|
-
const
|
|
954
|
-
let
|
|
921
|
+
const n = this._prepareEffectContext();
|
|
922
|
+
let r = !1;
|
|
955
923
|
try {
|
|
956
|
-
const
|
|
957
|
-
this._commitEffect(
|
|
958
|
-
!this.isDisposed && typeof
|
|
959
|
-
}).catch((
|
|
960
|
-
|
|
961
|
-
}) : this._cleanup = typeof
|
|
962
|
-
} catch (
|
|
963
|
-
|
|
924
|
+
const o = m.run(this, this._fn);
|
|
925
|
+
this._commitEffect(n), r = !0, this._checkLoopWarnings(), $(o) ? o.then((a) => {
|
|
926
|
+
!this.isDisposed && typeof a == "function" && (this._cleanup = a);
|
|
927
|
+
}).catch((a) => {
|
|
928
|
+
this._handleExecutionError(a);
|
|
929
|
+
}) : this._cleanup = typeof o == "function" ? o : null;
|
|
930
|
+
} catch (o) {
|
|
931
|
+
r = !0, this._handleExecutionError(o), this._cleanup = null;
|
|
964
932
|
} finally {
|
|
965
|
-
this._cleanupEffect(
|
|
933
|
+
this._cleanupEffect(n, r), this._setExecuting(!1);
|
|
966
934
|
}
|
|
967
|
-
}, this._currentEpoch = -1, this._lastFlushEpoch = -1, this._executionsInEpoch = 0, this._fn = e, this._sync = t.sync ?? !1, this._maxExecutions = t.maxExecutionsPerSecond ??
|
|
935
|
+
}, this._currentEpoch = -1, this._lastFlushEpoch = -1, this._executionsInEpoch = 0, this._fn = e, this._sync = t.sync ?? !1, this._maxExecutions = t.maxExecutionsPerSecond ?? D.MAX_EXECUTIONS_PER_SECOND, this._maxExecutionsPerFlush = t.maxExecutionsPerFlush ?? D.MAX_EXECUTIONS_PER_EFFECT, this._trackModifications = t.trackModifications ?? !1, this._cleanup = null, this._dependencies = u, this._dependencyVersions = _, this._unsubscribes = f, this._nextDeps = null, this._nextVersions = null, this._nextUnsubs = null, this._onError = t.onError ?? null, this._historyPtr = 0;
|
|
936
|
+
const i = Number.isFinite(this._maxExecutions);
|
|
937
|
+
this._historyCapacity = i ? Math.min(this._maxExecutions + 1, D.MAX_EXECUTIONS_PER_SECOND + 1) : 0, this._history = l && i && this._historyCapacity > 0 ? new Array(this._historyCapacity).fill(0) : null, this._executionCount = 0, E.attachDebugInfo(this, "effect", this.id);
|
|
968
938
|
}
|
|
969
939
|
/**
|
|
970
940
|
* Prepares the execution context by acquiring pools and setting up epoch.
|
|
971
941
|
* @returns The prepared EffectContext.
|
|
972
942
|
*/
|
|
973
943
|
_prepareEffectContext() {
|
|
974
|
-
const e = this._dependencies, t = this._dependencyVersions,
|
|
975
|
-
if (e !== u &&
|
|
944
|
+
const e = this._dependencies, t = this._dependencyVersions, i = this._unsubscribes, n = S.acquire(), r = d.acquire(), o = y.acquire(), a = Q();
|
|
945
|
+
if (e !== u && i !== f)
|
|
976
946
|
for (let C = 0; C < e.length; C++) {
|
|
977
|
-
const
|
|
978
|
-
|
|
947
|
+
const p = e[C];
|
|
948
|
+
p && (p._tempUnsub = i[C]);
|
|
979
949
|
}
|
|
980
|
-
return this._nextDeps = n, this._nextVersions = r, this._nextUnsubs = o, this._currentEpoch =
|
|
950
|
+
return this._nextDeps = n, this._nextVersions = r, this._nextUnsubs = o, this._currentEpoch = a, { prevDeps: e, prevVersions: t, prevUnsubs: i, nextDeps: n, nextVersions: r, nextUnsubs: o };
|
|
981
951
|
}
|
|
982
952
|
/**
|
|
983
953
|
* Commits the tracked dependencies as the current active dependencies.
|
|
@@ -995,20 +965,20 @@ class pe extends Y {
|
|
|
995
965
|
_cleanupEffect(e, t) {
|
|
996
966
|
if (this._nextDeps = null, this._nextVersions = null, this._nextUnsubs = null, t) {
|
|
997
967
|
if (e.prevDeps !== u) {
|
|
998
|
-
for (let
|
|
999
|
-
const n = e.prevDeps[
|
|
968
|
+
for (let i = 0; i < e.prevDeps.length; i++) {
|
|
969
|
+
const n = e.prevDeps[i];
|
|
1000
970
|
n?._tempUnsub && (n._tempUnsub(), n._tempUnsub = void 0);
|
|
1001
971
|
}
|
|
1002
972
|
S.release(e.prevDeps);
|
|
1003
973
|
}
|
|
1004
|
-
e.prevUnsubs !==
|
|
974
|
+
e.prevUnsubs !== f && y.release(e.prevUnsubs), e.prevVersions !== _ && d.release(e.prevVersions);
|
|
1005
975
|
} else {
|
|
1006
|
-
S.release(e.nextDeps),
|
|
1007
|
-
for (let
|
|
1008
|
-
e.nextUnsubs[
|
|
1009
|
-
if (
|
|
1010
|
-
for (let
|
|
1011
|
-
const n = e.prevDeps[
|
|
976
|
+
S.release(e.nextDeps), d.release(e.nextVersions);
|
|
977
|
+
for (let i = 0; i < e.nextUnsubs.length; i++)
|
|
978
|
+
e.nextUnsubs[i]?.();
|
|
979
|
+
if (y.release(e.nextUnsubs), e.prevDeps !== u)
|
|
980
|
+
for (let i = 0; i < e.prevDeps.length; i++) {
|
|
981
|
+
const n = e.prevDeps[i];
|
|
1012
982
|
n && (n._tempUnsub = void 0);
|
|
1013
983
|
}
|
|
1014
984
|
}
|
|
@@ -1024,7 +994,7 @@ class pe extends Y {
|
|
|
1024
994
|
});
|
|
1025
995
|
this._nextUnsubs && this._nextUnsubs.push(t);
|
|
1026
996
|
} catch (t) {
|
|
1027
|
-
console.error(R(t,
|
|
997
|
+
console.error(R(t, g, h.EFFECT_EXECUTION_FAILED)), this._nextUnsubs && this._nextUnsubs.push(() => {
|
|
1028
998
|
});
|
|
1029
999
|
}
|
|
1030
1000
|
}
|
|
@@ -1061,7 +1031,7 @@ class pe extends Y {
|
|
|
1061
1031
|
try {
|
|
1062
1032
|
this._cleanup();
|
|
1063
1033
|
} catch (e) {
|
|
1064
|
-
console.error(R(e,
|
|
1034
|
+
console.error(R(e, g, h.EFFECT_CLEANUP_FAILED));
|
|
1065
1035
|
}
|
|
1066
1036
|
this._cleanup = null;
|
|
1067
1037
|
}
|
|
@@ -1071,28 +1041,21 @@ class pe extends Y {
|
|
|
1071
1041
|
* @throws {EffectError} If an infinite loop is detected.
|
|
1072
1042
|
*/
|
|
1073
1043
|
_checkInfiniteLoop() {
|
|
1074
|
-
if (this._lastFlushEpoch !==
|
|
1075
|
-
const e = Date.now();
|
|
1076
|
-
this._history
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
n++;
|
|
1086
|
-
if (n > this._maxExecutions) {
|
|
1087
|
-
const r = new b(
|
|
1088
|
-
`Effect executed ${n} times within 1 second. Infinite loop suspected`
|
|
1089
|
-
);
|
|
1090
|
-
if (this.dispose(), console.error(r), a)
|
|
1091
|
-
throw r;
|
|
1044
|
+
if (this._lastFlushEpoch !== U && (this._lastFlushEpoch = U, this._executionsInEpoch = 0), this._executionsInEpoch++, this._executionsInEpoch > this._maxExecutionsPerFlush && this._throwInfiniteLoopError("per-effect"), oe() > D.MAX_EXECUTIONS_PER_FLUSH && this._throwInfiniteLoopError("global"), this._executionCount++, this._history && this._maxExecutions > 0) {
|
|
1045
|
+
const e = Date.now(), t = this._historyPtr, i = this._historyCapacity;
|
|
1046
|
+
this._history[t] = e;
|
|
1047
|
+
const n = (t + 1) % i, r = this._history[n] ?? 0;
|
|
1048
|
+
if (this._historyPtr = n, r > 0 && e - r < Z.ONE_SECOND_MS) {
|
|
1049
|
+
const o = new g(
|
|
1050
|
+
`Effect executed ${i} times within 1 second. Infinite loop suspected`
|
|
1051
|
+
);
|
|
1052
|
+
if (this.dispose(), console.error(o), this._onError && this._onError(o), l)
|
|
1053
|
+
throw o;
|
|
1054
|
+
}
|
|
1092
1055
|
}
|
|
1093
1056
|
}
|
|
1094
1057
|
_throwInfiniteLoopError(e) {
|
|
1095
|
-
const t = new
|
|
1058
|
+
const t = new g(
|
|
1096
1059
|
`Infinite loop detected (${e}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${w}`
|
|
1097
1060
|
);
|
|
1098
1061
|
throw this.dispose(), console.error(t), t;
|
|
@@ -1119,42 +1082,50 @@ class pe extends Y {
|
|
|
1119
1082
|
}
|
|
1120
1083
|
return !1;
|
|
1121
1084
|
}
|
|
1085
|
+
/**
|
|
1086
|
+
* Handles errors occurring during effect execution.
|
|
1087
|
+
* Wraps the error, logs it to console, and calls onError callback if provided.
|
|
1088
|
+
*/
|
|
1089
|
+
_handleExecutionError(e) {
|
|
1090
|
+
const t = R(e, g, h.EFFECT_EXECUTION_FAILED);
|
|
1091
|
+
console.error(t), this._onError && this._onError(t);
|
|
1092
|
+
}
|
|
1122
1093
|
/**
|
|
1123
1094
|
* Checks for potential infinite loops where an effect modifies its own dependencies.
|
|
1124
1095
|
* Only active if trackModifications is enabled and debug is on.
|
|
1125
1096
|
*/
|
|
1126
1097
|
_checkLoopWarnings() {
|
|
1127
|
-
if (this._trackModifications &&
|
|
1098
|
+
if (this._trackModifications && E.enabled) {
|
|
1128
1099
|
const e = this._dependencies;
|
|
1129
1100
|
for (let t = 0; t < e.length; t++) {
|
|
1130
|
-
const
|
|
1131
|
-
|
|
1101
|
+
const i = e[t];
|
|
1102
|
+
i && i._modifiedAtEpoch === this._currentEpoch && E.warn(
|
|
1132
1103
|
!0,
|
|
1133
|
-
`Effect is reading a dependency (${
|
|
1104
|
+
`Effect is reading a dependency (${E.getDebugName(i) || "unknown"}) that it just modified. Infinite loop may occur`
|
|
1134
1105
|
);
|
|
1135
1106
|
}
|
|
1136
1107
|
}
|
|
1137
1108
|
}
|
|
1138
1109
|
}
|
|
1139
|
-
function me(
|
|
1140
|
-
if (typeof
|
|
1141
|
-
throw new
|
|
1142
|
-
const t = new pe(
|
|
1110
|
+
function me(s, e = {}) {
|
|
1111
|
+
if (typeof s != "function")
|
|
1112
|
+
throw new g(h.EFFECT_MUST_BE_FUNCTION);
|
|
1113
|
+
const t = new pe(s, e);
|
|
1143
1114
|
return t.execute(), t;
|
|
1144
1115
|
}
|
|
1145
1116
|
export {
|
|
1146
|
-
|
|
1147
|
-
|
|
1117
|
+
P as AsyncState,
|
|
1118
|
+
b as AtomError,
|
|
1148
1119
|
N as ComputedError,
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1120
|
+
q as DEBUG_CONFIG,
|
|
1121
|
+
E as DEBUG_RUNTIME,
|
|
1122
|
+
g as EffectError,
|
|
1152
1123
|
be as POOL_CONFIG,
|
|
1153
|
-
|
|
1124
|
+
D as SCHEDULER_CONFIG,
|
|
1154
1125
|
O as SchedulerError,
|
|
1155
|
-
|
|
1126
|
+
ye as atom,
|
|
1156
1127
|
Se as batch,
|
|
1157
|
-
|
|
1128
|
+
Ie as computed,
|
|
1158
1129
|
me as effect,
|
|
1159
1130
|
ue as isAtom,
|
|
1160
1131
|
ge as isComputed,
|