@but212/atom-effect 0.19.0 → 0.20.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 +4 -3
- 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 +15 -76
- package/dist/index.mjs +333 -384
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -9
package/dist/index.mjs
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
const
|
|
2
|
-
ONE_SECOND_MS: 1e3
|
|
3
|
-
}, m = {
|
|
1
|
+
const k = {
|
|
4
2
|
IDLE: "idle",
|
|
5
3
|
PENDING: "pending",
|
|
6
4
|
RESOLVED: "resolved",
|
|
7
5
|
REJECTED: "rejected"
|
|
8
|
-
},
|
|
9
|
-
DISPOSED: 1
|
|
10
|
-
}, a = {
|
|
11
|
-
...M,
|
|
6
|
+
}, l = {
|
|
7
|
+
DISPOSED: 1,
|
|
12
8
|
EXECUTING: 8
|
|
13
|
-
},
|
|
14
|
-
|
|
9
|
+
}, q = {
|
|
10
|
+
DISPOSED: 1,
|
|
11
|
+
/** Marker bit: identifies this node as a computed. */
|
|
12
|
+
IS_COMPUTED: 2,
|
|
15
13
|
DIRTY: 8,
|
|
16
14
|
IDLE: 16,
|
|
17
15
|
PENDING: 32,
|
|
@@ -19,15 +17,15 @@ const tt = {
|
|
|
19
17
|
REJECTED: 128,
|
|
20
18
|
RECOMPUTING: 256,
|
|
21
19
|
HAS_ERROR: 512
|
|
22
|
-
},
|
|
23
|
-
|
|
20
|
+
}, E = {
|
|
21
|
+
DISPOSED: 1,
|
|
24
22
|
SYNC: 8,
|
|
25
23
|
NOTIFICATION_SCHEDULED: 16
|
|
26
|
-
},
|
|
24
|
+
}, St = {
|
|
27
25
|
MAX_SIZE: 1e3,
|
|
28
26
|
WARMUP_SIZE: 100,
|
|
29
27
|
ENABLE_STATS: !1
|
|
30
|
-
},
|
|
28
|
+
}, S = {
|
|
31
29
|
// Infinite loop protection
|
|
32
30
|
MAX_EXECUTIONS_PER_SECOND: 1e3,
|
|
33
31
|
MAX_EXECUTIONS_PER_EFFECT: 100,
|
|
@@ -40,71 +38,71 @@ const tt = {
|
|
|
40
38
|
BATCH_QUEUE_SHRINK_THRESHOLD: 1e3
|
|
41
39
|
}, et = {
|
|
42
40
|
WARN_INFINITE_LOOP: !0
|
|
43
|
-
},
|
|
44
|
-
class
|
|
41
|
+
}, z = 1073741823, u = typeof process < "u" && process.env && process.env.NODE_ENV !== "production" || typeof __DEV__ < "u" && !!__DEV__, st = Object.freeze([]);
|
|
42
|
+
class O extends Error {
|
|
45
43
|
constructor(t, e = null, s = !0) {
|
|
46
|
-
super(t), this.cause = e, this.recoverable = s, this.
|
|
44
|
+
super(t), this.cause = e, this.recoverable = s, this.name = "AtomError";
|
|
47
45
|
}
|
|
48
46
|
}
|
|
49
|
-
class
|
|
47
|
+
class d extends O {
|
|
50
48
|
constructor(t, e = null) {
|
|
51
49
|
super(t, e, !0), this.name = "ComputedError";
|
|
52
50
|
}
|
|
53
51
|
}
|
|
54
|
-
class
|
|
52
|
+
class I extends O {
|
|
55
53
|
constructor(t, e = null) {
|
|
56
54
|
super(t, e, !1), this.name = "EffectError";
|
|
57
55
|
}
|
|
58
56
|
}
|
|
59
|
-
class
|
|
57
|
+
class L extends O {
|
|
60
58
|
constructor(t, e = null) {
|
|
61
59
|
super(t, e, !1), this.name = "SchedulerError";
|
|
62
60
|
}
|
|
63
61
|
}
|
|
64
|
-
const
|
|
65
|
-
function
|
|
62
|
+
const X = /* @__PURE__ */ Symbol("AtomEffect.DebugName"), it = /* @__PURE__ */ Symbol("AtomEffect.Id"), G = /* @__PURE__ */ Symbol("AtomEffect.Type"), v = /* @__PURE__ */ Symbol("AtomEffect.NoDefaultValue"), nt = (i) => "dependencies" in i && Array.isArray(i.dependencies);
|
|
63
|
+
function $(i, t, e) {
|
|
66
64
|
if (i === t)
|
|
67
|
-
throw new
|
|
65
|
+
throw new d(
|
|
68
66
|
"Circular dependency detected: The computation refers to itself explicitly or implicitly."
|
|
69
67
|
);
|
|
70
68
|
if (!e.has(i.id) && (e.add(i.id), nt(i))) {
|
|
71
69
|
const s = i.dependencies;
|
|
72
70
|
for (let n = 0; n < s.length; n++) {
|
|
73
71
|
const r = s[n];
|
|
74
|
-
r &&
|
|
72
|
+
r && $(r, t, e);
|
|
75
73
|
}
|
|
76
74
|
}
|
|
77
75
|
}
|
|
78
|
-
const
|
|
76
|
+
const D = {
|
|
79
77
|
// Dev mode flag
|
|
80
|
-
enabled:
|
|
78
|
+
enabled: u,
|
|
81
79
|
warnInfiniteLoop: et.WARN_INFINITE_LOOP,
|
|
82
80
|
warn(i, t) {
|
|
83
|
-
|
|
81
|
+
u && this.enabled && i && console.warn(`[Atom Effect] ${t}`);
|
|
84
82
|
},
|
|
85
83
|
checkCircular(i, t) {
|
|
86
84
|
if (i === t)
|
|
87
|
-
throw new
|
|
88
|
-
|
|
85
|
+
throw new d("Direct circular dependency detected");
|
|
86
|
+
u && this.enabled && $(i, t, /* @__PURE__ */ new Set());
|
|
89
87
|
},
|
|
90
88
|
attachDebugInfo(i, t, e) {
|
|
91
|
-
if (!
|
|
89
|
+
if (!u || !this.enabled) return;
|
|
92
90
|
const s = i;
|
|
93
|
-
s[
|
|
91
|
+
s[X] = `${t}_${e}`, s[it] = e, s[G] = t;
|
|
94
92
|
},
|
|
95
|
-
getDebugName: (i) => i?.[
|
|
93
|
+
getDebugName: (i) => i?.[X],
|
|
96
94
|
getDebugType: (i) => i?.[G]
|
|
97
95
|
};
|
|
98
96
|
let rt = 1;
|
|
99
97
|
const ot = () => rt++;
|
|
100
|
-
function
|
|
98
|
+
function K(i, t, e) {
|
|
101
99
|
if (typeof t == "function") {
|
|
102
100
|
const n = t;
|
|
103
|
-
for (let r = 0,
|
|
104
|
-
const
|
|
105
|
-
if (
|
|
101
|
+
for (let r = 0, o = e.length; r < o; r++) {
|
|
102
|
+
const h = e[r];
|
|
103
|
+
if (h && h.fn === n) return;
|
|
106
104
|
}
|
|
107
|
-
e.push(new
|
|
105
|
+
e.push(new M(n, void 0));
|
|
108
106
|
return;
|
|
109
107
|
}
|
|
110
108
|
if ("addDependency" in t) {
|
|
@@ -113,41 +111,37 @@ function j(i, t, e) {
|
|
|
113
111
|
}
|
|
114
112
|
const s = t;
|
|
115
113
|
for (let n = 0, r = e.length; n < r; n++) {
|
|
116
|
-
const
|
|
117
|
-
if (
|
|
114
|
+
const o = e[n];
|
|
115
|
+
if (o && o.sub === s) return;
|
|
118
116
|
}
|
|
119
|
-
e.push(new
|
|
117
|
+
e.push(new M(void 0, s));
|
|
120
118
|
}
|
|
121
|
-
function
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
h
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const h = r.node;
|
|
136
|
-
h._tempUnsub !== void 0 && (h._tempUnsub(), h._tempUnsub = void 0), r.unsub = void 0;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
119
|
+
function Q(i, t, e) {
|
|
120
|
+
const s = /* @__PURE__ */ new Map();
|
|
121
|
+
for (let n = 0, r = t.length; n < r; n++) {
|
|
122
|
+
const o = t[n];
|
|
123
|
+
o?.unsub && s.set(o.node, o.unsub);
|
|
124
|
+
}
|
|
125
|
+
for (let n = 0, r = i.length; n < r; n++) {
|
|
126
|
+
const o = i[n];
|
|
127
|
+
if (!o) continue;
|
|
128
|
+
const h = o.node, f = s.get(h);
|
|
129
|
+
f !== void 0 ? (o.unsub = f, s.delete(h)) : (D.checkCircular(h, e), o.unsub = h.subscribe(e));
|
|
130
|
+
}
|
|
131
|
+
for (const n of s.values())
|
|
132
|
+
n();
|
|
139
133
|
}
|
|
140
|
-
class
|
|
134
|
+
class x {
|
|
141
135
|
constructor(t, e, s = void 0) {
|
|
142
136
|
this.node = t, this.version = e, this.unsub = s;
|
|
143
137
|
}
|
|
144
138
|
}
|
|
145
|
-
class
|
|
139
|
+
class M {
|
|
146
140
|
constructor(t, e) {
|
|
147
141
|
this.fn = t, this.sub = e;
|
|
148
142
|
}
|
|
149
143
|
}
|
|
150
|
-
const
|
|
144
|
+
const c = {
|
|
151
145
|
// Computed Errors
|
|
152
146
|
COMPUTED_MUST_BE_FUNCTION: "Computed target must be a function",
|
|
153
147
|
COMPUTED_ASYNC_PENDING_NO_DEFAULT: "Async computation pending with no default value",
|
|
@@ -170,40 +164,37 @@ const u = {
|
|
|
170
164
|
// Effect frequency
|
|
171
165
|
EFFECT_FREQUENCY_LIMIT_EXCEEDED: "Effect executed too frequently within 1 second. Suspected infinite loop."
|
|
172
166
|
};
|
|
173
|
-
function
|
|
174
|
-
if (i instanceof
|
|
167
|
+
function b(i, t, e) {
|
|
168
|
+
if (i instanceof O)
|
|
175
169
|
return i;
|
|
176
|
-
const s = i instanceof Error, n = s ? i.message : String(i), r = s ? i : void 0
|
|
177
|
-
|
|
178
|
-
i instanceof TypeError ? h = "Type error" : i instanceof ReferenceError && (h = "Reference error");
|
|
179
|
-
const c = `${h} (${e}): ${n}`;
|
|
180
|
-
return new t(c, r);
|
|
170
|
+
const s = i instanceof Error, n = s ? i.message : String(i), r = s ? i : void 0, h = `${s ? i.constructor.name : "Unexpected error"} (${e}): ${n}`;
|
|
171
|
+
return new t(h, r);
|
|
181
172
|
}
|
|
182
|
-
class
|
|
173
|
+
class W {
|
|
183
174
|
constructor() {
|
|
184
|
-
this.flags = 0, this.version = 0, this._lastSeenEpoch = -1, this.
|
|
175
|
+
this.flags = 0, this.version = 0, this._lastSeenEpoch = -1, this.id = ot() & z;
|
|
185
176
|
}
|
|
186
177
|
}
|
|
187
|
-
class
|
|
178
|
+
class J extends W {
|
|
188
179
|
/**
|
|
189
180
|
* Adds subscriber.
|
|
190
181
|
*/
|
|
191
182
|
subscribe(t) {
|
|
192
183
|
const e = typeof t == "function";
|
|
193
184
|
if (!e && (!t || typeof t.execute != "function"))
|
|
194
|
-
throw
|
|
185
|
+
throw b(
|
|
195
186
|
new TypeError("Invalid subscriber"),
|
|
196
|
-
|
|
197
|
-
|
|
187
|
+
O,
|
|
188
|
+
c.ATOM_SUBSCRIBER_MUST_BE_FUNCTION
|
|
198
189
|
);
|
|
199
190
|
const s = this._subscribers;
|
|
200
|
-
for (let r = 0,
|
|
201
|
-
const
|
|
202
|
-
if (
|
|
203
|
-
return
|
|
191
|
+
for (let r = 0, o = s.length; r < o; r++) {
|
|
192
|
+
const h = s[r];
|
|
193
|
+
if (h && (e ? h.fn === t : h.sub === t))
|
|
194
|
+
return u && console.warn("Duplicate subscription ignored."), () => {
|
|
204
195
|
};
|
|
205
196
|
}
|
|
206
|
-
const n = new
|
|
197
|
+
const n = new M(
|
|
207
198
|
e ? t : void 0,
|
|
208
199
|
e ? void 0 : t
|
|
209
200
|
);
|
|
@@ -222,157 +213,145 @@ class $ extends q {
|
|
|
222
213
|
if (this._subscribers.length === 0) return;
|
|
223
214
|
const s = this._subscribers.slice(0), n = s.length;
|
|
224
215
|
for (let r = 0; r < n; r++) {
|
|
225
|
-
const
|
|
226
|
-
if (
|
|
216
|
+
const o = s[r];
|
|
217
|
+
if (o)
|
|
227
218
|
try {
|
|
228
|
-
|
|
229
|
-
} catch (
|
|
230
|
-
this._handleNotifyError(
|
|
219
|
+
o.fn ? o.fn(t, e) : o.sub && o.sub.execute();
|
|
220
|
+
} catch (h) {
|
|
221
|
+
this._handleNotifyError(h);
|
|
231
222
|
}
|
|
232
223
|
}
|
|
233
224
|
}
|
|
234
225
|
_handleNotifyError(t) {
|
|
235
|
-
console.error(
|
|
226
|
+
console.error(b(t, O, c.ATOM_INDIVIDUAL_SUBSCRIBER_FAILED));
|
|
236
227
|
}
|
|
237
228
|
}
|
|
238
|
-
let
|
|
239
|
-
const
|
|
240
|
-
let
|
|
241
|
-
|
|
242
|
-
|
|
229
|
+
let w = 0;
|
|
230
|
+
const V = () => (w = w + 1 & z || 1, w), B = (i) => i + 1 & z;
|
|
231
|
+
let H = 0, U = !1, Z = 0;
|
|
232
|
+
const ht = () => Z;
|
|
233
|
+
function Y() {
|
|
234
|
+
return U ? (u && console.warn("startFlush() called during flush - ignored"), !1) : (U = !0, Z = V(), H = 0, !0);
|
|
243
235
|
}
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
},
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
/** Epoch counter */
|
|
252
|
-
_epoch: 0,
|
|
253
|
-
/** State flags */
|
|
254
|
-
_isProcessing: !1,
|
|
255
|
-
_isBatching: !1,
|
|
256
|
-
_isFlushingSync: !1,
|
|
257
|
-
/** Batching state */
|
|
258
|
-
_batchDepth: 0,
|
|
259
|
-
_batchQueue: [],
|
|
260
|
-
_batchQueueSize: 0,
|
|
261
|
-
/** Config */
|
|
262
|
-
_maxFlushIterations: I.MAX_FLUSH_ITERATIONS,
|
|
236
|
+
const j = () => {
|
|
237
|
+
U = !1;
|
|
238
|
+
}, ct = () => U ? ++H : 0;
|
|
239
|
+
class ut {
|
|
240
|
+
constructor() {
|
|
241
|
+
this._queueBuffer = [[], []], this._bufferIndex = 0, this._size = 0, this._epoch = 0, this._isProcessing = !1, this._isBatching = !1, this._isFlushingSync = !1, this._batchDepth = 0, this._batchQueue = [], this._batchQueueSize = 0, this._maxFlushIterations = S.MAX_FLUSH_ITERATIONS, this.onOverflow = null, this._boundRunLoop = this._runLoop.bind(this);
|
|
242
|
+
}
|
|
263
243
|
get phase() {
|
|
264
244
|
return this._isProcessing || this._isFlushingSync ? 2 : this._isBatching ? 1 : 0;
|
|
265
|
-
}
|
|
245
|
+
}
|
|
266
246
|
get queueSize() {
|
|
267
247
|
return this._size;
|
|
268
|
-
}
|
|
248
|
+
}
|
|
269
249
|
get isBatching() {
|
|
270
250
|
return this._isBatching;
|
|
271
|
-
}
|
|
251
|
+
}
|
|
272
252
|
/**
|
|
273
253
|
* Schedules job.
|
|
274
254
|
*/
|
|
275
|
-
schedule(
|
|
276
|
-
if (
|
|
277
|
-
throw new
|
|
278
|
-
if (
|
|
279
|
-
if (
|
|
280
|
-
this._batchQueue[this._batchQueueSize++] =
|
|
255
|
+
schedule(t) {
|
|
256
|
+
if (u && typeof t != "function")
|
|
257
|
+
throw new L("Scheduler callback must be a function");
|
|
258
|
+
if (t._nextEpoch !== this._epoch) {
|
|
259
|
+
if (t._nextEpoch = this._epoch, this._isBatching || this._isFlushingSync) {
|
|
260
|
+
this._batchQueue[this._batchQueueSize++] = t;
|
|
281
261
|
return;
|
|
282
262
|
}
|
|
283
|
-
this._queueBuffer[this._bufferIndex][this._size++] =
|
|
263
|
+
this._queueBuffer[this._bufferIndex][this._size++] = t, this._isProcessing || this._flush();
|
|
284
264
|
}
|
|
285
|
-
}
|
|
265
|
+
}
|
|
286
266
|
/**
|
|
287
267
|
* Triggers flush.
|
|
288
268
|
*/
|
|
289
269
|
_flush() {
|
|
290
|
-
this._isProcessing || this._size === 0 || (this._isProcessing = !0, queueMicrotask(this.
|
|
291
|
-
}
|
|
270
|
+
this._isProcessing || this._size === 0 || (this._isProcessing = !0, queueMicrotask(this._boundRunLoop));
|
|
271
|
+
}
|
|
292
272
|
/**
|
|
293
273
|
* Scheduler loop.
|
|
294
274
|
*/
|
|
295
|
-
_runLoop
|
|
275
|
+
_runLoop() {
|
|
296
276
|
try {
|
|
297
|
-
if (
|
|
298
|
-
const
|
|
299
|
-
|
|
277
|
+
if (this._size === 0) return;
|
|
278
|
+
const t = Y();
|
|
279
|
+
this._drainQueue(), t && j();
|
|
300
280
|
} finally {
|
|
301
|
-
|
|
281
|
+
this._isProcessing = !1, this._size > 0 && !this._isBatching && this._flush();
|
|
302
282
|
}
|
|
303
|
-
}
|
|
283
|
+
}
|
|
304
284
|
_flushSync() {
|
|
305
285
|
this._isFlushingSync = !0;
|
|
306
|
-
const
|
|
286
|
+
const t = Y();
|
|
307
287
|
try {
|
|
308
288
|
this._mergeBatchQueue(), this._drainQueue();
|
|
309
289
|
} finally {
|
|
310
|
-
this._isFlushingSync = !1,
|
|
290
|
+
this._isFlushingSync = !1, t && j();
|
|
311
291
|
}
|
|
312
|
-
}
|
|
292
|
+
}
|
|
313
293
|
_mergeBatchQueue() {
|
|
314
294
|
if (this._batchQueueSize === 0) return;
|
|
315
|
-
const
|
|
316
|
-
let
|
|
317
|
-
for (let
|
|
318
|
-
const
|
|
319
|
-
|
|
295
|
+
const t = ++this._epoch, e = this._batchQueue, s = this._queueBuffer[this._bufferIndex];
|
|
296
|
+
let n = this._size;
|
|
297
|
+
for (let r = 0; r < this._batchQueueSize; r++) {
|
|
298
|
+
const o = e[r];
|
|
299
|
+
o._nextEpoch !== t && (o._nextEpoch = t, s[n++] = o);
|
|
320
300
|
}
|
|
321
|
-
this._size =
|
|
322
|
-
}
|
|
301
|
+
this._size = n, this._batchQueueSize = 0, e.length > S.BATCH_QUEUE_SHRINK_THRESHOLD && (e.length = 0);
|
|
302
|
+
}
|
|
323
303
|
_drainQueue() {
|
|
324
|
-
let
|
|
304
|
+
let t = 0;
|
|
325
305
|
for (; this._size > 0; ) {
|
|
326
|
-
if (++
|
|
306
|
+
if (++t > this._maxFlushIterations) {
|
|
327
307
|
this._handleFlushOverflow();
|
|
328
308
|
return;
|
|
329
309
|
}
|
|
330
310
|
this._processQueue(), this._mergeBatchQueue();
|
|
331
311
|
}
|
|
332
|
-
}
|
|
312
|
+
}
|
|
333
313
|
_processQueue() {
|
|
334
|
-
const
|
|
335
|
-
this._bufferIndex =
|
|
336
|
-
for (let
|
|
314
|
+
const t = this._bufferIndex, e = this._queueBuffer[t], s = this._size;
|
|
315
|
+
this._bufferIndex = t ^ 1, this._size = 0, this._epoch++;
|
|
316
|
+
for (let n = 0; n < s; n++)
|
|
337
317
|
try {
|
|
338
|
-
|
|
339
|
-
} catch (
|
|
340
|
-
console.error(new
|
|
318
|
+
e[n]();
|
|
319
|
+
} catch (r) {
|
|
320
|
+
console.error(new L("Error occurred during scheduler execution", r));
|
|
341
321
|
}
|
|
342
|
-
|
|
343
|
-
}
|
|
344
|
-
/** Overflow callback */
|
|
345
|
-
onOverflow: null,
|
|
322
|
+
e.length = 0;
|
|
323
|
+
}
|
|
346
324
|
_handleFlushOverflow() {
|
|
347
|
-
const
|
|
325
|
+
const t = this._size + this._batchQueueSize;
|
|
348
326
|
if (console.error(
|
|
349
|
-
new
|
|
350
|
-
|
|
327
|
+
new L(
|
|
328
|
+
c.SCHEDULER_FLUSH_OVERFLOW(this._maxFlushIterations, t)
|
|
351
329
|
)
|
|
352
330
|
), this._size = 0, this._queueBuffer[this._bufferIndex].length = 0, this._batchQueueSize = 0, this.onOverflow)
|
|
353
331
|
try {
|
|
354
|
-
this.onOverflow(
|
|
332
|
+
this.onOverflow(t);
|
|
355
333
|
} catch {
|
|
356
334
|
}
|
|
357
|
-
}
|
|
335
|
+
}
|
|
358
336
|
startBatch() {
|
|
359
337
|
this._batchDepth++, this._isBatching = !0;
|
|
360
|
-
}
|
|
338
|
+
}
|
|
361
339
|
endBatch() {
|
|
362
340
|
if (this._batchDepth === 0) {
|
|
363
|
-
|
|
341
|
+
u && console.warn("endBatch() called without matching startBatch(). Ignoring.");
|
|
364
342
|
return;
|
|
365
343
|
}
|
|
366
344
|
--this._batchDepth === 0 && (this._flushSync(), this._isBatching = !1);
|
|
367
|
-
}
|
|
368
|
-
setMaxFlushIterations(
|
|
369
|
-
if (
|
|
370
|
-
throw new
|
|
371
|
-
`Max flush iterations must be at least ${
|
|
345
|
+
}
|
|
346
|
+
setMaxFlushIterations(t) {
|
|
347
|
+
if (t < S.MIN_FLUSH_ITERATIONS)
|
|
348
|
+
throw new L(
|
|
349
|
+
`Max flush iterations must be at least ${S.MIN_FLUSH_ITERATIONS}`
|
|
372
350
|
);
|
|
373
|
-
this._maxFlushIterations =
|
|
351
|
+
this._maxFlushIterations = t;
|
|
374
352
|
}
|
|
375
|
-
}
|
|
353
|
+
}
|
|
354
|
+
const N = new ut(), a = {
|
|
376
355
|
/** Active listener. */
|
|
377
356
|
current: null,
|
|
378
357
|
/**
|
|
@@ -392,35 +371,35 @@ const X = () => {
|
|
|
392
371
|
}
|
|
393
372
|
}
|
|
394
373
|
};
|
|
395
|
-
function
|
|
396
|
-
const t =
|
|
374
|
+
function Dt(i) {
|
|
375
|
+
const t = a.current;
|
|
397
376
|
if (t === null) return i();
|
|
398
|
-
|
|
377
|
+
a.current = null;
|
|
399
378
|
try {
|
|
400
379
|
return i();
|
|
401
380
|
} finally {
|
|
402
|
-
|
|
381
|
+
a.current = t;
|
|
403
382
|
}
|
|
404
383
|
}
|
|
405
|
-
class
|
|
384
|
+
class lt extends J {
|
|
406
385
|
constructor(t, e) {
|
|
407
|
-
super(), this._pendingOldValue = void 0, this._notifyTask = void 0, this._subscribers = [], this._value = t, e && (this.flags |=
|
|
386
|
+
super(), this._pendingOldValue = void 0, this._notifyTask = void 0, this._subscribers = [], this._value = t, e && (this.flags |= E.SYNC), D.attachDebugInfo(this, "atom", this.id);
|
|
408
387
|
}
|
|
409
388
|
get value() {
|
|
410
|
-
const t =
|
|
411
|
-
return t &&
|
|
389
|
+
const t = a.current;
|
|
390
|
+
return t && K(this, t, this._subscribers), this._value;
|
|
412
391
|
}
|
|
413
392
|
set value(t) {
|
|
414
393
|
const e = this._value;
|
|
415
394
|
if (Object.is(e, t)) return;
|
|
416
|
-
this._value = t, this.version =
|
|
395
|
+
this._value = t, this.version = B(this.version);
|
|
417
396
|
const s = this.flags;
|
|
418
|
-
if (!(this._subscribers.length === 0 || s &
|
|
419
|
-
if (this._pendingOldValue = e, this.flags = s |
|
|
397
|
+
if (!(this._subscribers.length === 0 || s & E.NOTIFICATION_SCHEDULED)) {
|
|
398
|
+
if (this._pendingOldValue = e, this.flags = s | E.NOTIFICATION_SCHEDULED, s & E.SYNC && !N.isBatching) {
|
|
420
399
|
this._flushNotifications();
|
|
421
400
|
return;
|
|
422
401
|
}
|
|
423
|
-
this._notifyTask || (this._notifyTask = () => this._flushNotifications()),
|
|
402
|
+
this._notifyTask || (this._notifyTask = () => this._flushNotifications()), N.schedule(this._notifyTask);
|
|
424
403
|
}
|
|
425
404
|
}
|
|
426
405
|
/**
|
|
@@ -428,7 +407,7 @@ class _t extends $ {
|
|
|
428
407
|
*/
|
|
429
408
|
_flushNotifications() {
|
|
430
409
|
const t = this.flags;
|
|
431
|
-
if (!(t &
|
|
410
|
+
if (!(t & E.NOTIFICATION_SCHEDULED) || t & E.DISPOSED)
|
|
432
411
|
return;
|
|
433
412
|
const e = this._pendingOldValue;
|
|
434
413
|
this._pendingOldValue = void 0, this.flags &= -17, this._notifySubscribers(this._value, e);
|
|
@@ -437,20 +416,20 @@ class _t extends $ {
|
|
|
437
416
|
return this._value;
|
|
438
417
|
}
|
|
439
418
|
dispose() {
|
|
440
|
-
this.flags &
|
|
419
|
+
this.flags & E.DISPOSED || (this._subscribers.length = 0, this.flags |= E.DISPOSED, this._value = void 0, this._pendingOldValue = void 0, this._notifyTask = void 0);
|
|
441
420
|
}
|
|
442
421
|
}
|
|
443
|
-
function
|
|
444
|
-
return new
|
|
422
|
+
function Ct(i, t = {}) {
|
|
423
|
+
return new lt(i, t.sync ?? !1);
|
|
445
424
|
}
|
|
446
|
-
class
|
|
425
|
+
class _t {
|
|
447
426
|
/**
|
|
448
427
|
* @param limit - Max unique arrays to hold (default: 50). Prevents the pool itself from consuming too much memory.
|
|
449
428
|
* @param capacity - Max length of an array to accept (default: 256).
|
|
450
429
|
* @param enableStats - Force-enable stats even in production (default: false).
|
|
451
430
|
*/
|
|
452
431
|
constructor(t = 50, e = 256, s = !1) {
|
|
453
|
-
this.limit = t, this.capacity = e, this.pool = [], this.stats = null, this.stats =
|
|
432
|
+
this.limit = t, this.capacity = e, this.pool = [], this.stats = null, this.stats = u || s ? {
|
|
454
433
|
acquired: 0,
|
|
455
434
|
released: 0,
|
|
456
435
|
rejected: { frozen: 0, tooLarge: 0, poolFull: 0 }
|
|
@@ -510,63 +489,59 @@ class lt {
|
|
|
510
489
|
});
|
|
511
490
|
}
|
|
512
491
|
}
|
|
513
|
-
const
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
y([]);
|
|
517
|
-
y([]);
|
|
518
|
-
const l = y([]), D = new lt();
|
|
492
|
+
const _ = Object.freeze(
|
|
493
|
+
[]
|
|
494
|
+
), p = new _t();
|
|
519
495
|
function at(i) {
|
|
520
496
|
return i !== null && typeof i == "object" && "value" in i && typeof i.subscribe == "function";
|
|
521
497
|
}
|
|
522
|
-
function
|
|
498
|
+
function Tt(i) {
|
|
523
499
|
return at(i) && typeof i.invalidate == "function";
|
|
524
500
|
}
|
|
525
|
-
function
|
|
501
|
+
function bt(i) {
|
|
526
502
|
return i !== null && typeof i == "object" && typeof i.dispose == "function" && typeof i.run == "function";
|
|
527
503
|
}
|
|
528
|
-
function
|
|
504
|
+
function tt(i) {
|
|
529
505
|
return i !== null && typeof i == "object" && typeof i.then == "function";
|
|
530
506
|
}
|
|
531
|
-
const
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
class Z extends $ {
|
|
507
|
+
const ft = 3, Et = Number.MAX_SAFE_INTEGER - 1, { IDLE: F, DIRTY: T, PENDING: R, RESOLVED: g, REJECTED: A, HAS_ERROR: m, RECOMPUTING: y, DISPOSED: P, IS_COMPUTED: dt } = q;
|
|
508
|
+
function pt(i) {
|
|
509
|
+
return i & g ? k.RESOLVED : i & R ? k.PENDING : i & A ? k.REJECTED : k.IDLE;
|
|
510
|
+
}
|
|
511
|
+
class It extends J {
|
|
537
512
|
constructor(t, e = {}) {
|
|
538
|
-
if (typeof t != "function") throw new
|
|
539
|
-
super(), this._error = null, this._promiseId = 0, this._subscribers = [], this._links =
|
|
513
|
+
if (typeof t != "function") throw new d(c.COMPUTED_MUST_BE_FUNCTION);
|
|
514
|
+
super(), this._error = null, this._promiseId = 0, this._subscribers = [], this._links = _, this._asyncStartAggregateVersion = 0, this._asyncRetryCount = 0, this._trackEpoch = -1, this._trackLinks = _, this._trackCount = 0, this._value = void 0, this.flags = dt | T | F, this._equal = e.equal ?? Object.is, this._fn = t, this._defaultValue = "defaultValue" in e ? e.defaultValue : v, this._onError = e.onError ?? null;
|
|
540
515
|
const s = e.maxAsyncRetries;
|
|
541
|
-
if (this._maxAsyncRetries = s != null && s >= 0 ? s : ft,
|
|
516
|
+
if (this._maxAsyncRetries = s != null && s >= 0 ? s : ft, D.attachDebugInfo(this, "computed", this.id), e.lazy === !1)
|
|
542
517
|
try {
|
|
543
518
|
this._recompute();
|
|
544
519
|
} catch {
|
|
545
520
|
}
|
|
546
521
|
}
|
|
547
522
|
_track() {
|
|
548
|
-
const t =
|
|
549
|
-
t &&
|
|
523
|
+
const t = a.current;
|
|
524
|
+
t && K(this, t, this._subscribers);
|
|
550
525
|
}
|
|
551
526
|
get value() {
|
|
552
527
|
this._track();
|
|
553
528
|
const t = this.flags;
|
|
554
|
-
if ((t & (
|
|
529
|
+
if ((t & (g | T | F)) === g)
|
|
555
530
|
return this._value;
|
|
556
|
-
if (t &
|
|
557
|
-
throw new
|
|
558
|
-
if (t &
|
|
559
|
-
if (this._defaultValue !==
|
|
560
|
-
throw new
|
|
531
|
+
if (t & P)
|
|
532
|
+
throw new d(c.COMPUTED_DISPOSED);
|
|
533
|
+
if (t & y) {
|
|
534
|
+
if (this._defaultValue !== v) return this._defaultValue;
|
|
535
|
+
throw new d(c.COMPUTED_CIRCULAR_DEPENDENCY);
|
|
561
536
|
}
|
|
562
|
-
if (t & (
|
|
537
|
+
if (t & (T | F) && (this._recompute(), this.flags & g))
|
|
563
538
|
return this._value;
|
|
564
|
-
const e = this._defaultValue, s = e !==
|
|
565
|
-
if (this.flags &
|
|
539
|
+
const e = this._defaultValue, s = e !== v;
|
|
540
|
+
if (this.flags & R) {
|
|
566
541
|
if (s) return e;
|
|
567
|
-
throw new
|
|
542
|
+
throw new d(c.COMPUTED_ASYNC_PENDING_NO_DEFAULT);
|
|
568
543
|
}
|
|
569
|
-
if (this.flags &
|
|
544
|
+
if (this.flags & A) {
|
|
570
545
|
if (this._error?.recoverable && s) return e;
|
|
571
546
|
throw this._error;
|
|
572
547
|
}
|
|
@@ -576,65 +551,61 @@ class Z extends $ {
|
|
|
576
551
|
return this._value;
|
|
577
552
|
}
|
|
578
553
|
get state() {
|
|
579
|
-
return this._track(),
|
|
554
|
+
return this._track(), pt(this.flags);
|
|
580
555
|
}
|
|
581
556
|
get hasError() {
|
|
582
|
-
if (this._track(), this.flags & (
|
|
557
|
+
if (this._track(), this.flags & (A | m)) return !0;
|
|
583
558
|
const t = this._links;
|
|
584
559
|
for (let e = 0, s = t.length; e < s; e++) {
|
|
585
560
|
const n = t[e]?.node;
|
|
586
|
-
if (n && n.flags &
|
|
561
|
+
if (n && n.flags & m) return !0;
|
|
587
562
|
}
|
|
588
563
|
return !1;
|
|
589
564
|
}
|
|
590
|
-
// ... (isValid, errors, lastError getters remain structurally similar, omitted for brevity if mostly unchanged) ...
|
|
591
565
|
get isValid() {
|
|
592
566
|
return !this.hasError;
|
|
593
567
|
}
|
|
594
568
|
get errors() {
|
|
595
569
|
if (this._track(), !this.hasError) return st;
|
|
596
|
-
const t =
|
|
597
|
-
|
|
598
|
-
const e =
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
const F = V[L];
|
|
609
|
-
F && e.indexOf(F) === -1 && e.push(F);
|
|
570
|
+
const t = [];
|
|
571
|
+
this._error && t.push(this._error);
|
|
572
|
+
const e = this._links;
|
|
573
|
+
for (let s = 0, n = e.length; s < n; s++) {
|
|
574
|
+
const r = e[s].node;
|
|
575
|
+
if (r.flags & m) {
|
|
576
|
+
const o = r;
|
|
577
|
+
if (o.errors) {
|
|
578
|
+
const h = o.errors;
|
|
579
|
+
for (let f = 0; f < h.length; f++) {
|
|
580
|
+
const C = h[f];
|
|
581
|
+
C && t.indexOf(C) === -1 && t.push(C);
|
|
610
582
|
}
|
|
611
583
|
}
|
|
612
584
|
}
|
|
613
585
|
}
|
|
614
|
-
|
|
615
|
-
return this._errorCacheEpoch = t, this._cachedErrors = n, n;
|
|
586
|
+
return Object.freeze(t);
|
|
616
587
|
}
|
|
617
588
|
get lastError() {
|
|
618
589
|
return this._track(), this._error;
|
|
619
590
|
}
|
|
620
591
|
get isPending() {
|
|
621
|
-
return this._track(), (this.flags &
|
|
592
|
+
return this._track(), (this.flags & R) !== 0;
|
|
622
593
|
}
|
|
623
594
|
get isResolved() {
|
|
624
|
-
return this._track(), (this.flags &
|
|
595
|
+
return this._track(), (this.flags & g) !== 0;
|
|
625
596
|
}
|
|
626
597
|
invalidate() {
|
|
627
|
-
this._markDirty()
|
|
598
|
+
this._markDirty();
|
|
628
599
|
}
|
|
629
600
|
dispose() {
|
|
630
|
-
if (this.flags &
|
|
601
|
+
if (this.flags & P) return;
|
|
631
602
|
const t = this._links;
|
|
632
|
-
if (t !==
|
|
603
|
+
if (t !== _) {
|
|
633
604
|
for (let e = 0, s = t.length; e < s; e++)
|
|
634
605
|
t[e].unsub?.();
|
|
635
|
-
|
|
606
|
+
p.release(t), this._links = _;
|
|
636
607
|
}
|
|
637
|
-
this._subscribers.length = 0, this.flags =
|
|
608
|
+
this._subscribers.length = 0, this.flags = P | T | F, this._error = null, this._value = void 0;
|
|
638
609
|
}
|
|
639
610
|
addDependency(t) {
|
|
640
611
|
if (t._lastSeenEpoch !== this._trackEpoch) {
|
|
@@ -642,151 +613,146 @@ class Z extends $ {
|
|
|
642
613
|
const e = this._trackLinks[this._trackCount];
|
|
643
614
|
e.node = t, e.version = t.version;
|
|
644
615
|
} else
|
|
645
|
-
this._trackLinks.push(new
|
|
616
|
+
this._trackLinks.push(new x(t, t.version));
|
|
646
617
|
this._trackCount++;
|
|
647
618
|
}
|
|
648
619
|
}
|
|
649
|
-
_commitDeps(t) {
|
|
650
|
-
this._trackLinks.length = this._trackCount, ht(this._trackLinks, t, this), this._links = this._trackLinks;
|
|
651
|
-
}
|
|
652
|
-
_updateErrorDepCount() {
|
|
653
|
-
let t = 0;
|
|
654
|
-
const e = this._links;
|
|
655
|
-
for (let s = 0, n = e.length; s < n; s++) {
|
|
656
|
-
const r = e[s]?.node;
|
|
657
|
-
r && r.flags & o.HAS_ERROR && t++;
|
|
658
|
-
}
|
|
659
|
-
this._errorDepCount = t;
|
|
660
|
-
}
|
|
661
620
|
_recompute() {
|
|
662
|
-
if (this.flags &
|
|
663
|
-
this.flags |=
|
|
621
|
+
if (this.flags & y) return;
|
|
622
|
+
this.flags |= y;
|
|
664
623
|
const t = this._links;
|
|
665
|
-
this._trackEpoch =
|
|
624
|
+
this._trackEpoch = V(), this._trackLinks = p.acquire(), this._trackCount = 0;
|
|
666
625
|
let e = !1;
|
|
667
626
|
try {
|
|
668
|
-
const s =
|
|
669
|
-
this.
|
|
627
|
+
const s = a.run(this, this._fn);
|
|
628
|
+
this._trackLinks.length = this._trackCount, Q(this._trackLinks, t, this), this._links = this._trackLinks, e = !0, tt(s) ? this._handleAsyncComputation(s) : this._finalizeResolution(s);
|
|
670
629
|
} catch (s) {
|
|
671
630
|
if (!e)
|
|
672
631
|
try {
|
|
673
|
-
this.
|
|
632
|
+
this._trackLinks.length = this._trackCount, Q(this._trackLinks, t, this), this._links = this._trackLinks, e = !0;
|
|
674
633
|
} catch (n) {
|
|
675
|
-
|
|
634
|
+
u && console.warn("[atom-effect] _commitDeps failed during error recovery:", n);
|
|
676
635
|
}
|
|
677
|
-
this._handleError(s,
|
|
636
|
+
this._handleError(s, c.COMPUTED_COMPUTATION_FAILED, !0);
|
|
678
637
|
} finally {
|
|
679
|
-
e && t !==
|
|
638
|
+
e && t !== _ ? p.release(t) : e || p.release(this._trackLinks), this._trackEpoch = -1, this._trackLinks = _, this._trackCount = 0, this.flags &= ~y;
|
|
680
639
|
}
|
|
681
640
|
}
|
|
682
641
|
_handleAsyncComputation(t) {
|
|
683
|
-
this.flags = (this.flags |
|
|
642
|
+
this.flags = (this.flags | R) & -217, this._notifySubscribers(void 0, void 0), this._asyncStartAggregateVersion = this._captureVersionSnapshot(), this._asyncRetryCount = 0, this._promiseId = (this._promiseId + 1) % Et;
|
|
684
643
|
const e = this._promiseId;
|
|
685
644
|
t.then(
|
|
686
645
|
(s) => {
|
|
687
646
|
if (e === this._promiseId) {
|
|
688
647
|
if (this._captureVersionSnapshot() !== this._asyncStartAggregateVersion)
|
|
689
648
|
return this._asyncRetryCount++ < this._maxAsyncRetries ? this._markDirty() : this._handleError(
|
|
690
|
-
new
|
|
649
|
+
new d(
|
|
691
650
|
`Async drift threshold exceeded after ${this._maxAsyncRetries} retries.`
|
|
692
651
|
),
|
|
693
|
-
|
|
652
|
+
c.COMPUTED_ASYNC_COMPUTATION_FAILED
|
|
694
653
|
);
|
|
695
654
|
this._finalizeResolution(s), this._notifySubscribers(s, void 0);
|
|
696
655
|
}
|
|
697
656
|
},
|
|
698
|
-
(s) => e === this._promiseId && this._handleError(s,
|
|
657
|
+
(s) => e === this._promiseId && this._handleError(s, c.COMPUTED_ASYNC_COMPUTATION_FAILED)
|
|
699
658
|
);
|
|
700
659
|
}
|
|
701
660
|
_captureVersionSnapshot() {
|
|
702
661
|
let t = 0;
|
|
703
662
|
const e = this._links;
|
|
704
663
|
for (let s = 0, n = e.length; s < n; s++)
|
|
705
|
-
t
|
|
664
|
+
t += e[s].node.version;
|
|
706
665
|
return t;
|
|
707
666
|
}
|
|
708
667
|
_handleError(t, e, s = !1) {
|
|
709
|
-
const n =
|
|
710
|
-
if (!s && !(this.flags &
|
|
668
|
+
const n = b(t, d, e);
|
|
669
|
+
if (!s && !(this.flags & A) && (this.version = B(this.version)), this._error = n, this.flags = this.flags & -121 | A | m, this._onError)
|
|
711
670
|
try {
|
|
712
671
|
this._onError(n);
|
|
713
672
|
} catch (r) {
|
|
714
|
-
console.error(
|
|
673
|
+
console.error(c.CALLBACK_ERROR_IN_ERROR_HANDLER, r);
|
|
715
674
|
}
|
|
716
675
|
if (s) throw n;
|
|
717
676
|
this._notifySubscribers(void 0, void 0);
|
|
718
677
|
}
|
|
719
678
|
_finalizeResolution(t) {
|
|
720
|
-
(!(this.flags &
|
|
679
|
+
(!(this.flags & g) || !this._equal(this._value, t)) && (this.version = B(this.version)), this._value = t, this._error = null, this.flags = (this.flags | g) & -697;
|
|
721
680
|
}
|
|
722
681
|
execute() {
|
|
723
682
|
this._markDirty();
|
|
724
683
|
}
|
|
725
684
|
/** @internal */
|
|
726
685
|
_markDirty() {
|
|
727
|
-
this.flags & (
|
|
686
|
+
this.flags & (y | T) || (this.flags |= T, this._notifySubscribers(void 0, void 0));
|
|
728
687
|
}
|
|
729
688
|
}
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
return new Z(i, t);
|
|
689
|
+
function Ot(i, t = {}) {
|
|
690
|
+
return new It(i, t);
|
|
733
691
|
}
|
|
734
|
-
class
|
|
692
|
+
class gt extends W {
|
|
735
693
|
constructor(t, e = {}) {
|
|
736
|
-
super(), this._cleanup = null, this._links =
|
|
737
|
-
const s = Number.isFinite(this._maxExecutions), n = s ? Math.min(this._maxExecutions + 1, I.MAX_EXECUTIONS_PER_SECOND + 1) : 0;
|
|
738
|
-
this._historyCapacity = n, this._history = _ && s && n > 0 ? new Array(n).fill(0) : null, g.attachDebugInfo(this, "effect", this.id);
|
|
694
|
+
super(), this._cleanup = null, this._links = _, this._nextLinks = null, this._prevLinks = null, this._currentEpoch = -1, this._lastFlushEpoch = -1, this._executionsInEpoch = 0, this._executionCount = 0, this._windowStart = 0, this._windowCount = 0, this._execId = 0, this._fn = t, this._onError = e.onError ?? null, this._sync = e.sync ?? !1, this._maxExecutions = e.maxExecutionsPerSecond ?? S.MAX_EXECUTIONS_PER_SECOND, this._maxExecutionsPerFlush = e.maxExecutionsPerFlush ?? S.MAX_EXECUTIONS_PER_EFFECT, this._trackModifications = e.trackModifications ?? !1, D.attachDebugInfo(this, "effect", this.id);
|
|
739
695
|
}
|
|
740
696
|
run() {
|
|
741
|
-
if (this.flags &
|
|
742
|
-
throw new
|
|
697
|
+
if (this.flags & l.DISPOSED)
|
|
698
|
+
throw new I(c.EFFECT_DISPOSED);
|
|
743
699
|
this.execute(!0);
|
|
744
700
|
}
|
|
745
701
|
dispose() {
|
|
746
|
-
this.flags &
|
|
702
|
+
this.flags & l.DISPOSED || (this.flags |= l.DISPOSED, this._execCleanup(), this._unsubLinks(this._links), this._links !== _ && p.release(this._links), this._links = _, this._executeTask = void 0);
|
|
747
703
|
}
|
|
748
704
|
addDependency(t) {
|
|
749
|
-
if (!(this.flags &
|
|
705
|
+
if (!(this.flags & l.EXECUTING) || t._lastSeenEpoch === this._currentEpoch) return;
|
|
750
706
|
t._lastSeenEpoch = this._currentEpoch;
|
|
751
|
-
const e = this._nextLinks;
|
|
752
|
-
if (
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
707
|
+
const e = this._nextLinks, s = this._prevLinks;
|
|
708
|
+
if (s)
|
|
709
|
+
for (let n = 0, r = s.length; n < r; n++) {
|
|
710
|
+
const o = s[n];
|
|
711
|
+
if (o && o.node === t && o.unsub) {
|
|
712
|
+
e.push(new x(t, t.version, o.unsub)), o.unsub = void 0;
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
756
716
|
try {
|
|
757
|
-
const
|
|
758
|
-
if (this._trackModifications && this.flags &
|
|
759
|
-
this._executeTask || (this._executeTask = () => this.execute()),
|
|
717
|
+
const n = t.subscribe(() => {
|
|
718
|
+
if (this._trackModifications && this.flags & l.EXECUTING && (t._modifiedAtEpoch = this._currentEpoch), this._sync) return this.execute();
|
|
719
|
+
this._executeTask || (this._executeTask = () => this.execute()), N.schedule(this._executeTask);
|
|
760
720
|
});
|
|
761
|
-
e.push(new
|
|
762
|
-
} catch (
|
|
763
|
-
const
|
|
764
|
-
if (console.error(
|
|
721
|
+
e.push(new x(t, t.version, n));
|
|
722
|
+
} catch (n) {
|
|
723
|
+
const r = b(n, I, c.EFFECT_EXECUTION_FAILED);
|
|
724
|
+
if (console.error(r), this._onError)
|
|
765
725
|
try {
|
|
766
|
-
this._onError(
|
|
726
|
+
this._onError(r);
|
|
767
727
|
} catch {
|
|
768
728
|
}
|
|
769
|
-
e.push(new
|
|
729
|
+
e.push(new x(t, t.version, void 0));
|
|
770
730
|
}
|
|
771
731
|
}
|
|
772
732
|
/**
|
|
773
733
|
* Executes effect with tracking.
|
|
774
734
|
*/
|
|
775
735
|
execute(t = !1) {
|
|
776
|
-
if (this.flags & (
|
|
777
|
-
this._checkInfiniteLoops(), this.flags |=
|
|
736
|
+
if (this.flags & (l.DISPOSED | l.EXECUTING) || !t && this._links.length > 0 && !this._isDirty()) return;
|
|
737
|
+
this._checkInfiniteLoops(), this.flags |= l.EXECUTING, this._execCleanup();
|
|
778
738
|
const e = this._links;
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
c && (c.node._tempUnsub = c.unsub);
|
|
783
|
-
}
|
|
784
|
-
const s = D.acquire();
|
|
785
|
-
this._nextLinks = s, this._currentEpoch = J();
|
|
739
|
+
this._prevLinks = e !== _ ? e : null;
|
|
740
|
+
const s = p.acquire();
|
|
741
|
+
this._nextLinks = s, this._currentEpoch = V();
|
|
786
742
|
let n = !1;
|
|
787
743
|
try {
|
|
788
|
-
const r =
|
|
789
|
-
this._links = s, n = !0, this.
|
|
744
|
+
const r = a.run(this, this._fn);
|
|
745
|
+
if (this._links = s, n = !0, this._trackModifications && D.enabled) {
|
|
746
|
+
const o = this._currentEpoch;
|
|
747
|
+
for (let h = 0, f = s.length; h < f; h++) {
|
|
748
|
+
const C = s[h].node;
|
|
749
|
+
C._modifiedAtEpoch === o && D.warn(
|
|
750
|
+
!0,
|
|
751
|
+
`Effect is reading a dependency (${D.getDebugName(C) || "unknown"}) that it just modified. Infinite loop may occur`
|
|
752
|
+
);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
tt(r) ? this._handleAsyncResult(r) : this._cleanup = typeof r == "function" ? r : null;
|
|
790
756
|
} catch (r) {
|
|
791
757
|
n = !0, this._handleExecutionError(r), this._cleanup = null;
|
|
792
758
|
} finally {
|
|
@@ -797,12 +763,12 @@ class dt extends q {
|
|
|
797
763
|
const e = ++this._execId;
|
|
798
764
|
t.then(
|
|
799
765
|
(s) => {
|
|
800
|
-
if (e !== this._execId || this.flags &
|
|
766
|
+
if (e !== this._execId || this.flags & l.DISPOSED) {
|
|
801
767
|
if (typeof s == "function")
|
|
802
768
|
try {
|
|
803
769
|
s();
|
|
804
770
|
} catch (n) {
|
|
805
|
-
this._handleExecutionError(n,
|
|
771
|
+
this._handleExecutionError(n, c.EFFECT_CLEANUP_FAILED);
|
|
806
772
|
}
|
|
807
773
|
return;
|
|
808
774
|
}
|
|
@@ -813,41 +779,36 @@ class dt extends q {
|
|
|
813
779
|
}
|
|
814
780
|
_finalizeDependencies(t, e, s) {
|
|
815
781
|
if (this._nextLinks = null, t) {
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
if (t !== l) {
|
|
829
|
-
for (let e = 0, s = t.length; e < s; e++)
|
|
830
|
-
t[e]?.unsub?.();
|
|
831
|
-
D.release(t);
|
|
832
|
-
}
|
|
782
|
+
const n = this._prevLinks;
|
|
783
|
+
if (n)
|
|
784
|
+
for (let r = 0, o = n.length; r < o; r++)
|
|
785
|
+
n[r]?.unsub?.();
|
|
786
|
+
e !== _ && p.release(e);
|
|
787
|
+
} else
|
|
788
|
+
this._unsubLinks(s), p.release(s);
|
|
789
|
+
this._prevLinks = null;
|
|
790
|
+
}
|
|
791
|
+
_unsubLinks(t) {
|
|
792
|
+
for (let e = 0, s = t.length; e < s; e++)
|
|
793
|
+
t[e]?.unsub?.();
|
|
833
794
|
}
|
|
834
795
|
_isDirty() {
|
|
835
|
-
const t = this._links, e =
|
|
836
|
-
|
|
796
|
+
const t = this._links, e = a.current;
|
|
797
|
+
a.current = null;
|
|
837
798
|
try {
|
|
838
799
|
for (let s = 0, n = t.length; s < n; s++) {
|
|
839
|
-
const r = t[s],
|
|
840
|
-
if (
|
|
800
|
+
const r = t[s], o = r.node;
|
|
801
|
+
if (o.flags & q.IS_COMPUTED)
|
|
841
802
|
try {
|
|
842
|
-
|
|
803
|
+
o.value;
|
|
843
804
|
} catch {
|
|
844
|
-
return
|
|
805
|
+
return u && console.warn(`[atom-effect] Dependency #${o.id} threw during dirty check`), !0;
|
|
845
806
|
}
|
|
846
|
-
if (
|
|
807
|
+
if (o.version !== r.version) return !0;
|
|
847
808
|
}
|
|
848
809
|
return !1;
|
|
849
810
|
} finally {
|
|
850
|
-
|
|
811
|
+
a.current = e;
|
|
851
812
|
}
|
|
852
813
|
}
|
|
853
814
|
_execCleanup() {
|
|
@@ -855,94 +816,82 @@ class dt extends q {
|
|
|
855
816
|
try {
|
|
856
817
|
this._cleanup();
|
|
857
818
|
} catch (t) {
|
|
858
|
-
this._handleExecutionError(t,
|
|
819
|
+
this._handleExecutionError(t, c.EFFECT_CLEANUP_FAILED);
|
|
859
820
|
}
|
|
860
821
|
this._cleanup = null;
|
|
861
822
|
}
|
|
862
823
|
}
|
|
863
824
|
_checkInfiniteLoops() {
|
|
864
|
-
const t =
|
|
865
|
-
if (this._lastFlushEpoch !== t && (this._lastFlushEpoch = t, this._executionsInEpoch = 0), ++this._executionsInEpoch > this._maxExecutionsPerFlush && this._throwInfiniteLoopError("per-effect"),
|
|
825
|
+
const t = ht();
|
|
826
|
+
if (this._lastFlushEpoch !== t && (this._lastFlushEpoch = t, this._executionsInEpoch = 0), ++this._executionsInEpoch > this._maxExecutionsPerFlush && this._throwInfiniteLoopError("per-effect"), ct() > S.MAX_EXECUTIONS_PER_FLUSH && this._throwInfiniteLoopError("global"), this._executionCount++, u && Number.isFinite(this._maxExecutions)) {
|
|
866
827
|
const e = Date.now();
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
if (
|
|
870
|
-
const
|
|
871
|
-
|
|
828
|
+
if (e - this._windowStart >= 1e3)
|
|
829
|
+
this._windowStart = e, this._windowCount = 1;
|
|
830
|
+
else if (++this._windowCount > this._maxExecutions) {
|
|
831
|
+
const s = new I(c.EFFECT_FREQUENCY_LIMIT_EXCEEDED);
|
|
832
|
+
throw this.dispose(), this._handleExecutionError(s), s;
|
|
872
833
|
}
|
|
873
834
|
}
|
|
874
835
|
}
|
|
875
836
|
get isDisposed() {
|
|
876
|
-
return (this.flags &
|
|
837
|
+
return (this.flags & l.DISPOSED) !== 0;
|
|
877
838
|
}
|
|
878
839
|
get executionCount() {
|
|
879
840
|
return this._executionCount;
|
|
880
841
|
}
|
|
881
842
|
get isExecuting() {
|
|
882
|
-
return (this.flags &
|
|
843
|
+
return (this.flags & l.EXECUTING) !== 0;
|
|
883
844
|
}
|
|
884
845
|
_throwInfiniteLoopError(t) {
|
|
885
|
-
const e = new
|
|
886
|
-
`Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${
|
|
846
|
+
const e = new I(
|
|
847
|
+
`Infinite loop detected (${t}): effect executed ${this._executionsInEpoch} times in current flush. Total executions in flush: ${H}`
|
|
887
848
|
);
|
|
888
849
|
throw this.dispose(), console.error(e), e;
|
|
889
850
|
}
|
|
890
|
-
_handleExecutionError(t, e =
|
|
891
|
-
const s =
|
|
851
|
+
_handleExecutionError(t, e = c.EFFECT_EXECUTION_FAILED) {
|
|
852
|
+
const s = b(t, I, e);
|
|
892
853
|
if (console.error(s), this._onError)
|
|
893
854
|
try {
|
|
894
855
|
this._onError(s);
|
|
895
856
|
} catch (n) {
|
|
896
|
-
console.error(
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
_checkLoopWarnings() {
|
|
900
|
-
if (this._trackModifications && g.enabled) {
|
|
901
|
-
const t = this._currentEpoch, e = this._links;
|
|
902
|
-
for (let s = 0, n = e.length; s < n; s++) {
|
|
903
|
-
const r = e[s].node;
|
|
904
|
-
r._modifiedAtEpoch === t && g.warn(
|
|
905
|
-
!0,
|
|
906
|
-
`Effect is reading a dependency (${g.getDebugName(r) || "unknown"}) that it just modified. Infinite loop may occur`
|
|
907
|
-
);
|
|
857
|
+
console.error(b(n, I, c.CALLBACK_ERROR_IN_ERROR_HANDLER));
|
|
908
858
|
}
|
|
909
|
-
}
|
|
910
859
|
}
|
|
911
860
|
}
|
|
912
|
-
function
|
|
861
|
+
function yt(i, t = {}) {
|
|
913
862
|
if (typeof i != "function")
|
|
914
|
-
throw new
|
|
915
|
-
const e = new
|
|
863
|
+
throw new I(c.EFFECT_MUST_BE_FUNCTION);
|
|
864
|
+
const e = new gt(i, t);
|
|
916
865
|
return e.execute(), e;
|
|
917
866
|
}
|
|
918
|
-
function
|
|
867
|
+
function At(i) {
|
|
919
868
|
if (typeof i != "function")
|
|
920
869
|
throw new TypeError("Batch callback must be a function");
|
|
921
|
-
|
|
870
|
+
N.startBatch();
|
|
922
871
|
try {
|
|
923
872
|
return i();
|
|
924
873
|
} finally {
|
|
925
|
-
|
|
874
|
+
N.endBatch();
|
|
926
875
|
}
|
|
927
876
|
}
|
|
928
877
|
export {
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
878
|
+
k as AsyncState,
|
|
879
|
+
O as AtomError,
|
|
880
|
+
d as ComputedError,
|
|
932
881
|
et as DEBUG_CONFIG,
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
882
|
+
D as DEBUG_RUNTIME,
|
|
883
|
+
I as EffectError,
|
|
884
|
+
St as POOL_CONFIG,
|
|
885
|
+
S as SCHEDULER_CONFIG,
|
|
886
|
+
L as SchedulerError,
|
|
887
|
+
Ct as atom,
|
|
888
|
+
At as batch,
|
|
889
|
+
Ot as computed,
|
|
890
|
+
yt as effect,
|
|
942
891
|
at as isAtom,
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
892
|
+
Tt as isComputed,
|
|
893
|
+
bt as isEffect,
|
|
894
|
+
N as scheduler,
|
|
895
|
+
Dt as untracked
|
|
947
896
|
};
|
|
948
897
|
//# sourceMappingURL=index.mjs.map
|