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.
@@ -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
- assert.strictEqual(gate, false);
14
- assert.strictEqual(a.value, 5);
15
- assert.strictEqual(gate, true);
16
- });
7
+ describe('sync', function () {
17
8
 
18
- it('cache', function () {
19
- let gate = false;
20
- const a = new Computed(() => {
21
- gate = true;
22
- return 5;
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
- assert.strictEqual(a.value, 5);
21
+ it('cache', function () {
22
+ let gate = false;
23
+ const a = new Computed(() => {
24
+ gate = true;
25
+ return 5;
26
+ });
26
27
 
27
- gate = false;
28
- assert.strictEqual(a.value, 5);
29
- assert.strictEqual(gate, false);
30
- });
28
+ assert.strictEqual(a.value, 5);
31
29
 
32
- it('invalidate dependents', function () {
33
- const a = new Ref(5);
34
- const b = new Computed((value) => {
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
- it('dependents up-to-date', function () {
43
- const a = new Ref(5);
44
- const b = new Ref(10);
45
- let gate;
46
- const c = new Computed((value) => {
47
- gate = true;
48
- return value(a) < 10 ? value(b) : 0;
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
- it('detect circular dependency', function () {
60
- // @ts-expect-error
61
- const a = new Computed((value) => {
62
- return value(b);
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
- xit('detect circular deeper dependency', function () {
72
- // do not support for better performance
73
- assert.fail('not implemented');
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
- it('throw error', function () {
77
- const a = new Computed(() => {
78
- throw new Error();
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
- it('ignore same ref value', function () {
84
- let gate = 0;
85
- const a = new Ref(5);
86
- const b = new Computed((value) => {
87
- gate++;
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
- assert.strictEqual(b.value, 5);
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
- a.value = 5;
94
- assert.strictEqual(b.value, 5);
95
- assert.strictEqual(gate, 1);
96
- });
94
+ assert.strictEqual(b.value, 5);
97
95
 
98
- it('ignore same computed value', function () {
99
- let gate = 0;
100
- const a = new Ref(5);
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
- assert.strictEqual(c.value, 6);
110
- assert.strictEqual(gate, 1);
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
- a.value = 7;
113
- assert.strictEqual(c.value, 6);
114
- assert.strictEqual(gate, 1);
115
- });
112
+ assert.strictEqual(c.value, 6);
113
+ assert.strictEqual(gate, 1);
116
114
 
117
- it('ignore same computed values', function () {
118
- let gate = 0;
119
- const a = new Ref(5);
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
- assert.strictEqual(c.value, 6);
130
- assert.strictEqual(gate, 1);
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
- a.value = 7;
133
- assert.strictEqual(c.value, 6);
134
- assert.strictEqual(gate, 1);
135
- });
132
+ assert.strictEqual(c.value, 6);
133
+ assert.strictEqual(gate, 1);
136
134
 
137
- it('compute when forced', function () {
138
- let gate = 0;
139
- const a = new Ref(5);
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
- assert.strictEqual(c.value, 6);
149
- assert.strictEqual(gate, 1);
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
- c.forceInvalidate();
152
- assert.strictEqual(c.value, 6);
153
- assert.strictEqual(gate, 2);
154
- });
151
+ assert.strictEqual(c.value, 6);
152
+ assert.strictEqual(gate, 1);
155
153
 
156
- it('compute and abort when forced', async function () {
157
- let gate = 0;
158
- const b = new Computed(async (_value, _previousValue, abortSignal) => {
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
- const v = b.value;
168
- await new Promise(resolve => setTimeout(resolve, 5));
169
- b.forceInvalidate();
170
- assert.strictEqual(await v, 5);
171
- assert.strictEqual(gate, 1);
172
- });
173
-
174
- it('isEqual', function() {
175
- const a = new Ref(1);
176
- const b = new Computed((value) => {
177
- return value(a);
178
- }, (_newValue, oldValue) => oldValue !== undefined);
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
- assert.strictEqual(b.value, 1);
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
- a.value = 2;
183
- assert.strictEqual(b.value, 1);
184
- });
185
- });
187
+ assert.strictEqual(c.value, 1);
186
188
 
187
- describe('async computed', function () {
188
- it('getter', async function () {
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
- it('tracks async dependencies', async function () {
197
- const a = new Ref(5);
198
- const b = new Computed(async (value) => {
199
- await new Promise(resolve => setTimeout(resolve));
200
- return value(a) + 5;
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
- it('get value while computing', async function () {
208
- const a = new Computed(async () => {
209
- await new Promise(resolve => setTimeout(resolve));
210
- return 5;
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
- a.value;
214
- assert.strictEqual(await a.value, 5);
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
- it('detect circular dependency', async function () {
218
- // @ts-expect-error
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
- // @ts-expect-error
224
- const b = new Computed(async (value) => {
225
- await new Promise(resolve => setTimeout(resolve));
226
- return value(a);
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
- assert.rejects(async () => await a.value);
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
- it('dependency changed while computing', async function () {
233
- const a = new Ref(5);
234
- const b = new Computed(async (value) => value(a) + 5);
235
- b.value; // trigger compute
236
- a.value = 8;
237
- assert.strictEqual(await b.value, 13);
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
- it('dependency changed while computing - abortSignal', async function () {
241
- const a = new Ref(5);
242
- let gate = 0;
243
- const b = new Computed(async (value, _previousValue, abortSignal) => {
244
- const dep = value(a);
245
- await new Promise(resolve => setTimeout(resolve, 10));
246
- if (abortSignal.aborted) {
247
- throw new Error();
248
- }
249
- gate++;
250
- return dep + 5;
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
- it('old dependency changed while computing', async function () {
259
- let gate = 0;
260
- const a = new Ref(5);
261
- const b = new Computed(async (value) => {
262
- gate++;
263
- await new Promise(resolve => setTimeout(resolve));
264
- return value(a) + 2;
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
- it('new dependency changed while computing', async function () {
275
- let gate = 0;
276
- const a = new Ref(5);
277
- const b = new Ref(10);
278
- const c = new Computed(async (value) => {
279
- gate++;
280
- await new Promise(resolve => setTimeout(resolve, 50));
281
- let sum = value(a);
282
- await new Promise(resolve => setTimeout(resolve, 50));
283
- sum += value(b);
284
- return sum;
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
- it('fallback to primitive while computing', async function () {
296
- const a = new Ref(5);
297
- const b = new Ref(10);
298
- const c = new Computed(async (value) => {
299
- if (value(a) < 10) {
300
- await new Promise(resolve => setTimeout(resolve, 50));
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
- it('throw error', async function () {
312
- const a = new Computed(async () => {
313
- await new Promise(resolve => setTimeout(resolve));
314
- throw new Error();
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
- it('reset computed', async function () {
320
- const a = new Ref(5);
321
- let gate = 0;
322
- const b = new Computed(value => {
323
- gate++;
324
- return value(a) + 2;
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
- it('auto reset', async function() {
334
- const a = new Computed(() => true, undefined, 0);
335
- const b = new Computed(value => value(a));
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
- assert.strictEqual(b.value, true);
366
+ assert.strictEqual(b.value, true);
338
367
 
339
- b.dispose();
368
+ b.dispose();
340
369
 
341
- await new Promise(resolve => setTimeout(resolve, 10));
370
+ await new Promise(resolve => setTimeout(resolve, 10));
342
371
 
343
- // @ts-expect-error
344
- assert.strictEqual(a._value, undefined);
372
+ // @ts-expect-error
373
+ assert.strictEqual(a._value, undefined);
374
+ });
345
375
  });
346
376
  });