async-reactivity 1.1.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/computed.js +130 -129
- package/lib/dependency.js +2 -2
- package/lib/dependent.js +2 -2
- package/lib/index.js +12 -12
- package/lib/index.test.js +324 -315
- package/lib/ref.js +23 -23
- package/lib/tracker.js +22 -22
- package/lib/watcher.js +41 -41
- package/package.json +5 -5
- package/src/computed.ts +2 -1
- package/src/index.test.ts +8 -0
- package/types/computed.d.ts +29 -29
- package/types/computed.d.ts.map +1 -1
- package/types/dependency.d.ts +6 -6
- package/types/dependent.d.ts +6 -6
- package/types/index.d.ts +5 -5
- package/types/index.test.d.ts +1 -1
- package/types/ref.d.ts +7 -7
- package/types/tracker.d.ts +12 -12
- package/types/watcher.d.ts +14 -14
package/lib/index.test.js
CHANGED
|
@@ -1,315 +1,324 @@
|
|
|
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('
|
|
174
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
a
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
c.
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
assert_1.default.
|
|
236
|
-
});
|
|
237
|
-
});
|
|
238
|
-
it('
|
|
239
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
240
|
-
const a = new index_js_1.
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
assert_1.default.strictEqual(
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
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
|
+
});
|