@esportsplus/reactivity 0.43.1 → 0.43.3

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.
@@ -8,8 +8,9 @@ function dispose(value) {
8
8
  }
9
9
  }
10
10
  function move(array, from, to) {
11
- if (from in array) {
12
- array[to] = array[from];
11
+ let value = array[from];
12
+ if (value !== undefined || from in array) {
13
+ array[to] = value;
13
14
  }
14
15
  else {
15
16
  delete array[to];
@@ -58,6 +59,10 @@ function rank(array, fn) {
58
59
  delete array[i];
59
60
  }
60
61
  }
62
+ function stringwise(a, b) {
63
+ let x = `${a}`, y = `${b}`;
64
+ return x < y ? -1 : x > y ? 1 : 0;
65
+ }
61
66
  class ReactiveArray extends Array {
62
67
  _length;
63
68
  batched = null;
@@ -93,8 +98,12 @@ class ReactiveArray extends Array {
93
98
  if (i >= length) {
94
99
  write(this._length, i + 1);
95
100
  }
96
- this.dispatch('set', { index: i, item: value });
97
- this.record({ index: i, item: value, op: 'set' });
101
+ if (this.listeners.set !== undefined) {
102
+ this.dispatch('set', { index: i, item: value });
103
+ }
104
+ if (this.batched !== null) {
105
+ this.record({ index: i, item: value, op: 'set' });
106
+ }
98
107
  dispose(prev);
99
108
  }
100
109
  batch() {
@@ -157,8 +166,12 @@ class ReactiveArray extends Array {
157
166
  clear() {
158
167
  this.dispose();
159
168
  write(this._length, 0);
160
- this.dispatch('clear');
161
- this.record({ op: 'clear' });
169
+ if (this.listeners.clear !== undefined) {
170
+ this.dispatch('clear');
171
+ }
172
+ if (this.batched !== null) {
173
+ this.record({ op: 'clear' });
174
+ }
162
175
  }
163
176
  concat(...items) {
164
177
  let added = [];
@@ -178,8 +191,12 @@ class ReactiveArray extends Array {
178
191
  }
179
192
  if (added.length) {
180
193
  write(this._length, this.length);
181
- this.dispatch('concat', { items: added });
182
- this.record({ items: added, op: 'concat' });
194
+ if (this.listeners.concat !== undefined) {
195
+ this.dispatch('concat', { items: added });
196
+ }
197
+ if (this.batched !== null) {
198
+ this.record({ items: added, op: 'concat' });
199
+ }
183
200
  }
184
201
  return this;
185
202
  }
@@ -223,8 +240,18 @@ class ReactiveArray extends Array {
223
240
  }
224
241
  }
225
242
  dispose() {
226
- while (this.length) {
227
- dispose(super.pop());
243
+ let length;
244
+ while ((length = this.length)) {
245
+ let i = length - 1;
246
+ while (i >= 0 && !isReactiveObject(this[i])) {
247
+ i--;
248
+ }
249
+ if (i !== length - 1) {
250
+ this.length = i + 1;
251
+ }
252
+ if (i >= 0) {
253
+ super.pop().dispose();
254
+ }
228
255
  }
229
256
  write(this._length, 0);
230
257
  }
@@ -279,8 +306,12 @@ class ReactiveArray extends Array {
279
306
  let item = super.pop();
280
307
  dispose(item);
281
308
  write(this._length, this.length);
282
- this.dispatch('pop', { item });
283
- this.record({ item, op: 'pop' });
309
+ if (this.listeners.pop !== undefined) {
310
+ this.dispatch('pop', { item });
311
+ }
312
+ if (this.batched !== null) {
313
+ this.record({ item, op: 'pop' });
314
+ }
284
315
  return item;
285
316
  }
286
317
  push(...items) {
@@ -292,13 +323,17 @@ class ReactiveArray extends Array {
292
323
  this[length++] = items[i];
293
324
  }
294
325
  write(this._length, length);
295
- this.dispatch('push', { items });
296
- this.record({ items, op: 'push' });
326
+ if (this.listeners.push !== undefined) {
327
+ this.dispatch('push', { items });
328
+ }
329
+ if (this.batched !== null) {
330
+ this.record({ items, op: 'push' });
331
+ }
297
332
  return length;
298
333
  }
299
334
  reverse() {
300
335
  for (let lower = 0, upper = this.length - 1; lower < upper; lower++, upper--) {
301
- let value = this[lower], within = lower in this;
336
+ let value = this[lower], within = value !== undefined || lower in this;
302
337
  move(this, upper, lower);
303
338
  if (within) {
304
339
  this[upper] = value;
@@ -307,8 +342,12 @@ class ReactiveArray extends Array {
307
342
  delete this[upper];
308
343
  }
309
344
  }
310
- this.dispatch('reverse');
311
- this.record({ op: 'reverse' });
345
+ if (this.listeners.reverse !== undefined) {
346
+ this.dispatch('reverse');
347
+ }
348
+ if (this.batched !== null) {
349
+ this.record({ op: 'reverse' });
350
+ }
312
351
  return this;
313
352
  }
314
353
  shift() {
@@ -319,8 +358,12 @@ class ReactiveArray extends Array {
319
358
  place(this, 0, 1, []);
320
359
  dispose(item);
321
360
  write(this._length, this.length);
322
- this.dispatch('shift', { item });
323
- this.record({ item, op: 'shift' });
361
+ if (this.listeners.shift !== undefined) {
362
+ this.dispatch('shift', { item });
363
+ }
364
+ if (this.batched !== null) {
365
+ this.record({ item, op: 'shift' });
366
+ }
324
367
  return item;
325
368
  }
326
369
  sort(fn) {
@@ -328,24 +371,34 @@ class ReactiveArray extends Array {
328
371
  rank(this, fn);
329
372
  }
330
373
  else {
331
- let n = this.length, before = new Array(n);
332
- for (let i = 0; i < n; i++) {
333
- before[i] = this[i];
334
- }
335
- rank(this, fn);
336
- let buckets = new Map(), order = new Array(n);
374
+ let compare = fn ?? stringwise, n = this.length, before = new Array(n), order = [], stored = 0;
337
375
  for (let i = 0; i < n; i++) {
338
- let value = before[i], bucket = buckets.get(value);
339
- if (bucket === undefined) {
340
- buckets.set(value, { indices: [i], next: 0 });
376
+ let value = this[i];
377
+ before[i] = value;
378
+ if (value !== undefined) {
379
+ order.push(i);
341
380
  }
342
- else {
343
- bucket.indices.push(i);
381
+ else if (i in this) {
382
+ stored++;
344
383
  }
345
384
  }
346
- for (let i = 0; i < n; i++) {
347
- let bucket = buckets.get(this[i]);
348
- order[i] = bucket.indices[bucket.next++];
385
+ order.sort((a, b) => compare(before[a], before[b]));
386
+ let m = order.length;
387
+ for (let i = 0; i < m; i++) {
388
+ this[i] = before[order[i]];
389
+ }
390
+ if (m < n) {
391
+ for (let i = m, end = m + stored; i < end; i++) {
392
+ this[i] = undefined;
393
+ }
394
+ for (let i = m + stored; i < n; i++) {
395
+ delete this[i];
396
+ }
397
+ for (let i = 0; i < n; i++) {
398
+ if (before[i] === undefined) {
399
+ order.push(i);
400
+ }
401
+ }
349
402
  }
350
403
  this.dispatch('sort', { order });
351
404
  this.record({ op: 'sort', order });
@@ -366,8 +419,12 @@ class ReactiveArray extends Array {
366
419
  for (let i = 0; i < count; i++) {
367
420
  dispose(removed[i]);
368
421
  }
369
- this.dispatch('splice', { deleteCount: count, items, start: resolved });
370
- this.record({ deleteCount: count, items, op: 'splice', start: resolved });
422
+ if (this.listeners.splice !== undefined) {
423
+ this.dispatch('splice', { deleteCount: count, items, start: resolved });
424
+ }
425
+ if (this.batched !== null) {
426
+ this.record({ deleteCount: count, items, op: 'splice', start: resolved });
427
+ }
371
428
  }
372
429
  return removed;
373
430
  }
@@ -378,8 +435,12 @@ class ReactiveArray extends Array {
378
435
  place(this, 0, 0, items);
379
436
  let length = this.length;
380
437
  write(this._length, length);
381
- this.dispatch('unshift', { items });
382
- this.record({ items, op: 'unshift' });
438
+ if (this.listeners.unshift !== undefined) {
439
+ this.dispatch('unshift', { items });
440
+ }
441
+ if (this.batched !== null) {
442
+ this.record({ items, op: 'unshift' });
443
+ }
383
444
  return length;
384
445
  }
385
446
  }
package/build/system.js CHANGED
@@ -239,6 +239,10 @@ function pull(node) {
239
239
  }
240
240
  }
241
241
  }
242
+ if (node.gv === writes) {
243
+ node.state &= ~STATE_NOTIFY_MASK;
244
+ return;
245
+ }
242
246
  let o = observer;
243
247
  observer = null;
244
248
  pulled = node;
@@ -547,7 +551,9 @@ function makeComputed(fn, eager = false) {
547
551
  link(self, observer);
548
552
  }
549
553
  else {
554
+ self.gv = -1;
550
555
  self.height = observer.height + 1;
556
+ self.state |= STATE_DIRTY;
551
557
  insertIntoHeap(self);
552
558
  schedule();
553
559
  }
package/package.json CHANGED
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "type": "module",
43
43
  "types": "build/index.d.ts",
44
- "version": "0.43.1",
44
+ "version": "0.43.3",
45
45
  "scripts": {
46
46
  "agent:bench": "vitest bench --run",
47
47
  "agent:test": "pnpm typecheck && vitest run",
@@ -71,8 +71,11 @@ function dispose(value: unknown) {
71
71
 
72
72
  // One element move of Array.prototype's algorithms: a hole moves as a hole
73
73
  function move<T>(array: T[], from: number, to: number) {
74
- if (from in array) {
75
- array[to] = array[from];
74
+ let value = array[from];
75
+
76
+ // Only an undefined read can be a hole
77
+ if (value !== undefined || from in array) {
78
+ array[to] = value;
76
79
  }
77
80
  else {
78
81
  // oxlint-disable-next-line typescript/no-array-delete -- a hole must move as a hole, as the builtin does
@@ -143,6 +146,16 @@ function rank<T>(array: T[], fn?: (a: T, b: T) => number) {
143
146
  }
144
147
  }
145
148
 
149
+ // Array.prototype.sort's comparison when none is given: the values' string forms, by code unit
150
+ function stringwise(a: unknown, b: unknown) {
151
+ // oxlint-disable-next-line typescript/restrict-template-expressions -- ToString as the builtin applies it (a symbol throws, String() would not)
152
+ let x = `${a}`,
153
+ // oxlint-disable-next-line typescript/restrict-template-expressions -- as above
154
+ y = `${b}`;
155
+
156
+ return x < y ? -1 : x > y ? 1 : 0;
157
+ }
158
+
146
159
  // Derived arrays (splice removals, map/filter/slice) must be plain Arrays: species-creating a
147
160
  // ReactiveArray runs the reactive constructor per call and seeds _length from the length argument.
148
161
  // V8 runs Array.prototype mutators on its generic path for an instance whose prototype is not
@@ -205,8 +218,13 @@ class ReactiveArray<T> extends Array<T> {
205
218
  write(this._length, i + 1);
206
219
  }
207
220
 
208
- this.dispatch('set', { index: i, item: value });
209
- this.record({ index: i, item: value, op: 'set' });
221
+ if (this.listeners.set !== undefined) {
222
+ this.dispatch('set', { index: i, item: value });
223
+ }
224
+
225
+ if (this.batched !== null) {
226
+ this.record({ index: i, item: value, op: 'set' });
227
+ }
210
228
  dispose(prev);
211
229
  }
212
230
 
@@ -293,8 +311,13 @@ class ReactiveArray<T> extends Array<T> {
293
311
  clear() {
294
312
  this.dispose();
295
313
  write(this._length, 0);
296
- this.dispatch('clear');
297
- this.record({ op: 'clear' });
314
+ if (this.listeners.clear !== undefined) {
315
+ this.dispatch('clear');
316
+ }
317
+
318
+ if (this.batched !== null) {
319
+ this.record({ op: 'clear' });
320
+ }
298
321
  }
299
322
 
300
323
  concat(...items: ConcatArray<T>[]): ReactiveArray<T>;
@@ -321,8 +344,13 @@ class ReactiveArray<T> extends Array<T> {
321
344
 
322
345
  if (added.length) {
323
346
  write(this._length, this.length);
324
- this.dispatch('concat', { items: added });
325
- this.record({ items: added, op: 'concat' });
347
+ if (this.listeners.concat !== undefined) {
348
+ this.dispatch('concat', { items: added });
349
+ }
350
+
351
+ if (this.batched !== null) {
352
+ this.record({ items: added, op: 'concat' });
353
+ }
326
354
  }
327
355
 
328
356
  return this;
@@ -381,8 +409,24 @@ class ReactiveArray<T> extends Array<T> {
381
409
  }
382
410
 
383
411
  dispose() {
384
- while (this.length) {
385
- dispose(super.pop());
412
+ let length;
413
+
414
+ while ((length = this.length)) {
415
+ let i = length - 1;
416
+
417
+ // Only a reactive object has anything to dispose: the plain values after the last one go in one
418
+ // truncation, so it still sees the array shortened past it
419
+ while (i >= 0 && !isReactiveObject(this[i])) {
420
+ i--;
421
+ }
422
+
423
+ if (i !== length - 1) {
424
+ this.length = i + 1;
425
+ }
426
+
427
+ if (i >= 0) {
428
+ (super.pop() as { dispose(): void }).dispose();
429
+ }
386
430
  }
387
431
 
388
432
  write(this._length, 0);
@@ -458,8 +502,13 @@ class ReactiveArray<T> extends Array<T> {
458
502
 
459
503
  dispose(item);
460
504
  write(this._length, this.length);
461
- this.dispatch('pop', { item });
462
- this.record({ item, op: 'pop' });
505
+ if (this.listeners.pop !== undefined) {
506
+ this.dispatch('pop', { item });
507
+ }
508
+
509
+ if (this.batched !== null) {
510
+ this.record({ item, op: 'pop' });
511
+ }
463
512
 
464
513
  return item;
465
514
  }
@@ -477,8 +526,13 @@ class ReactiveArray<T> extends Array<T> {
477
526
 
478
527
  write(this._length, length);
479
528
 
480
- this.dispatch('push', { items });
481
- this.record({ items, op: 'push' });
529
+ if (this.listeners.push !== undefined) {
530
+ this.dispatch('push', { items });
531
+ }
532
+
533
+ if (this.batched !== null) {
534
+ this.record({ items, op: 'push' });
535
+ }
482
536
 
483
537
  return length;
484
538
  }
@@ -486,7 +540,7 @@ class ReactiveArray<T> extends Array<T> {
486
540
  reverse() {
487
541
  for (let lower = 0, upper = this.length - 1; lower < upper; lower++, upper--) {
488
542
  let value = this[lower],
489
- within = lower in this;
543
+ within = value !== undefined || lower in this;
490
544
 
491
545
  move(this, upper, lower);
492
546
 
@@ -499,8 +553,13 @@ class ReactiveArray<T> extends Array<T> {
499
553
  }
500
554
  }
501
555
 
502
- this.dispatch('reverse');
503
- this.record({ op: 'reverse' });
556
+ if (this.listeners.reverse !== undefined) {
557
+ this.dispatch('reverse');
558
+ }
559
+
560
+ if (this.batched !== null) {
561
+ this.record({ op: 'reverse' });
562
+ }
504
563
 
505
564
  return this;
506
565
  }
@@ -515,8 +574,13 @@ class ReactiveArray<T> extends Array<T> {
515
574
  place(this, 0, 1, []);
516
575
  dispose(item);
517
576
  write(this._length, this.length);
518
- this.dispatch('shift', { item });
519
- this.record({ item, op: 'shift' });
577
+ if (this.listeners.shift !== undefined) {
578
+ this.dispatch('shift', { item });
579
+ }
580
+
581
+ if (this.batched !== null) {
582
+ this.record({ item, op: 'shift' });
583
+ }
520
584
 
521
585
  return item;
522
586
  }
@@ -526,36 +590,53 @@ class ReactiveArray<T> extends Array<T> {
526
590
  rank(this, fn);
527
591
  }
528
592
  else {
529
- let n = this.length,
593
+ let compare = fn ?? stringwise,
594
+ n = this.length,
530
595
  // oxlint-disable-next-line unicorn/no-new-array -- preallocated to n, every slot is written next
531
- before = new Array(n) as T[];
596
+ before = new Array(n) as T[],
597
+ order: number[] = [],
598
+ stored = 0;
532
599
 
600
+ // The indices of the values sort, so the order comes out of the sort itself; undefineds and holes are
601
+ // never compared, as the builtin never compares them
533
602
  for (let i = 0; i < n; i++) {
534
- before[i] = this[i];
603
+ let value = this[i];
604
+
605
+ before[i] = value;
606
+
607
+ if (value !== undefined) {
608
+ order.push(i);
609
+ }
610
+ else if (i in this) {
611
+ stored++;
612
+ }
535
613
  }
536
614
 
537
- rank(this, fn);
615
+ order.sort((a, b) => compare(before[a], before[b]));
538
616
 
539
- let buckets = new Map<T, { indices: number[]; next: number }>(),
540
- // oxlint-disable-next-line unicorn/no-new-array -- preallocated to n, every slot is written next
541
- order = new Array(n);
617
+ let m = order.length;
542
618
 
543
- for (let i = 0; i < n; i++) {
544
- let value = before[i],
545
- bucket = buckets.get(value);
619
+ for (let i = 0; i < m; i++) {
620
+ this[i] = before[order[i]];
621
+ }
546
622
 
547
- if (bucket === undefined) {
548
- buckets.set(value, { indices: [i], next: 0 });
549
- }
550
- else {
551
- bucket.indices.push(i);
623
+ // Undefineds then holes end the array, as the builtin leaves them. The order does not tell a hole from an
624
+ // undefined: their slots report the indices both came from, in index order.
625
+ if (m < n) {
626
+ for (let i = m, end = m + stored; i < end; i++) {
627
+ this[i] = undefined as T;
552
628
  }
553
- }
554
629
 
555
- for (let i = 0; i < n; i++) {
556
- let bucket = buckets.get(this[i])!;
630
+ for (let i = m + stored; i < n; i++) {
631
+ // oxlint-disable-next-line typescript/no-array-delete -- holes sort to the end, as the builtin leaves them
632
+ delete this[i];
633
+ }
557
634
 
558
- order[i] = bucket.indices[bucket.next++];
635
+ for (let i = 0; i < n; i++) {
636
+ if (before[i] === undefined) {
637
+ order.push(i);
638
+ }
639
+ }
559
640
  }
560
641
 
561
642
  this.dispatch('sort', { order });
@@ -590,8 +671,13 @@ class ReactiveArray<T> extends Array<T> {
590
671
  dispose(removed[i]);
591
672
  }
592
673
 
593
- this.dispatch('splice', { deleteCount: count, items, start: resolved });
594
- this.record({ deleteCount: count, items, op: 'splice', start: resolved });
674
+ if (this.listeners.splice !== undefined) {
675
+ this.dispatch('splice', { deleteCount: count, items, start: resolved });
676
+ }
677
+
678
+ if (this.batched !== null) {
679
+ this.record({ deleteCount: count, items, op: 'splice', start: resolved });
680
+ }
595
681
  }
596
682
 
597
683
  return removed;
@@ -607,8 +693,13 @@ class ReactiveArray<T> extends Array<T> {
607
693
  let length = this.length;
608
694
 
609
695
  write(this._length, length);
610
- this.dispatch('unshift', { items });
611
- this.record({ items, op: 'unshift' });
696
+ if (this.listeners.unshift !== undefined) {
697
+ this.dispatch('unshift', { items });
698
+ }
699
+
700
+ if (this.batched !== null) {
701
+ this.record({ items, op: 'unshift' });
702
+ }
612
703
 
613
704
  return length;
614
705
  }
package/src/system.ts CHANGED
@@ -384,6 +384,12 @@ function pull<T>(node: Computed<T>): void {
384
384
  }
385
385
  }
386
386
 
387
+ // Validated since the last write: update() would only clear the notify bits
388
+ if (node.gv === writes) {
389
+ node.state &= ~STATE_NOTIFY_MASK;
390
+ return;
391
+ }
392
+
387
393
  let o = observer;
388
394
 
389
395
  // Cleared rather than restored: an outer pull then merely fails to skip, which is only a re-run
@@ -817,7 +823,7 @@ function makeComputed<T>(fn: Computed<T>['fn'], eager: boolean = false): Compute
817
823
  else if (eager) {
818
824
  // computed() must know fn's return type to pick sync vs async. This probe runs BEFORE
819
825
  // link(), so self has no subs yet — recompute's propagate is a no-op and cannot
820
- // re-run the parent. Deferring here (as effect() still does) would leave value unset.
826
+ // re-run the parent. Deferring here (as effect() still does) would leave the type unknown until it ran.
821
827
  self.height = observer.height + 1;
822
828
  recompute(self);
823
829
  link(self, observer);
@@ -825,8 +831,11 @@ function makeComputed<T>(fn: Computed<T>['fn'], eager: boolean = false): Compute
825
831
  else {
826
832
  // Deferred, so not linked: the parent has not read a value, and the creation run setting one later is
827
833
  // no change it depends on. Linked, that run would re-run the parent, whose re-run creates another node,
828
- // looping. A parent that reads the node links through read().
834
+ // looping. A parent that reads the node links through read(), whose pull must then run it: a node
835
+ // that never ran has no notify bits and a write version that can match, so mark it dirty and unversioned.
836
+ self.gv = -1;
829
837
  self.height = observer.height + 1;
838
+ self.state |= STATE_DIRTY;
830
839
  insertIntoHeap(self);
831
840
  schedule();
832
841
  }