-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaching.test.ts
150 lines (134 loc) · 3.4 KB
/
caching.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { input } from '.';
test('signals are lazy evaluated and cached', () => {
let count = 0;
const a = input(1);
const c = a.read(x => {
count++;
return x;
});
expect(count).toBe(0);
expect(c.value).toBe(1);
expect(count).toBe(1);
// read again to make sure it's cached
expect(c.value).toBe(1);
expect(count).toBe(1);
});
test('intermediate signals are recomputed and cached too', () => {
const a = input(1);
let bCount = 0;
const b = a.read(x => {
bCount++;
return x;
});
let cCount = 0;
const c = b.read(x => {
cCount++;
return x;
});
expect(bCount).toBe(0);
expect(cCount).toBe(0);
// get b to compute it.
expect(b.value).toBe(1);
expect(bCount).toBe(1);
expect(cCount).toBe(0);
// get c to compute it.
expect(c.value).toBe(1);
expect(bCount).toBe(1);
expect(cCount).toBe(1);
});
test('further caching happens when a new value happens to coincide', () => {
const x = input(0);
const parity = x.read(x => x % 2 === 0);
let count = 0;
const comp = parity.read(p => {
count++;
return p ? 'even' : 'odd';
});
expect(comp.value).toBe('even');
expect(count).toBe(1);
x.value = 2;
expect(comp.value).toBe('even');
expect(count).toBe(1);
x.value = 1;
expect(comp.value).toBe('odd');
expect(count).toBe(2);
});
test('detached signals do not trigger a recomputation', () => {
const x = input('x');
const y = input('y');
const b = input(true);
let bCount = 0;
let xCount = 0;
let yCount = 0;
const z = b.read(b => {
bCount++;
return b ? x.read(x => {
xCount++;
return x;
}) : y.read(y => {
yCount++;
return y;
});
});
expect(z.value).toBe('x');
expect(bCount).toBe(1);
expect(xCount).toBe(1);
expect(yCount).toBe(0);
y.value = 'y2';
expect(z.value).toBe('x');
// no change, all cached.
expect(bCount).toBe(1);
expect(xCount).toBe(1);
expect(yCount).toBe(0);
x.value = 'x2';
expect(z.value).toBe('x2');
expect(bCount).toBe(1);
expect(xCount).toBe(2);
expect(yCount).toBe(0);
// flip the boolean
b.value = false;
expect(z.value).toBe('y2');
expect(bCount).toBe(2);
expect(xCount).toBe(2);
expect(yCount).toBe(1);
x.value = 'x3';
expect(z.value).toBe('y2');
expect(bCount).toBe(2);
expect(xCount).toBe(2);
expect(yCount).toBe(1);
y.value = 'y3';
expect(z.value).toBe('y3');
// no change, all cached.
expect(bCount).toBe(2);
expect(xCount).toBe(2);
expect(yCount).toBe(2);
});
test('uncomputed signals are not skipped', () => {
const x = input(0, 'x');
const y = x.read(x => x % 2, 'y');
const z = y.read(x => x + 2, 'z');
// compute but not z
expect(y.value).toBe(0);
x.value = 2;
expect(z.value).toBe(2);
});
test('nested reads are cached', () => {
const x = input(0);
const y = input(0);
let countX = 0;
let countY = 0;
const sum = x.read(x => {
countX++;
return y.read(y => {
countY++;
return x + y;
});
});
expect(sum.value).toBe(0);
expect(countX).toBe(1);
expect(countY).toBe(1);
y.value = 1;
expect(sum.value).toBe(1);
expect(countX).toBe(1);
expect(countY).toBe(2);
});