reflex-quant 1.2.0 → 1.3.1

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.
@@ -1,7 +1,7 @@
1
- /* reflex-quant 1.2.0 | MIT */
1
+ /* reflex-quant 1.3.1 | MIT */
2
2
 
3
3
  // src/graph.ts
4
- var UNREAD = -1, SKIPPED = -2, SETTLE = 536870911, SCAN = 8, TAIL = 2, KEEP = 256, TRIM = 512;
4
+ var UNREAD = -1, SKIPPED = -2, EXPIRED = -3, SETTLE = 536870911, SCAN = 8, TAIL = 2, KEEP = 256, TRIM = 512;
5
5
  function createNode(fn, value, flags) {
6
6
  return {
7
7
  flags,
@@ -48,28 +48,39 @@ function trim() {
48
48
  stack.length > TRIM && top === 0 && (stack.length = KEEP);
49
49
  }
50
50
  function write(s, value) {
51
- if (!compare(s.value, value)) {
52
- s.value = value, s.ver = s.ver + 1 & 1073741823 /* Mask */;
53
- for (let e = s.firstSub; e !== null; e = e.nextSub) {
54
- let n = e.to, f = n.flags;
55
- if ((f & 4 /* Busy */) !== 0) {
56
- if ((f & 1 /* Stale */) === 0)
57
- if (n.flags = f | 1 /* Stale */, (f & 16 /* Watch */) !== 0) wake(n);
58
- else {
59
- let subs = n.firstSub;
60
- subs !== null && mark(subs);
61
- }
62
- } else if ((f & 16 /* Watch */) !== 0)
63
- n.flags = f | 2 /* Dirty */ | 32 /* Woken */, wake(n);
64
- else if ((f & 1 /* Stale */) !== 0)
65
- n.flags = f | 2 /* Dirty */;
51
+ let old = s.value;
52
+ if (old === value ? value !== 0 || 1 / old === 1 / value : old !== old && value !== value)
53
+ return !1;
54
+ s.value = value;
55
+ let e = s.firstSub;
56
+ if (e === null) return !1;
57
+ for (bump(s); e !== null; e = e.nextSub) {
58
+ let n = e.to, f = n.flags;
59
+ if ((f & 5) === 4 /* Busy */)
60
+ if (n.flags = f | 1 /* Stale */, (f & 16 /* Watch */) !== 0) wake(n);
66
61
  else {
67
- n.flags = f | 2 /* Dirty */ | 1 /* Stale */;
68
62
  let subs = n.firstSub;
69
63
  subs !== null && mark(subs);
70
64
  }
65
+ else if ((f & 16 /* Watch */) !== 0)
66
+ n.flags = f | 2 /* Dirty */ | 32 /* Woken */, wake(n);
67
+ else if ((f & 1 /* Stale */) !== 0)
68
+ n.flags = f | 2 /* Dirty */;
69
+ else {
70
+ n.flags = f | 2 /* Dirty */ | 1 /* Stale */;
71
+ let subs = n.firstSub;
72
+ subs !== null && mark(subs);
71
73
  }
72
74
  }
75
+ return !0;
76
+ }
77
+ function bump(n) {
78
+ let version = n.ver + 1 & 1073741823 /* Mask */;
79
+ version === 0 && expire(n), n.ver = version;
80
+ }
81
+ function expire(n) {
82
+ for (let e = n.firstSub; e !== null; e = e.nextSub)
83
+ e.ver >= 0 && (e.ver = EXPIRED);
73
84
  }
74
85
  function mark(first) {
75
86
  let base = top, e = first;
@@ -97,15 +108,34 @@ function mark(first) {
97
108
  }
98
109
  }
99
110
  function read(n) {
100
- let value = peek(n), c = active;
101
- if (c !== null) {
102
- let last = cursor, next = last === null ? c.firstDep : last.nextDep;
103
- next !== null && next.from === n ? (cursor = next, next.ver = n.ver) : last !== null ? last.from !== n && miss(c, n, last, next) : next !== null ? miss(c, n, last, next) : link(c, n, null, null);
104
- }
105
- return value;
111
+ (n.flags & 47 /* Pull */) !== 0 && pull(n);
112
+ let c = active;
113
+ if (c === null) return n.value;
114
+ let last = cursor, next = last === null ? c.firstDep : last.nextDep;
115
+ if (next !== null && next.from === n)
116
+ return cursor = next, next.ver = n.ver, n.value;
117
+ if (last !== null) {
118
+ if (last.from === n)
119
+ return n.value;
120
+ miss(c, n, last, next);
121
+ } else next !== null ? miss(c, n, last, next) : attach(c, n, null, null);
122
+ return n.value;
123
+ }
124
+ function track(n) {
125
+ let c = active;
126
+ if (c === null) return n.value;
127
+ let last = cursor, next = last === null ? c.firstDep : last.nextDep;
128
+ if (next !== null && next.from === n)
129
+ return cursor = next, next.ver = n.ver, n.value;
130
+ if (last !== null) {
131
+ if (last.from === n)
132
+ return n.value;
133
+ miss(c, n, last, next);
134
+ } else next !== null ? miss(c, n, last, next) : attach(c, n, null, null);
135
+ return n.value;
106
136
  }
107
137
  function peek(n) {
108
- return (n.flags & 39 /* Pull */) !== 0 && pull(n), n.value;
138
+ return (n.flags & 47 /* Pull */) !== 0 && pull(n), n.value;
109
139
  }
110
140
  function pull(root) {
111
141
  let f = root.flags;
@@ -114,26 +144,32 @@ function pull(root) {
114
144
  verify(root, f);
115
145
  }
116
146
  function verify(root, f) {
117
- root.flags = f & -2 | 32 /* Checking */;
147
+ root.flags = f & -10 | 32 /* Checking */;
118
148
  let base = top, n = root, e = root.firstDep;
119
149
  try {
120
150
  for (; ; ) {
121
151
  settled: {
122
152
  for (; e !== null; ) {
123
153
  let s = e.from, sf = s.flags;
124
- if ((sf & 39 /* Pull */) !== 0) {
125
- if ((sf & 36 /* Active */) !== 0) throw new Error("Cycle detected");
154
+ if ((sf & 47 /* Pull */) === 0) {
155
+ if (s.ver === e.ver) {
156
+ e = e.nextDep;
157
+ continue;
158
+ }
159
+ } else {
160
+ if ((sf & 36 /* Active */) !== 0)
161
+ throw new Error("Cycle detected");
126
162
  if ((sf & 2 /* Dirty */) === 0) {
127
- s.flags = sf & -2 | 32 /* Checking */, stack[top++] = e, n = s, e = s.firstDep;
163
+ s.flags = sf & -10 | 32 /* Checking */, stack[top++] = e, n = s, e = s.firstDep;
164
+ continue;
165
+ }
166
+ if (exec(s), s.ver === e.ver) {
167
+ e = e.nextDep;
128
168
  continue;
129
169
  }
130
- exec(s);
131
- }
132
- if (s.ver !== e.ver) {
133
- n.fn !== null && exec(n);
134
- break settled;
135
170
  }
136
- e = e.nextDep;
171
+ n.fn !== null && exec(n);
172
+ break settled;
137
173
  }
138
174
  n.flags &= -33;
139
175
  }
@@ -151,26 +187,47 @@ function verify(root, f) {
151
187
  }
152
188
  }
153
189
  } catch (error) {
154
- for (n.fn !== null && (n.flags = (n.flags | 2 /* Dirty */) & -33); top > base; ) {
155
- let p = stack[--top].to;
156
- p.fn !== null && (p.flags = (p.flags | 2 /* Dirty */) & -33), stack[top] = null;
190
+ for (; ; ) {
191
+ if (n.fn !== null) {
192
+ let flags = n.flags & -42;
193
+ n.flags = (flags & 2 /* Dirty */) === 0 ? flags | 8 /* Unverified */ : flags;
194
+ }
195
+ if (top === base) break;
196
+ n = stack[--top].to, stack[top] = null;
157
197
  }
158
198
  throw trim(), error;
159
199
  }
160
200
  }
161
- function check(w) {
162
- let tentative = w.gen + 1 & 1073741823 /* Mask */, e = w.firstDep;
163
- if ((w.flags & 2 /* Dirty */) === 0)
164
- for (; e !== null; e = e.nextDep) {
165
- let s = e.from;
166
- if ((s.flags & 39 /* Pull */) !== 0 && e.gen !== tentative && pull(s), s.ver !== e.ver) {
201
+ function check(w, flags) {
202
+ let tentative = w.gen + 1 & 1073741823 /* Mask */;
203
+ for (; ; ) {
204
+ let e = w.firstDep;
205
+ if ((flags & 2 /* Dirty */) === 0)
206
+ for (; e !== null; ) {
207
+ let s = e.from;
208
+ if ((s.flags & 47 /* Pull */) === 0) {
209
+ if (s.ver === e.ver) {
210
+ e = e.nextDep;
211
+ continue;
212
+ }
213
+ } else if (pull(s), s.ver === e.ver) {
214
+ e = e.nextDep;
215
+ continue;
216
+ }
167
217
  w.flags |= 2 /* Dirty */, e = e.nextDep;
168
218
  break;
169
219
  }
220
+ for (; e !== null; ) {
221
+ let s = e.from;
222
+ if ((s.flags & 47 /* Pull */) === 0 || e.gen === tentative) {
223
+ e = e.nextDep;
224
+ continue;
225
+ }
226
+ pull(s), e = e.nextDep;
170
227
  }
171
- for (; e !== null; e = e.nextDep) {
172
- let s = e.from;
173
- (s.flags & 39 /* Pull */) !== 0 && e.gen !== tentative && pull(s);
228
+ if (w.fn === null) return 0;
229
+ if (flags = w.flags, (flags & 1 /* Stale */) === 0) return flags;
230
+ w.flags = flags & -2;
174
231
  }
175
232
  }
176
233
  function untracked(fn) {
@@ -184,23 +241,25 @@ function untracked(fn) {
184
241
  }
185
242
  function exec(n) {
186
243
  let prev = active, prevCursor = cursor, base = top;
187
- n.flags = n.flags & -36 | 4 /* Busy */, active = n, cursor = null;
244
+ n.flags = n.flags & -44 | 4 /* Busy */, active = n, cursor = null;
188
245
  let value;
189
246
  try {
190
247
  value = n.fn(n.value);
191
248
  } catch (error) {
192
249
  let last2 = cursor;
193
- throw active = prev, cursor = prevCursor, fail(n, base, last2), error;
250
+ throw active = prev, cursor = prevCursor, n.flags = n.flags & -5 | (n.fn !== null ? 2 /* Dirty */ : 0), fail(n, base, last2), error;
194
251
  }
195
252
  let last = cursor;
196
253
  active = prev, cursor = prevCursor;
197
254
  let f = n.flags;
198
- if (last !== null && last.from.slot === last && unindex(n, base), n.flags = f & -5, n.fn === null) return detach(n);
199
- (last === null ? n.firstDep : last.nextDep) !== null && cutDeps(n, last), n.gen = n.gen + 1 & 1073741823 /* Mask */, (n.gen & SETTLE) === 0 && settleDeps(n), compare(value, n.value) === !1 && (n.value = value, n.ver = n.ver + 1 & 1073741823 /* Mask */);
255
+ if (last !== null && last.from.slot === last && unindex(n, base), n.flags = f & -5, n.fn === null) return disposed(n, value);
256
+ (last === null ? n.firstDep : last.nextDep) !== null && cutDeps(n, last), n.gen = n.gen + 1 & 1073741823 /* Mask */, (n.gen & SETTLE) === 0 && settleDeps(n), compare(value, n.value) === !1 && (n.value = value, bump(n));
257
+ }
258
+ function disposed(n, value) {
259
+ detach(n), (n.flags & 16 /* Watch */) === 0 && compare(value, n.value) === !1 && (n.value = value, bump(n));
200
260
  }
201
261
  function fail(n, base, last) {
202
- if (last !== null && last.from.slot === last && unindex(n, base), n.flags &= -5, n.fn === null) return detach(n);
203
- n.flags |= 2 /* Dirty */;
262
+ if (last !== null && last.from.slot === last && unindex(n, base), n.fn === null) return detach(n);
204
263
  let tentative = n.gen + 1 & 1073741823 /* Mask */;
205
264
  for (let e = last === null ? n.firstDep : last.nextDep; e !== null; ) {
206
265
  let after = e.nextDep;
@@ -215,7 +274,15 @@ function dispose(n) {
215
274
  n.fn !== null && (n.fn = null, (n.flags & 4 /* Busy */) === 0 && detach(n));
216
275
  }
217
276
  function detach(n) {
218
- cutDeps(n, null), n.flags &= -36;
277
+ if ((n.flags & 16 /* Watch */) === 0) cutDeps(n, null);
278
+ else {
279
+ let e = n.firstDep;
280
+ for (n.firstDep = n.lastDep = null; e !== null; ) {
281
+ let next = e.nextDep;
282
+ unlinkSub(e), e.prevDep = e.nextDep = null, e = next;
283
+ }
284
+ }
285
+ n.flags &= -44;
219
286
  }
220
287
  function index(c, next) {
221
288
  let unread = !1;
@@ -234,23 +301,23 @@ function unindex(n, base) {
234
301
  }
235
302
  trim();
236
303
  }
237
- function link(c, s, last, next) {
304
+ function attach(c, s, last, next) {
238
305
  let first = s.firstSub, tail = first !== null ? first.prevSub : null, e = {
239
306
  from: s,
240
- to: c,
241
307
  ver: s.ver,
308
+ nextDep: next,
309
+ to: c,
310
+ nextSub: null,
242
311
  gen: c.gen + 1 & 1073741823 /* Mask */,
243
312
  prevDep: last,
244
- nextDep: next,
245
- prevSub: tail,
246
- nextSub: null
313
+ prevSub: tail
247
314
  };
248
- last !== null ? last.nextDep = e : c.firstDep = e, next !== null ? next.prevDep = e : c.lastDep = e, first !== null ? (tail.nextSub = e, first.prevSub = e) : (s.firstSub = e, e.prevSub = e), cursor = e;
315
+ last !== null ? last.nextDep = e : c.firstDep = e, next !== null ? next.prevDep = e : c.lastDep = e, first !== null ? (tail.nextSub = e, first.prevSub = e) : (s.firstSub = e, e.prevSub = e), cursor = e, (s.flags & 1 /* Stale */) !== 0 && mark(e);
249
316
  }
250
317
  function miss(c, s, last, next) {
251
318
  if (last !== null && last.from.slot === last) return slot(c, s, last, next);
252
319
  let first = s.firstSub;
253
- if (first === null || alien(first, c)) return link(c, s, last, next);
320
+ if (first === null || alien(first, c)) return attach(c, s, last, next);
254
321
  if (next !== null && next.ver === UNREAD)
255
322
  return settle(c, s, last, next, pair(first, c));
256
323
  if (next !== null) {
@@ -275,7 +342,7 @@ function near(c, s, last, next, first) {
275
342
  let p = last !== null ? last.prevDep : null, budget = SCAN;
276
343
  for (; p !== null && p.from !== s && --budget !== 0; ) p = p.prevDep;
277
344
  if (!(p !== null && p.from === s)) {
278
- if (e === null && p === null) return link(c, s, last, next);
345
+ if (e === null && p === null) return attach(c, s, last, next);
279
346
  lookup(c, s, last, next, first);
280
347
  }
281
348
  }
@@ -285,17 +352,22 @@ function skip(c, s, last, next, e) {
285
352
  }
286
353
  function lookup(c, s, last, next, first) {
287
354
  let found = pair(first, c);
288
- found != null && begin(c, found, first, next), settle(c, s, last, next, found);
355
+ if (found != null) {
356
+ if (found.ver === SKIPPED)
357
+ return found.ver = s.ver, cursor = found, moveDep(c, found, last, next);
358
+ begin(c, found, first, next);
359
+ }
360
+ settle(c, s, last, next, found);
289
361
  }
290
362
  function settle(c, s, last, next, found) {
291
- if (found === null) return link(c, s, last, next);
363
+ if (found === null) return attach(c, s, last, next);
292
364
  if (found !== void 0) return take(c, found, s, last, next);
293
365
  index(c, next), slot(c, s, last, next);
294
366
  }
295
367
  function slot(c, s, last, next) {
296
368
  let cell = s.slot;
297
369
  if (cell !== null && cell.to === c) return take(c, cell, s, last, next);
298
- link(c, s, last, next), claim(s, cursor);
370
+ attach(c, s, last, next), claim(s, cursor);
299
371
  }
300
372
  function alien(first, c) {
301
373
  let tail = first.prevSub;
@@ -324,7 +396,7 @@ function take(c, e, s, last, next) {
324
396
  var queue = new Array(KEEP).fill(null), mask = KEEP - 1, head = 0, spare = 0, calm = 0, size = 0, hold = 0;
325
397
  function wake2(w) {
326
398
  let flags = w.flags;
327
- (flags & 192 /* AutoQueued */) === 64 /* Auto */ && (w.flags = flags | 128 /* Queued */, size > mask && grow(), queue[head + size++ & mask] = w);
399
+ (flags & 192 /* AutoQueued */) === 64 /* Auto */ && (size > mask && grow(), queue[head + size++ & mask] = w, w.flags = flags | 128 /* Queued */);
328
400
  }
329
401
  onWake(wake2);
330
402
  function grow() {
@@ -344,14 +416,12 @@ function deliver(w, flags) {
344
416
  w.flags = flags;
345
417
  return;
346
418
  }
347
- if (w.flags = flags & -34, (flags & 1 /* Stale */) !== 0) {
419
+ if (w.flags = flags & -34, (flags & 1 /* Stale */) !== 0)
348
420
  try {
349
- check(w);
421
+ if ((check(w, flags) & 2 /* Dirty */) === 0) return;
350
422
  } catch (error) {
351
423
  throw w.flags |= 1 /* Stale */, error;
352
424
  }
353
- if ((w.flags & 2 /* Dirty */) === 0 || w.fn === null) return;
354
- }
355
425
  let cleanup = w.value;
356
426
  typeof cleanup == "function" && (w.value = void 0, untracked(cleanup), w.fn === null) || exec(w);
357
427
  }
@@ -382,8 +452,10 @@ function drain(failed, first) {
382
452
  for (; size !== 0; taken++) {
383
453
  let queuedWatcher = queue[head];
384
454
  queue[head] = null, head = head + 1 & mask, size--;
455
+ let flags = queuedWatcher.flags & -129;
456
+ queuedWatcher.flags = flags;
385
457
  try {
386
- deliver(queuedWatcher, queuedWatcher.flags & -129);
458
+ deliver(queuedWatcher, flags);
387
459
  } catch (error) {
388
460
  failed || (first = error), failed = !0;
389
461
  }
@@ -439,9 +511,17 @@ function effect(fn) {
439
511
  }
440
512
  return effectNode;
441
513
  }
442
- var get = read, peek2 = peek;
514
+ var read2 = read, get = read2, readSignal = checkedReadSignal;
515
+ function checkedReadSignal(n) {
516
+ if (n.fn !== null)
517
+ throw new TypeError(
518
+ "readSignal() takes a signal; read a computed with read() or compute()"
519
+ );
520
+ return track(n);
521
+ }
522
+ var compute = read2, peek2 = peek;
443
523
  function set(source, value) {
444
- write(source, value), size !== 0 && hold === 0 && drainNow();
524
+ write(source, value) && size !== 0 && hold === 0 && drainNow();
445
525
  }
446
526
  function update(source, fn) {
447
527
  set(
@@ -455,11 +535,14 @@ function dispose2(target) {
455
535
  }
456
536
  export {
457
537
  batch,
538
+ compute,
458
539
  computed,
459
540
  dispose2 as dispose,
460
541
  effect,
461
542
  get,
462
543
  peek2 as peek,
544
+ read2 as read,
545
+ readSignal,
463
546
  set,
464
547
  signal,
465
548
  typeOf,