@esportsplus/reactivity 0.43.0 → 0.43.2
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/build/system.js +4 -1
- package/package.json +1 -1
- package/src/system.ts +10 -3
- package/test/effects.test.ts +105 -0
package/build/system.js
CHANGED
|
@@ -539,17 +539,20 @@ function makeComputed(fn, eager = false) {
|
|
|
539
539
|
if (observer.depsTail === null) {
|
|
540
540
|
self.height = observer.height;
|
|
541
541
|
recompute(self);
|
|
542
|
+
link(self, observer);
|
|
542
543
|
}
|
|
543
544
|
else if (eager) {
|
|
544
545
|
self.height = observer.height + 1;
|
|
545
546
|
recompute(self);
|
|
547
|
+
link(self, observer);
|
|
546
548
|
}
|
|
547
549
|
else {
|
|
550
|
+
self.gv = -1;
|
|
548
551
|
self.height = observer.height + 1;
|
|
552
|
+
self.state |= STATE_DIRTY;
|
|
549
553
|
insertIntoHeap(self);
|
|
550
554
|
schedule();
|
|
551
555
|
}
|
|
552
|
-
link(self, observer);
|
|
553
556
|
own(observer, self);
|
|
554
557
|
}
|
|
555
558
|
else {
|
package/package.json
CHANGED
package/src/system.ts
CHANGED
|
@@ -812,21 +812,28 @@ function makeComputed<T>(fn: Computed<T>['fn'], eager: boolean = false): Compute
|
|
|
812
812
|
if (observer.depsTail === null) {
|
|
813
813
|
self.height = observer.height;
|
|
814
814
|
recompute(self);
|
|
815
|
+
link(self, observer);
|
|
815
816
|
}
|
|
816
817
|
else if (eager) {
|
|
817
818
|
// computed() must know fn's return type to pick sync vs async. This probe runs BEFORE
|
|
818
|
-
// link()
|
|
819
|
-
// re-run the parent. Deferring here (as effect() still does) would leave
|
|
819
|
+
// 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 the type unknown until it ran.
|
|
820
821
|
self.height = observer.height + 1;
|
|
821
822
|
recompute(self);
|
|
823
|
+
link(self, observer);
|
|
822
824
|
}
|
|
823
825
|
else {
|
|
826
|
+
// Deferred, so not linked: the parent has not read a value, and the creation run setting one later is
|
|
827
|
+
// 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(), whose pull must then run it: a node
|
|
829
|
+
// that never ran has no notify bits and a write version that can match, so mark it dirty and unversioned.
|
|
830
|
+
self.gv = -1;
|
|
824
831
|
self.height = observer.height + 1;
|
|
832
|
+
self.state |= STATE_DIRTY;
|
|
825
833
|
insertIntoHeap(self);
|
|
826
834
|
schedule();
|
|
827
835
|
}
|
|
828
836
|
|
|
829
|
-
link(self, observer);
|
|
830
837
|
own(observer, self as Computed<unknown>);
|
|
831
838
|
}
|
|
832
839
|
else {
|
package/test/effects.test.ts
CHANGED
|
@@ -367,3 +367,108 @@ describe('scheduling from a top-level creation run', () => {
|
|
|
367
367
|
await waitFor(() => inner === 1, 'the inner effect runs');
|
|
368
368
|
});
|
|
369
369
|
});
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
describe('deferred inner effects', () => {
|
|
373
|
+
// The parent tracks a dependency before creating the inner effect, so the inner effect's creation run is deferred.
|
|
374
|
+
// That run sets its first value; it must not count as a change the parent depends on, or the parent re-runs,
|
|
375
|
+
// creates a new inner effect, and loops. The guard ends such a loop so a regression fails instead of hanging.
|
|
376
|
+
it('an inner effect returning a value does not re-run its parent', async () => {
|
|
377
|
+
let inner = 0,
|
|
378
|
+
outer = 0,
|
|
379
|
+
s = signal(0);
|
|
380
|
+
|
|
381
|
+
effect(() => {
|
|
382
|
+
read(s);
|
|
383
|
+
outer++;
|
|
384
|
+
|
|
385
|
+
if (outer > 20) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
effect(() => {
|
|
390
|
+
inner++;
|
|
391
|
+
|
|
392
|
+
return true;
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
await waitFor(() => inner >= 1, 'the inner effect runs');
|
|
397
|
+
await Promise.resolve();
|
|
398
|
+
await Promise.resolve();
|
|
399
|
+
|
|
400
|
+
expect(outer).toBe(1);
|
|
401
|
+
expect(inner).toBe(1);
|
|
402
|
+
|
|
403
|
+
write(s, 1);
|
|
404
|
+
await waitFor(() => outer === 2, 'the parent re-runs on its own dependency');
|
|
405
|
+
await Promise.resolve();
|
|
406
|
+
await Promise.resolve();
|
|
407
|
+
|
|
408
|
+
expect(outer).toBe(2);
|
|
409
|
+
expect(inner).toBe(2);
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
it('an inner effect with an apply phase does not re-run its parent', async () => {
|
|
413
|
+
let applied: boolean[] = [],
|
|
414
|
+
outer = 0,
|
|
415
|
+
path = signal('/');
|
|
416
|
+
|
|
417
|
+
effect(() => {
|
|
418
|
+
read(path);
|
|
419
|
+
outer++;
|
|
420
|
+
|
|
421
|
+
if (outer > 20) {
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
effect(() => read(path) === '/', (home) => {
|
|
426
|
+
applied.push(home);
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
await waitFor(() => applied.length >= 1, 'the inner effect applies');
|
|
431
|
+
await Promise.resolve();
|
|
432
|
+
await Promise.resolve();
|
|
433
|
+
|
|
434
|
+
expect(outer).toBe(1);
|
|
435
|
+
expect(applied).toEqual([true]);
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it('a parent that reads its inner effect gets its value, once, and follows it', async () => {
|
|
439
|
+
let inner = 0,
|
|
440
|
+
other = signal(0),
|
|
441
|
+
outer = 0,
|
|
442
|
+
s = signal(1),
|
|
443
|
+
seen: (number | undefined)[] = [];
|
|
444
|
+
|
|
445
|
+
effect(() => {
|
|
446
|
+
read(other);
|
|
447
|
+
outer++;
|
|
448
|
+
|
|
449
|
+
if (outer > 20) {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
let doubled = effect(() => {
|
|
454
|
+
inner++;
|
|
455
|
+
|
|
456
|
+
return read(s) * 2;
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
seen.push(read(doubled));
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
await Promise.resolve();
|
|
463
|
+
await Promise.resolve();
|
|
464
|
+
|
|
465
|
+
expect(seen).toEqual([2]);
|
|
466
|
+
expect(outer).toBe(1);
|
|
467
|
+
expect(inner).toBe(1);
|
|
468
|
+
|
|
469
|
+
write(s, 2);
|
|
470
|
+
await waitFor(() => seen.length === 2, 'the parent re-runs');
|
|
471
|
+
|
|
472
|
+
expect(seen).toEqual([2, 4]);
|
|
473
|
+
});
|
|
474
|
+
});
|