@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.
@@ -0,0 +1,163 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { computed, dispose, effect, flush, onCleanup, owner, read, root, signal, write } from '~/system';
3
+ import { captureUncaught } from './lib/uncaught';
4
+
5
+
6
+ describe('effect apply phase ownership', () => {
7
+ it('apply is owned by its effect', () => {
8
+ let owners: unknown[] = [],
9
+ s = signal(0),
10
+ stop = effect(() => read(s), () => { owners.push(owner()); });
11
+
12
+ write(s, 1);
13
+ flush();
14
+
15
+ expect(owners).toEqual([stop, stop]);
16
+
17
+ dispose(stop);
18
+ });
19
+
20
+ it('what apply creates is disposed by the next run and by dispose', () => {
21
+ let log: string[] = [],
22
+ s = signal(0),
23
+ stop = effect(
24
+ () => read(s),
25
+ (value) => {
26
+ effect(() => {
27
+ onCleanup(() => log.push(`inner ${value}`));
28
+ });
29
+ onCleanup(() => log.push(`cleanup ${value}`));
30
+ }
31
+ );
32
+
33
+ flush();
34
+
35
+ expect(log).toEqual([]);
36
+
37
+ write(s, 1);
38
+ flush();
39
+
40
+ expect(log).toEqual(['inner 0', 'cleanup 0']);
41
+
42
+ dispose(stop);
43
+
44
+ expect(log.slice(2).toSorted()).toEqual(['cleanup 1', 'inner 1']);
45
+ });
46
+
47
+ it('apply does not subscribe the effect, or an enclosing computed, to what it reads', () => {
48
+ let runs = 0,
49
+ s = signal(0),
50
+ t = signal(0),
51
+ derived = computed(() => read(t) * 2),
52
+ stop = effect(
53
+ () => {
54
+ runs++;
55
+ return read(s);
56
+ },
57
+ () => {
58
+ read(t);
59
+ read(derived);
60
+ }
61
+ );
62
+
63
+ write(t, 1);
64
+ flush();
65
+
66
+ expect(runs).toBe(1);
67
+
68
+ write(s, 1);
69
+ flush();
70
+
71
+ expect(runs).toBe(2);
72
+
73
+ dispose(stop);
74
+ });
75
+
76
+ it('tracking resumes after apply: a later effect in the same owner still tracks', () => {
77
+ let seen: number[] = [],
78
+ s = signal(0),
79
+ t = signal(10),
80
+ stop = root((dispose) => {
81
+ effect(() => read(s), () => {});
82
+ effect(() => { seen.push(read(t)); });
83
+
84
+ return dispose;
85
+ });
86
+
87
+ write(t, 11);
88
+ flush();
89
+
90
+ expect(seen).toEqual([10, 11]);
91
+
92
+ stop();
93
+ });
94
+
95
+ it('a throwing apply surfaces the error and leaves tracking and ownership intact', async () => {
96
+ let outer: unknown[] = [],
97
+ s = signal(0),
98
+ stop!: ReturnType<typeof effect>,
99
+ errors = await captureUncaught(() => {
100
+ stop = effect(
101
+ () => read(s),
102
+ (value) => {
103
+ if (value === 1) {
104
+ throw new Error('apply failed');
105
+ }
106
+ }
107
+ );
108
+
109
+ outer.push(owner());
110
+ write(s, 1);
111
+ flush();
112
+ outer.push(owner());
113
+ });
114
+
115
+ expect(errors).toHaveLength(1);
116
+ expect((errors[0] as Error).message).toBe('apply failed');
117
+ expect(outer).toEqual([null, null]);
118
+
119
+ let applied: unknown[] = [],
120
+ owners: unknown[] = [],
121
+ other = effect(() => read(s), (value, prev) => {
122
+ applied.push([value, prev]);
123
+ owners.push(owner());
124
+ });
125
+
126
+ write(s, 2);
127
+ flush();
128
+
129
+ expect(applied).toEqual([[1, undefined], [2, 1]]);
130
+ expect(owners).toEqual([other, other]);
131
+
132
+ dispose(other);
133
+ dispose(stop);
134
+ });
135
+
136
+ it('prev is the last value fn returned, even after a run whose apply threw', async () => {
137
+ let prevs: unknown[] = [],
138
+ s = signal(0),
139
+ stop!: ReturnType<typeof effect>;
140
+
141
+ await captureUncaught(() => {
142
+ stop = effect(
143
+ () => read(s),
144
+ (value, prev) => {
145
+ prevs.push(prev);
146
+
147
+ if (value === 1) {
148
+ throw new Error('apply failed');
149
+ }
150
+ }
151
+ );
152
+
153
+ write(s, 1);
154
+ flush();
155
+ write(s, 2);
156
+ flush();
157
+ });
158
+
159
+ expect(prevs).toEqual([undefined, 0, 0]);
160
+
161
+ dispose(stop);
162
+ });
163
+ });
@@ -434,4 +434,41 @@ describe('deferred inner effects', () => {
434
434
  expect(outer).toBe(1);
435
435
  expect(applied).toEqual([true]);
436
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
+ });
437
474
  });