async-reactivity 2.0.1 → 2.0.2

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,62 @@
1
+ import 'mocha';
2
+ import assert from 'assert';
3
+ import { Computed, Ref, Watcher } from './index.js';
4
+
5
+ describe('watcher', function () {
6
+ it('sync', function () {
7
+ const a = new Ref(5);
8
+ new Watcher(a, (newValue: number, oldValue?: number) => {
9
+ assert.strictEqual(oldValue, 5);
10
+ assert.strictEqual(newValue, 6);
11
+ }, false);
12
+ a.value = 6;
13
+ });
14
+
15
+ it('async', async function () {
16
+ const a = new Computed(async () => {
17
+ await new Promise(resolve => setTimeout(resolve));
18
+ return 10;
19
+ });
20
+ const result = await new Promise<number>(resolve => new Watcher(a, resolve));
21
+ assert.strictEqual(result, 10);
22
+ });
23
+
24
+ it('sync cancel', async function () {
25
+ const a = new Ref(5);
26
+ const b = new Computed((value) => {
27
+ return value(a) % 2;
28
+ });
29
+ let gate = 0;
30
+ new Watcher(b, () => {
31
+ gate++;
32
+ }, false);
33
+ a.value = 6;
34
+ assert.strictEqual(gate, 1);
35
+ a.value = 7;
36
+ a.value = 9;
37
+ a.value = 11;
38
+ assert.strictEqual(gate, 2);
39
+ });
40
+
41
+ it('async sync cancel', async function () {
42
+ const a = new Ref(5);
43
+ const b = new Computed(async (value) => {
44
+ new Promise(resolve => setTimeout(resolve));
45
+ return value(a) % 2;
46
+ });
47
+ let gate = 0;
48
+ new Watcher(b, () => {
49
+ gate++;
50
+ }, false);
51
+ await new Promise(resolve => setTimeout(resolve, 10));
52
+ a.value = 6;
53
+ await new Promise(resolve => setTimeout(resolve, 10));
54
+ assert.strictEqual(gate, 1);
55
+ a.value = 7;
56
+ await new Promise(resolve => setTimeout(resolve, 10));
57
+ assert.strictEqual(gate, 2);
58
+ a.value = 9;
59
+ await new Promise(resolve => setTimeout(resolve, 10));
60
+ assert.strictEqual(gate, 3);
61
+ });
62
+ });
@@ -0,0 +1,2 @@
1
+ import 'mocha';
2
+ //# sourceMappingURL=computed.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"computed.test.d.ts","sourceRoot":"","sources":["../src/computed.test.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,CAAC"}
package/types/ref.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import Dependency from "./dependency.js";
2
2
  import Tracker from "./tracker.js";
