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