async-reactivity 2.0.25 → 2.0.27
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/lib/computed.js +38 -126
- package/lib/computed.test.js +294 -269
- package/lib/effect.js +130 -0
- package/lib/effect.test.js +192 -0
- package/lib/listener.test.js +1 -1
- package/lib/ref.js +17 -6
- package/lib/watcher.js +9 -37
- package/package.json +6 -6
- package/src/computed.test.ts +304 -274
- package/src/computed.ts +40 -131
- package/src/effect.test.ts +246 -0
- package/src/effect.ts +149 -0
- package/src/listener.test.ts +1 -1
- package/src/ref.ts +21 -6
- package/src/tsconfig.json +3 -3
- package/src/watcher.ts +9 -43
- package/types/computed.d.ts +7 -19
- package/types/computed.d.ts.map +1 -1
- package/types/effect.d.ts +27 -0
- package/types/effect.d.ts.map +1 -0
- package/types/effect.test.d.ts +2 -0
- package/types/effect.test.d.ts.map +1 -0
- package/types/ref.d.ts +7 -2
- package/types/ref.d.ts.map +1 -1
- package/types/watcher.d.ts +2 -9
- package/types/watcher.d.ts.map +1 -1
- package/lib/tracker.js +0 -18
- package/src/tracker.ts +0 -24
- package/types/tracker.d.ts +0 -10
- package/types/tracker.d.ts.map +0 -1
package/src/computed.test.ts
CHANGED
|
@@ -3,344 +3,374 @@ import assert from 'assert';
|
|
|
3
3
|
import { Computed, Ref } from './index.js';
|
|
4
4
|
|
|
5
5
|
describe('computed', function () {
|
|
6
|
-
it('lazy compute', function () {
|
|
7
|
-
let gate = false;
|
|
8
|
-
const a = new Computed(() => {
|
|
9
|
-
gate = true;
|
|
10
|
-
return 5;
|
|
11
|
-
});
|
|
12
6
|
|
|
13
|
-
|
|
14
|
-
assert.strictEqual(a.value, 5);
|
|
15
|
-
assert.strictEqual(gate, true);
|
|
16
|
-
});
|
|
7
|
+
describe('sync', function () {
|
|
17
8
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
9
|
+
it('lazy compute', function () {
|
|
10
|
+
let gate = false;
|
|
11
|
+
const a = new Computed(() => {
|
|
12
|
+
gate = true;
|
|
13
|
+
return 5;
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
assert.strictEqual(gate, false);
|
|
17
|
+
assert.strictEqual(a.value, 5);
|
|
18
|
+
assert.strictEqual(gate, true);
|
|
23
19
|
});
|
|
24
20
|
|
|
25
|
-
|
|
21
|
+
it('cache', function () {
|
|
22
|
+
let gate = false;
|
|
23
|
+
const a = new Computed(() => {
|
|
24
|
+
gate = true;
|
|
25
|
+
return 5;
|
|
26
|
+
});
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
assert.strictEqual(a.value, 5);
|
|
29
|
-
assert.strictEqual(gate, false);
|
|
30
|
-
});
|
|
28
|
+
assert.strictEqual(a.value, 5);
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return value(a) + 4;
|
|
30
|
+
gate = false;
|
|
31
|
+
assert.strictEqual(a.value, 5);
|
|
32
|
+
assert.strictEqual(gate, false);
|
|
36
33
|
});
|
|
37
|
-
assert.strictEqual(b.value, 9);
|
|
38
|
-
a.value = 6;
|
|
39
|
-
assert.strictEqual(b.value, 10);
|
|
40
|
-
});
|
|
41
34
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
35
|
+
it('invalidate dependents', function () {
|
|
36
|
+
const a = new Ref(5);
|
|
37
|
+
const b = new Computed((value) => {
|
|
38
|
+
return value(a) + 4;
|
|
39
|
+
});
|
|
40
|
+
assert.strictEqual(b.value, 9);
|
|
41
|
+
a.value = 6;
|
|
42
|
+
assert.strictEqual(b.value, 10);
|
|
49
43
|
});
|
|
50
|
-
assert.strictEqual(c.value, 10);
|
|
51
|
-
a.value = 15;
|
|
52
|
-
assert.strictEqual(c.value, 0);
|
|
53
|
-
b.value = 15;
|
|
54
|
-
gate = false;
|
|
55
|
-
assert.strictEqual(c.value, 0);
|
|
56
|
-
assert.strictEqual(gate, false);
|
|
57
|
-
});
|
|
58
44
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
45
|
+
it('dependents up-to-date', function () {
|
|
46
|
+
const a = new Ref(5);
|
|
47
|
+
const b = new Ref(10);
|
|
48
|
+
let gate;
|
|
49
|
+
const c = new Computed((value) => {
|
|
50
|
+
gate = true;
|
|
51
|
+
return value(a) < 10 ? value(b) : 0;
|
|
52
|
+
});
|
|
53
|
+
assert.strictEqual(c.value, 10);
|
|
54
|
+
a.value = 15;
|
|
55
|
+
assert.strictEqual(c.value, 0);
|
|
56
|
+
b.value = 15;
|
|
57
|
+
gate = false;
|
|
58
|
+
assert.strictEqual(c.value, 0);
|
|
59
|
+
assert.strictEqual(gate, false);
|
|
63
60
|
});
|
|
64
|
-
// @ts-expect-error
|
|
65
|
-
const b = new Computed((value) => {
|
|
66
|
-
return value(a);
|
|
67
|
-
});
|
|
68
|
-
assert.throws(() => a.value);
|
|
69
|
-
});
|
|
70
61
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
62
|
+
it('detect circular dependency', function () {
|
|
63
|
+
// @ts-expect-error
|
|
64
|
+
const a = new Computed((value) => {
|
|
65
|
+
return value(b);
|
|
66
|
+
});
|
|
67
|
+
// @ts-expect-error
|
|
68
|
+
const b = new Computed((value) => {
|
|
69
|
+
return value(a);
|
|
70
|
+
});
|
|
71
|
+
assert.throws(() => a.value);
|
|
72
|
+
});
|
|
75
73
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
74
|
+
xit('detect circular deeper dependency', function () {
|
|
75
|
+
// do not support for better performance
|
|
76
|
+
assert.fail('not implemented');
|
|
79
77
|
});
|
|
80
|
-
assert.throws(() => a.value);
|
|
81
|
-
});
|
|
82
78
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return value(a);
|
|
79
|
+
it('throw error', function () {
|
|
80
|
+
const a = new Computed(() => {
|
|
81
|
+
throw new Error();
|
|
82
|
+
});
|
|
83
|
+
assert.throws(() => a.value);
|
|
89
84
|
});
|
|
90
85
|
|
|
91
|
-
|
|
86
|
+
it('ignore same ref value', function () {
|
|
87
|
+
let gate = 0;
|
|
88
|
+
const a = new Ref(5);
|
|
89
|
+
const b = new Computed((value) => {
|
|
90
|
+
gate++;
|
|
91
|
+
return value(a);
|
|
92
|
+
});
|
|
92
93
|
|
|
93
|
-
|
|
94
|
-
assert.strictEqual(b.value, 5);
|
|
95
|
-
assert.strictEqual(gate, 1);
|
|
96
|
-
});
|
|
94
|
+
assert.strictEqual(b.value, 5);
|
|
97
95
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const b = new Computed((value) => {
|
|
102
|
-
return value(a) % 2;
|
|
103
|
-
});
|
|
104
|
-
const c = new Computed((value) => {
|
|
105
|
-
gate++;
|
|
106
|
-
return value(b) + 5;
|
|
96
|
+
a.value = 5;
|
|
97
|
+
assert.strictEqual(b.value, 5);
|
|
98
|
+
assert.strictEqual(gate, 1);
|
|
107
99
|
});
|
|
108
100
|
|
|
109
|
-
|
|
110
|
-
|
|
101
|
+
it('ignore same computed value', function () {
|
|
102
|
+
let gate = 0;
|
|
103
|
+
const a = new Ref(5);
|
|
104
|
+
const b = new Computed((value) => {
|
|
105
|
+
return value(a) % 2;
|
|
106
|
+
});
|
|
107
|
+
const c = new Computed((value) => {
|
|
108
|
+
gate++;
|
|
109
|
+
return value(b) + 5;
|
|
110
|
+
});
|
|
111
111
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
assert.strictEqual(gate, 1);
|
|
115
|
-
});
|
|
112
|
+
assert.strictEqual(c.value, 6);
|
|
113
|
+
assert.strictEqual(gate, 1);
|
|
116
114
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const b1 = new Computed((value) => {
|
|
121
|
-
return value(a) % 2;
|
|
122
|
-
});
|
|
123
|
-
const b2 = new Computed(() => 5);
|
|
124
|
-
const c = new Computed((value) => {
|
|
125
|
-
gate++;
|
|
126
|
-
return value(b1) + value(b2);
|
|
115
|
+
a.value = 7;
|
|
116
|
+
assert.strictEqual(c.value, 6);
|
|
117
|
+
assert.strictEqual(gate, 1);
|
|
127
118
|
});
|
|
128
119
|
|
|
129
|
-
|
|
130
|
-
|
|
120
|
+
it('ignore same computed values', function () {
|
|
121
|
+
let gate = 0;
|
|
122
|
+
const a = new Ref(5);
|
|
123
|
+
const b1 = new Computed((value) => {
|
|
124
|
+
return value(a) % 2;
|
|
125
|
+
});
|
|
126
|
+
const b2 = new Computed(() => 5);
|
|
127
|
+
const c = new Computed((value) => {
|
|
128
|
+
gate++;
|
|
129
|
+
return value(b1) + value(b2);
|
|
130
|
+
});
|
|
131
131
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
assert.strictEqual(gate, 1);
|
|
135
|
-
});
|
|
132
|
+
assert.strictEqual(c.value, 6);
|
|
133
|
+
assert.strictEqual(gate, 1);
|
|
136
134
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const b = new Computed((value) => {
|
|
141
|
-
return value(a) % 2;
|
|
142
|
-
});
|
|
143
|
-
const c = new Computed((value) => {
|
|
144
|
-
gate++;
|
|
145
|
-
return value(b) + 5;
|
|
135
|
+
a.value = 7;
|
|
136
|
+
assert.strictEqual(c.value, 6);
|
|
137
|
+
assert.strictEqual(gate, 1);
|
|
146
138
|
});
|
|
147
139
|
|
|
148
|
-
|
|
149
|
-
|
|
140
|
+
it('compute when forced', function () {
|
|
141
|
+
let gate = 0;
|
|
142
|
+
const a = new Ref(5);
|
|
143
|
+
const b = new Computed((value) => {
|
|
144
|
+
return value(a) % 2;
|
|
145
|
+
});
|
|
146
|
+
const c = new Computed((value) => {
|
|
147
|
+
gate++;
|
|
148
|
+
return value(b) + 5;
|
|
149
|
+
});
|
|
150
150
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
assert.strictEqual(gate, 2);
|
|
154
|
-
});
|
|
151
|
+
assert.strictEqual(c.value, 6);
|
|
152
|
+
assert.strictEqual(gate, 1);
|
|
155
153
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
await new Promise(resolve => setTimeout(resolve, 10));
|
|
160
|
-
if (abortSignal.aborted) {
|
|
161
|
-
gate++;
|
|
162
|
-
throw new Error();
|
|
163
|
-
}
|
|
164
|
-
return 5;
|
|
154
|
+
c.forceInvalidate();
|
|
155
|
+
assert.strictEqual(c.value, 6);
|
|
156
|
+
assert.strictEqual(gate, 2);
|
|
165
157
|
});
|
|
166
158
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
159
|
+
it('compute and abort', function () {
|
|
160
|
+
let gate = 0;
|
|
161
|
+
const a = new Ref(5);
|
|
162
|
+
const b = new Computed(() => {
|
|
163
|
+
a.value = 6;
|
|
164
|
+
return 6;
|
|
165
|
+
});
|
|
166
|
+
const c = new Computed((value, _previousValue, abortSignal) => {
|
|
167
|
+
const result = value(a);
|
|
168
|
+
value(b);
|
|
169
|
+
if (abortSignal.aborted) {
|
|
170
|
+
gate++;
|
|
171
|
+
throw new Error();
|
|
172
|
+
}
|
|
173
|
+
return result;
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
assert.strictEqual(c.value, 6);
|
|
177
|
+
assert.strictEqual(gate, 1);
|
|
178
|
+
});
|
|
179
179
|
|
|
180
|
-
|
|
180
|
+
it('isEqual', function () {
|
|
181
|
+
const a = new Ref(1);
|
|
182
|
+
const b = new Computed((value) => {
|
|
183
|
+
return value(a);
|
|
184
|
+
}, (_newValue, oldValue) => oldValue !== undefined);
|
|
185
|
+
const c = new Computed(value => value(b));
|
|
181
186
|
|
|
182
|
-
|
|
183
|
-
assert.strictEqual(b.value, 1);
|
|
184
|
-
});
|
|
185
|
-
});
|
|
187
|
+
assert.strictEqual(c.value, 1);
|
|
186
188
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const a = new Computed(async () => {
|
|
190
|
-
await new Promise(resolve => setTimeout(resolve));
|
|
191
|
-
return 5;
|
|
189
|
+
a.value = 2;
|
|
190
|
+
assert.strictEqual(c.value, 1);
|
|
192
191
|
});
|
|
193
|
-
assert.strictEqual(await a.value, 5);
|
|
194
192
|
});
|
|
195
193
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
194
|
+
describe('async', function () {
|
|
195
|
+
|
|
196
|
+
it('getter', async function () {
|
|
197
|
+
const a = new Computed(async () => {
|
|
198
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
199
|
+
return 5;
|
|
200
|
+
});
|
|
201
|
+
assert.strictEqual(await a.value, 5);
|
|
201
202
|
});
|
|
202
|
-
assert.strictEqual(await b.value, 10);
|
|
203
|
-
a.value = 6;
|
|
204
|
-
assert.strictEqual(await b.value, 11);
|
|
205
|
-
});
|
|
206
203
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
204
|
+
it('tracks async dependencies', async function () {
|
|
205
|
+
const a = new Ref(5);
|
|
206
|
+
const b = new Computed(async (value) => {
|
|
207
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
208
|
+
return value(a) + 5;
|
|
209
|
+
});
|
|
210
|
+
assert.strictEqual(await b.value, 10);
|
|
211
|
+
a.value = 6;
|
|
212
|
+
assert.strictEqual(await b.value, 11);
|
|
211
213
|
});
|
|
212
214
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
215
|
+
it('get value while computing', async function () {
|
|
216
|
+
const a = new Computed(async () => {
|
|
217
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
218
|
+
return 5;
|
|
219
|
+
});
|
|
216
220
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const a = new Computed(async (value) => {
|
|
220
|
-
await new Promise(resolve => setTimeout(resolve));
|
|
221
|
-
return value(b);
|
|
221
|
+
a.value;
|
|
222
|
+
assert.strictEqual(await a.value, 5);
|
|
222
223
|
});
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
224
|
+
|
|
225
|
+
it('detect circular dependency', async function () {
|
|
226
|
+
// @ts-expect-error
|
|
227
|
+
const a = new Computed(async (value) => {
|
|
228
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
229
|
+
return value(b);
|
|
230
|
+
});
|
|
231
|
+
// @ts-expect-error
|
|
232
|
+
const b = new Computed(async (value) => {
|
|
233
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
234
|
+
return value(a);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
assert.rejects(async () => await a.value);
|
|
227
238
|
});
|
|
228
239
|
|
|
229
|
-
|
|
230
|
-
|
|
240
|
+
it('dependency changed while computing', async function () {
|
|
241
|
+
const a = new Ref(5);
|
|
242
|
+
const b = new Computed(async (value) => {
|
|
243
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
244
|
+
return value(a) + 5;
|
|
245
|
+
});
|
|
246
|
+
b.value; // trigger compute
|
|
247
|
+
a.value = 8;
|
|
248
|
+
assert.strictEqual(await b.value, 13);
|
|
249
|
+
});
|
|
231
250
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
251
|
+
it('dependency changed while computing - abortSignal', async function () {
|
|
252
|
+
const a = new Ref(5);
|
|
253
|
+
let gate = 0;
|
|
254
|
+
const b = new Computed(async (value, _previousValue, abortSignal) => {
|
|
255
|
+
const dep = value(a);
|
|
256
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
257
|
+
if (abortSignal.aborted) {
|
|
258
|
+
throw new Error();
|
|
259
|
+
}
|
|
260
|
+
gate++;
|
|
261
|
+
return dep + 5;
|
|
262
|
+
});
|
|
263
|
+
b.value; // trigger compute
|
|
264
|
+
a.value = 8;
|
|
265
|
+
await b.value;
|
|
266
|
+
assert.strictEqual(gate, 1);
|
|
267
|
+
});
|
|
239
268
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
269
|
+
it('old dependency changed while computing', async function () {
|
|
270
|
+
let gate = 0;
|
|
271
|
+
const a = new Ref(5);
|
|
272
|
+
const b = new Computed(async (value) => {
|
|
273
|
+
gate++;
|
|
274
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
275
|
+
return value(a) + 2;
|
|
276
|
+
});
|
|
277
|
+
assert.strictEqual(await b.value, 7);
|
|
278
|
+
b.forceInvalidate();
|
|
279
|
+
const promise = b.value;
|
|
280
|
+
a.value = 6;
|
|
281
|
+
assert.strictEqual(await promise, 8);
|
|
282
|
+
assert.strictEqual(gate, 2);
|
|
251
283
|
});
|
|
252
|
-
b.value; // trigger compute
|
|
253
|
-
a.value = 8;
|
|
254
|
-
await b.value;
|
|
255
|
-
assert.strictEqual(gate, 1);
|
|
256
|
-
});
|
|
257
284
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
285
|
+
it('new dependency changed while computing', async function () {
|
|
286
|
+
let gate = 0;
|
|
287
|
+
const a = new Ref(5);
|
|
288
|
+
const b = new Ref(10);
|
|
289
|
+
const c = new Computed(async (value) => {
|
|
290
|
+
gate++;
|
|
291
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
292
|
+
let sum = value(a);
|
|
293
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
294
|
+
sum += value(b);
|
|
295
|
+
return sum;
|
|
296
|
+
});
|
|
297
|
+
assert.strictEqual(await c.value, 15);
|
|
298
|
+
c.forceInvalidate();
|
|
299
|
+
const promise = c.value;
|
|
300
|
+
await new Promise(resolve => setTimeout(resolve, 60));
|
|
301
|
+
a.value = 10;
|
|
302
|
+
assert.strictEqual(await promise, 20);
|
|
303
|
+
assert.strictEqual(gate, 3);
|
|
265
304
|
});
|
|
266
|
-
assert.strictEqual(await b.value, 7);
|
|
267
|
-
b.invalidate();
|
|
268
|
-
const promise = b.value;
|
|
269
|
-
a.value = 6;
|
|
270
|
-
assert.strictEqual(await promise, 8);
|
|
271
|
-
assert.strictEqual(gate, 2);
|
|
272
|
-
});
|
|
273
305
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
306
|
+
it('fallback to primitive while computing', async function () {
|
|
307
|
+
const a = new Ref(5);
|
|
308
|
+
const b = new Ref(10);
|
|
309
|
+
const c = new Computed(async (value) => {
|
|
310
|
+
if (value(a) < 10) {
|
|
311
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
312
|
+
return value(b) + 5;
|
|
313
|
+
}
|
|
314
|
+
return 2;
|
|
315
|
+
});
|
|
316
|
+
const promise = c.value;
|
|
317
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
318
|
+
a.value = 10;
|
|
319
|
+
assert.strictEqual(await promise, 2);
|
|
285
320
|
});
|
|
286
|
-
assert.strictEqual(await c.value, 15);
|
|
287
|
-
c.invalidate();
|
|
288
|
-
const promise = c.value;
|
|
289
|
-
await new Promise(resolve => setTimeout(resolve, 60));
|
|
290
|
-
a.value = 10;
|
|
291
|
-
assert.strictEqual(await promise, 20);
|
|
292
|
-
assert.strictEqual(gate, 3);
|
|
293
|
-
});
|
|
294
321
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
return value(b) + 5;
|
|
302
|
-
}
|
|
303
|
-
return 2;
|
|
322
|
+
it('throw error', async function () {
|
|
323
|
+
const a = new Computed(async () => {
|
|
324
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
325
|
+
throw new Error();
|
|
326
|
+
});
|
|
327
|
+
assert.rejects(() => a.value);
|
|
304
328
|
});
|
|
305
|
-
const promise = c.value;
|
|
306
|
-
await new Promise(resolve => setTimeout(resolve, 20));
|
|
307
|
-
a.value = 10;
|
|
308
|
-
assert.strictEqual(await promise, 2);
|
|
309
|
-
});
|
|
310
329
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
330
|
+
it('compute and abort when forced', async function () {
|
|
331
|
+
let gate = 0;
|
|
332
|
+
const b = new Computed(async (_value, _previousValue, abortSignal) => {
|
|
333
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
334
|
+
if (abortSignal.aborted) {
|
|
335
|
+
gate++;
|
|
336
|
+
throw new Error();
|
|
337
|
+
}
|
|
338
|
+
return 5;
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
const v = b.value;
|
|
342
|
+
await new Promise(resolve => setTimeout(resolve, 5));
|
|
343
|
+
b.forceInvalidate();
|
|
344
|
+
assert.strictEqual(await v, 5);
|
|
345
|
+
assert.strictEqual(gate, 1);
|
|
315
346
|
});
|
|
316
|
-
assert.rejects(() => a.value);
|
|
317
|
-
});
|
|
318
347
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
348
|
+
it('reset computed', async function () {
|
|
349
|
+
const a = new Ref(5);
|
|
350
|
+
let gate = 0;
|
|
351
|
+
const b = new Computed(value => {
|
|
352
|
+
gate++;
|
|
353
|
+
return value(a) + 2;
|
|
354
|
+
});
|
|
355
|
+
b.value;
|
|
356
|
+
assert.strictEqual(gate, 1);
|
|
357
|
+
b.reset();
|
|
358
|
+
b.value;
|
|
359
|
+
assert.strictEqual(gate, 2);
|
|
325
360
|
});
|
|
326
|
-
b.value;
|
|
327
|
-
assert.strictEqual(gate, 1);
|
|
328
|
-
b.reset();
|
|
329
|
-
b.value;
|
|
330
|
-
assert.strictEqual(gate, 2);
|
|
331
|
-
});
|
|
332
361
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
362
|
+
it('auto reset', async function () {
|
|
363
|
+
const a = new Computed(() => true, undefined, 0);
|
|
364
|
+
const b = new Computed(value => value(a));
|
|
336
365
|
|
|
337
|
-
|
|
366
|
+
assert.strictEqual(b.value, true);
|
|
338
367
|
|
|
339
|
-
|
|
368
|
+
b.dispose();
|
|
340
369
|
|
|
341
|
-
|
|
370
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
342
371
|
|
|
343
|
-
|
|
344
|
-
|
|
372
|
+
// @ts-expect-error
|
|
373
|
+
assert.strictEqual(a._value, undefined);
|
|
374
|
+
});
|
|
345
375
|
});
|
|
346
376
|
});
|