@esportsplus/reactivity 0.43.0 → 0.43.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.
- package/build/system.js +2 -1
- package/package.json +1 -1
- package/src/system.ts +6 -2
- package/test/effects.test.ts +68 -0
package/build/system.js
CHANGED
|
@@ -539,17 +539,18 @@ 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 {
|
|
548
550
|
self.height = observer.height + 1;
|
|
549
551
|
insertIntoHeap(self);
|
|
550
552
|
schedule();
|
|
551
553
|
}
|
|
552
|
-
link(self, observer);
|
|
553
554
|
own(observer, self);
|
|
554
555
|
}
|
|
555
556
|
else {
|
package/package.json
CHANGED
package/src/system.ts
CHANGED
|
@@ -812,21 +812,25 @@ 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
|
+
// link(), so self has no subs yet — recompute's propagate is a no-op and cannot
|
|
819
820
|
// re-run the parent. Deferring here (as effect() still does) would leave value unset.
|
|
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().
|
|
824
829
|
self.height = observer.height + 1;
|
|
825
830
|
insertIntoHeap(self);
|
|
826
831
|
schedule();
|
|
827
832
|
}
|
|
828
833
|
|
|
829
|
-
link(self, observer);
|
|
830
834
|
own(observer, self as Computed<unknown>);
|
|
831
835
|
}
|
|
832
836
|
else {
|
package/test/effects.test.ts
CHANGED
|
@@ -367,3 +367,71 @@ 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
|
+
});
|