3
3
  export default class Ref<T> extends Tracker<T> implements Dependency<T> {
4
- constructor(_value: T);
4
+ private isEqual;
5
+ constructor(_value: T, isEqual?: (v1?: T | undefined, v2?: T | undefined) => boolean);
5
6
  set value(_value: T);
6
7
  get value(): T;
7
8
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ref.d.ts","sourceRoot":"","sources":["../src/ref.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AACzC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,MAAM,CAAC,OAAO,OAAO,GAAG,CAAC,CAAC,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAE,YAAW,UAAU,CAAC,CAAC,CAAC;gBAEvD,MAAM,EAAE,CAAC;IAKrB,IAAW,KAAK,CAAC,MAAM,EAAE,CAAC,EAMzB;IAED,IAAW,KAAK,IAAI,CAAC,CAEpB;CACJ"}
1
+ {"version":3,"file":"ref.d.ts","sourceRoot":"","sources":["../src/ref.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AACzC,OAAO,OAAO,MAAM,cAAc,CAAC;AAInC,MAAM,CAAC,OAAO,OAAO,GAAG,CAAC,CAAC,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAE,YAAW,UAAU,CAAC,CAAC,CAAC;IACnE,OAAO,CAAC,OAAO,CAA4B;gBAE/B,MAAM,EAAE,CAAC,EAAE,OAAO,sDAAoB;IAMlD,IAAW,KAAK,CAAC,MAAM,EAAE,CAAC,EAMzB;IAED,IAAW,KAAK,IAAI,CAAC,CAEpB;CACJ"}
@@ -0,0 +1,2 @@
1
+ import 'mocha';
2
+ //# sourceMappingURL=ref.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ref.test.d.ts","sourceRoot":"","sources":["../src/ref.test.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,CAAC"}
@@ -0,0 +1,2 @@
1
+ import 'mocha';
2
+ //# sourceMappingURL=watcher.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"watcher.test.d.ts","sourceRoot":"","sources":["../src/watcher.test.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,CAAC"}
package/lib/index.test.js DELETED
@@ -1,319 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import 'mocha';
11
- import assert from 'assert';
12
- import { Computed, Ref, Watcher } from './index.js';
13
- describe('async reactivity', function () {
14
- describe('ref', function () {
15
- it('getter', function () {
16
- const a = new Ref(5);
17
- assert.strictEqual(a.value, 5);
18
- });
19
- it('setter', function () {
20
- const a = new Ref(5);
21
- a.value = 4;
22
- assert.strictEqual(a.value, 4);
23
- });
24
- });
25
- describe('computed', function () {
26
- it('lazy compute', function () {
27
- let gate = false;
28
- const a = new Computed(() => {
29
- gate = true;
30
- return 5;
31
- });
32
- assert.strictEqual(gate, false);
33
- assert.strictEqual(a.value, 5);
34
- assert.strictEqual(gate, true);
35
- });
36
- it('cache', function () {
37
- let gate = false;
38
- const a = new Computed(() => {
39
- gate = true;
40
- return 5;
41
- });
42
- assert.strictEqual(a.value, 5);
43
- gate = false;
44
- assert.strictEqual(a.value, 5);
45
- assert.strictEqual(gate, false);
46
- });
47
- it('invalidate dependents', function () {
48
- const a = new Ref(5);
49
- const b = new Computed((value) => {
50
- return value(a) + 4;
51
- });
52
- assert.strictEqual(b.value, 9);
53
- a.value = 6;
54
- assert.strictEqual(b.value, 10);
55
- });
56
- it('dependents up-to-date', function () {
57
- const a = new Ref(5);
58
- const b = new Ref(10);
59
- let gate;
60
- const c = new Computed((value) => {
61
- gate = true;
62
- return value(a) < 10 ? value(b) : 0;
63
- });
64
- assert.strictEqual(c.value, 10);
65
- a.value = 15;
66
- assert.strictEqual(c.value, 0);
67
- b.value = 15;
68
- gate = false;
69
- assert.strictEqual(c.value, 0);
70
- assert.strictEqual(gate, false);
71
- });
72
- it('detect circular dependency', function () {
73
- // @ts-expect-error
74
- const a = new Computed((value) => {
75
- return value(b);
76
- });
77
- // @ts-expect-error
78
- const b = new Computed((value) => {
79
- return value(a);
80
- });
81
- assert.throws(() => a.value);
82
- });
83
- xit('detect circular deeper dependency', function () {
84
- // do not support for better performance
85
- assert.fail('not implemented');
86
- });
87
- it('throw error', function () {
88
- const a = new Computed(() => {
89
- throw new Error();
90
- });
91
- assert.throws(() => a.value);
92
- });
93
- it('ignore same ref value', function () {
94
- let gate = 0;
95
- const a = new Ref(5);
96
- const b = new Computed((value) => {
97
- gate++;
98
- return value(a);
99
- });
100
- assert.strictEqual(b.value, 5);
101
- a.value = 5;
102
- assert.strictEqual(b.value, 5);
103
- assert.strictEqual(gate, 1);
104
- });
105
- it('ignore same computed value', function () {
106
- let gate = 0;
107
- const a = new Ref(5);
108
- const b = new Computed((value) => {
109
- return value(a) % 2;
110
- });
111
- const c = new Computed((value) => {
112
- gate++;
113
- return value(b) + 5;
114
- });
115
- assert.strictEqual(c.value, 6);
116
- a.value = 7;
117
- assert.strictEqual(c.value, 6);
118
- assert.strictEqual(gate, 1);
119
- });
120
- });
121
- describe('async computed', function () {
122
- it('getter', function () {
123
- return __awaiter(this, void 0, void 0, function* () {
124
- const a = new Computed(() => __awaiter(this, void 0, void 0, function* () {
125
- yield new Promise(resolve => setTimeout(resolve));
126
- return 5;
127
- }));
128
- assert.strictEqual(yield a.value, 5);
129
- });
130
- });
131
- it('tracks async dependencies', function () {
132
- return __awaiter(this, void 0, void 0, function* () {
133
- const a = new Ref(5);
134
- const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
135
- yield new Promise(resolve => setTimeout(resolve));
136
- return value(a) + 5;
137
- }));
138
- assert.strictEqual(yield b.value, 10);
139
- a.value = 6;
140
- assert.strictEqual(yield b.value, 11);
141
- });
142
- });
143
- it('get value while computing', function () {
144
- return __awaiter(this, void 0, void 0, function* () {
145
- const a = new Computed(() => __awaiter(this, void 0, void 0, function* () {
146
- yield new Promise(resolve => setTimeout(resolve));
147
- return 5;
148
- }));
149
- a.value;
150
- assert.strictEqual(yield a.value, 5);
151
- });
152
- });
153
- it('detect circular dependency', function () {
154
- return __awaiter(this, void 0, void 0, function* () {
155
- // @ts-expect-error
156
- const a = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
157
- yield new Promise(resolve => setTimeout(resolve));
158
- return value(b);
159
- }));
160
- // @ts-expect-error
161
- const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
162
- yield new Promise(resolve => setTimeout(resolve));
163
- return value(a);
164
- }));
165
- assert.rejects(() => __awaiter(this, void 0, void 0, function* () { return yield a.value; }));
166
- });
167
- });
168
- it('dependency changed while computing', function () {
169
- return __awaiter(this, void 0, void 0, function* () {
170
- const a = new Ref(5);
171
- const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () { return value(a) + 5; }));
172
- b.value; // trigger compute
173
- a.value = 8;
174
- assert.strictEqual(yield b.value, 13);
175
- });
176
- });
177
- it('old dependency changed while computing', function () {
178
- return __awaiter(this, void 0, void 0, function* () {
179
- let gate = 0;
180
- const a = new Ref(5);
181
- const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
182
- gate++;
183
- yield new Promise(resolve => setTimeout(resolve));
184
- return value(a) + 2;
185
- }));
186
- assert.strictEqual(yield b.value, 7);
187
- b.invalidate();
188
- const promise = b.value;
189
- a.value = 6;
190
- assert.strictEqual(yield promise, 8);
191
- assert.strictEqual(gate, 2);
192
- });
193
- });
194
- it('new dependency changed while computing', function () {
195
- return __awaiter(this, void 0, void 0, function* () {
196
- let gate = 0;
197
- const a = new Ref(5);
198
- const b = new Ref(10);
199
- const c = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
200
- gate++;
201
- yield new Promise(resolve => setTimeout(resolve, 50));
202
- let sum = value(a);
203
- yield new Promise(resolve => setTimeout(resolve, 50));
204
- sum += value(b);
205
- return sum;
206
- }));
207
- assert.strictEqual(yield c.value, 15);
208
- c.invalidate();
209
- const promise = c.value;
210
- yield new Promise(resolve => setTimeout(resolve, 60));
211
- a.value = 10;
212
- assert.strictEqual(yield promise, 20);
213
- assert.strictEqual(gate, 3);
214
- });
215
- });
216
- it('fallback to primitive while computing', function () {
217
- return __awaiter(this, void 0, void 0, function* () {
218
- const a = new Ref(5);
219
- const b = new Ref(10);
220
- const c = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
221
- if (value(a) < 10) {
222
- yield new Promise(resolve => setTimeout(resolve, 50));
223
- return value(b) + 5;
224
- }
225
- return 2;
226
- }));
227
- const promise = c.value;
228
- yield new Promise(resolve => setTimeout(resolve, 20));
229
- a.value = 10;
230
- assert.strictEqual(yield promise, 2);
231
- });
232
- });
233
- it('throw error', function () {
234
- return __awaiter(this, void 0, void 0, function* () {
235
- const a = new Computed(() => __awaiter(this, void 0, void 0, function* () {
236
- yield new Promise(resolve => setTimeout(resolve));
237
- throw new Error();
238
- }));
239
- assert.rejects(() => a.value);
240
- });
241
- });
242
- it('dispose computed', function () {
243
- return __awaiter(this, void 0, void 0, function* () {
244
- const a = new Ref(5);
245
- let gate = 0;
246
- const b = new Computed(value => {
247
- gate++;
248
- return value(a) + 2;
249
- });
250
- b.value;
251
- assert.strictEqual(gate, 1);
252
- b.dispose();
253
- b.value;
254
- assert.strictEqual(gate, 2);
255
- });
256
- });
257
- });
258
- describe('watcher', function () {
259
- it('sync', function () {
260
- const a = new Ref(5);
261
- new Watcher(a, (newValue, oldValue) => {
262
- assert.strictEqual(oldValue, 5);
263
- assert.strictEqual(newValue, 6);
264
- }, false);
265
- a.value = 6;
266
- });
267
- it('async', function () {
268
- return __awaiter(this, void 0, void 0, function* () {
269
- const a = new Computed(() => __awaiter(this, void 0, void 0, function* () {
270
- yield new Promise(resolve => setTimeout(resolve));
271
- return 10;
272
- }));
273
- const result = yield new Promise(resolve => new Watcher(a, resolve));
274
- assert.strictEqual(result, 10);
275
- });
276
- });
277
- it('sync cancel', function () {
278
- return __awaiter(this, void 0, void 0, function* () {
279
- const a = new Ref(5);
280
- const b = new Computed((value) => {
281
- return value(a) % 2;
282
- });
283
- let gate = 0;
284
- new Watcher(b, () => {
285
- gate++;
286
- }, false);
287
- a.value = 6;
288
- assert.strictEqual(gate, 1);
289
- a.value = 7;
290
- a.value = 9;
291
- a.value = 11;
292
- assert.strictEqual(gate, 2);
293
- });
294
- });
295
- it('async sync cancel', function () {
296
- return __awaiter(this, void 0, void 0, function* () {
297
- const a = new Ref(5);
298
- const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
299
- new Promise(resolve => setTimeout(resolve));
300
- return value(a) % 2;
301
- }));
302
- let gate = 0;
303
- new Watcher(b, () => {
304
- gate++;
305
- }, false);
306
- yield new Promise(resolve => setTimeout(resolve, 10));
307
- a.value = 6;
308
- yield new Promise(resolve => setTimeout(resolve, 10));
309
- assert.strictEqual(gate, 1);
310
- a.value = 7;
311
- yield new Promise(resolve => setTimeout(resolve, 10));
312
- assert.strictEqual(gate, 2);
313
- a.value = 9;
314
- yield new Promise(resolve => setTimeout(resolve, 10));
315
- assert.strictEqual(gate, 3);
316
- });
317
- });
318
- });
319
- });