async-reactivity 1.0.2 → 1.1.3

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