async-reactivity 2.0.2 → 2.0.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 +7 -3
- package/lib/computed.test.js +109 -138
- package/lib/ref.js +1 -0
- package/lib/tracker.js +2 -3
- package/lib/watcher.js +13 -6
- package/lib/watcher.test.js +43 -54
- package/package.json +1 -1
- package/src/tsconfig.json +1 -1
- package/src/watcher.test.ts +10 -6
- package/src/watcher.ts +10 -5
- package/types/watcher.d.ts.map +1 -1
package/lib/computed.js
CHANGED
|
@@ -10,11 +10,14 @@ var ComputedState;
|
|
|
10
10
|
class CircularDependencyError extends Error {
|
|
11
11
|
}
|
|
12
12
|
export default class Computed extends Tracker {
|
|
13
|
+
getter;
|
|
14
|
+
state = ComputedState.Invalid;
|
|
15
|
+
dependencies = new Map();
|
|
16
|
+
computePromise;
|
|
17
|
+
computePromiseActions;
|
|
18
|
+
lastComputeAttemptPromise;
|
|
13
19
|
constructor(getter) {
|
|
14
20
|
super();
|
|
15
|
-
this.state = ComputedState.Invalid;
|
|
16
|
-
this.dependencies = new Map();
|
|
17
|
-
this.trackDependency = this.innerTrackDependency.bind(this);
|
|
18
21
|
this.getter = getter;
|
|
19
22
|
this.prepareComputePromise();
|
|
20
23
|
}
|
|
@@ -91,6 +94,7 @@ export default class Computed extends Tracker {
|
|
|
91
94
|
dependency.addDependent(this);
|
|
92
95
|
return dependency.value;
|
|
93
96
|
}
|
|
97
|
+
trackDependency = this.innerTrackDependency.bind(this);
|
|
94
98
|
finalizeComputing() {
|
|
95
99
|
this.state = ComputedState.Valid;
|
|
96
100
|
this.lastComputeAttemptPromise = undefined;
|
package/lib/computed.test.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
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
1
|
import 'mocha';
|
|
11
2
|
import assert from 'assert';
|
|
12
3
|
import { Computed, Ref } from './index.js';
|
|
@@ -107,139 +98,119 @@ describe('computed', function () {
|
|
|
107
98
|
});
|
|
108
99
|
});
|
|
109
100
|
describe('async computed', function () {
|
|
110
|
-
it('getter', function () {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return 5;
|
|
115
|
-
}));
|
|
116
|
-
assert.strictEqual(yield a.value, 5);
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
it('tracks async dependencies', function () {
|
|
120
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
-
const a = new Ref(5);
|
|
122
|
-
const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
|
|
123
|
-
yield new Promise(resolve => setTimeout(resolve));
|
|
124
|
-
return value(a) + 5;
|
|
125
|
-
}));
|
|
126
|
-
assert.strictEqual(yield b.value, 10);
|
|
127
|
-
a.value = 6;
|
|
128
|
-
assert.strictEqual(yield b.value, 11);
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
it('get value while computing', function () {
|
|
132
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
133
|
-
const a = new Computed(() => __awaiter(this, void 0, void 0, function* () {
|
|
134
|
-
yield new Promise(resolve => setTimeout(resolve));
|
|
135
|
-
return 5;
|
|
136
|
-
}));
|
|
137
|
-
a.value;
|
|
138
|
-
assert.strictEqual(yield a.value, 5);
|
|
101
|
+
it('getter', async function () {
|
|
102
|
+
const a = new Computed(async () => {
|
|
103
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
104
|
+
return 5;
|
|
139
105
|
});
|
|
106
|
+
assert.strictEqual(await a.value, 5);
|
|
140
107
|
});
|
|
141
|
-
it('
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
return value(b);
|
|
147
|
-
}));
|
|
148
|
-
// @ts-expect-error
|
|
149
|
-
const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
|
|
150
|
-
yield new Promise(resolve => setTimeout(resolve));
|
|
151
|
-
return value(a);
|
|
152
|
-
}));
|
|
153
|
-
assert.rejects(() => __awaiter(this, void 0, void 0, function* () { return yield a.value; }));
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
it('dependency changed while computing', function () {
|
|
157
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
-
const a = new Ref(5);
|
|
159
|
-
const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () { return value(a) + 5; }));
|
|
160
|
-
b.value; // trigger compute
|
|
161
|
-
a.value = 8;
|
|
162
|
-
assert.strictEqual(yield b.value, 13);
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
it('old dependency changed while computing', function () {
|
|
166
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
167
|
-
let gate = 0;
|
|
168
|
-
const a = new Ref(5);
|
|
169
|
-
const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
|
|
170
|
-
gate++;
|
|
171
|
-
yield new Promise(resolve => setTimeout(resolve));
|
|
172
|
-
return value(a) + 2;
|
|
173
|
-
}));
|
|
174
|
-
assert.strictEqual(yield b.value, 7);
|
|
175
|
-
b.invalidate();
|
|
176
|
-
const promise = b.value;
|
|
177
|
-
a.value = 6;
|
|
178
|
-
assert.strictEqual(yield promise, 8);
|
|
179
|
-
assert.strictEqual(gate, 2);
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
it('new dependency changed while computing', function () {
|
|
183
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
184
|
-
let gate = 0;
|
|
185
|
-
const a = new Ref(5);
|
|
186
|
-
const b = new Ref(10);
|
|
187
|
-
const c = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
|
|
188
|
-
gate++;
|
|
189
|
-
yield new Promise(resolve => setTimeout(resolve, 50));
|
|
190
|
-
let sum = value(a);
|
|
191
|
-
yield new Promise(resolve => setTimeout(resolve, 50));
|
|
192
|
-
sum += value(b);
|
|
193
|
-
return sum;
|
|
194
|
-
}));
|
|
195
|
-
assert.strictEqual(yield c.value, 15);
|
|
196
|
-
c.invalidate();
|
|
197
|
-
const promise = c.value;
|
|
198
|
-
yield new Promise(resolve => setTimeout(resolve, 60));
|
|
199
|
-
a.value = 10;
|
|
200
|
-
assert.strictEqual(yield promise, 20);
|
|
201
|
-
assert.strictEqual(gate, 3);
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
it('fallback to primitive while computing', function () {
|
|
205
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
206
|
-
const a = new Ref(5);
|
|
207
|
-
const b = new Ref(10);
|
|
208
|
-
const c = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
|
|
209
|
-
if (value(a) < 10) {
|
|
210
|
-
yield new Promise(resolve => setTimeout(resolve, 50));
|
|
211
|
-
return value(b) + 5;
|
|
212
|
-
}
|
|
213
|
-
return 2;
|
|
214
|
-
}));
|
|
215
|
-
const promise = c.value;
|
|
216
|
-
yield new Promise(resolve => setTimeout(resolve, 20));
|
|
217
|
-
a.value = 10;
|
|
218
|
-
assert.strictEqual(yield promise, 2);
|
|
108
|
+
it('tracks async dependencies', async function () {
|
|
109
|
+
const a = new Ref(5);
|
|
110
|
+
const b = new Computed(async (value) => {
|
|
111
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
112
|
+
return value(a) + 5;
|
|
219
113
|
});
|
|
114
|
+
assert.strictEqual(await b.value, 10);
|
|
115
|
+
a.value = 6;
|
|
116
|
+
assert.strictEqual(await b.value, 11);
|
|
220
117
|
});
|
|
221
|
-
it('
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
118
|
+
it('get value while computing', async function () {
|
|
119
|
+
const a = new Computed(async () => {
|
|
120
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
121
|
+
return 5;
|
|
122
|
+
});
|
|
123
|
+
a.value;
|
|
124
|
+
assert.strictEqual(await a.value, 5);
|
|
125
|
+
});
|
|
126
|
+
it('detect circular dependency', async function () {
|
|
127
|
+
// @ts-expect-error
|
|
128
|
+
const a = new Computed(async (value) => {
|
|
129
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
130
|
+
return value(b);
|
|
131
|
+
});
|
|
132
|
+
// @ts-expect-error
|
|
133
|
+
const b = new Computed(async (value) => {
|
|
134
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
135
|
+
return value(a);
|
|
136
|
+
});
|
|
137
|
+
assert.rejects(async () => await a.value);
|
|
138
|
+
});
|
|
139
|
+
it('dependency changed while computing', async function () {
|
|
140
|
+
const a = new Ref(5);
|
|
141
|
+
const b = new Computed(async (value) => value(a) + 5);
|
|
142
|
+
b.value; // trigger compute
|
|
143
|
+
a.value = 8;
|
|
144
|
+
assert.strictEqual(await b.value, 13);
|
|
145
|
+
});
|
|
146
|
+
it('old dependency changed while computing', async function () {
|
|
147
|
+
let gate = 0;
|
|
148
|
+
const a = new Ref(5);
|
|
149
|
+
const b = new Computed(async (value) => {
|
|
150
|
+
gate++;
|
|
151
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
152
|
+
return value(a) + 2;
|
|
153
|
+
});
|
|
154
|
+
assert.strictEqual(await b.value, 7);
|
|
155
|
+
b.invalidate();
|
|
156
|
+
const promise = b.value;
|
|
157
|
+
a.value = 6;
|
|
158
|
+
assert.strictEqual(await promise, 8);
|
|
159
|
+
assert.strictEqual(gate, 2);
|
|
160
|
+
});
|
|
161
|
+
it('new dependency changed while computing', async function () {
|
|
162
|
+
let gate = 0;
|
|
163
|
+
const a = new Ref(5);
|
|
164
|
+
const b = new Ref(10);
|
|
165
|
+
const c = new Computed(async (value) => {
|
|
166
|
+
gate++;
|
|
167
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
168
|
+
let sum = value(a);
|
|
169
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
170
|
+
sum += value(b);
|
|
171
|
+
return sum;
|
|
172
|
+
});
|
|
173
|
+
assert.strictEqual(await c.value, 15);
|
|
174
|
+
c.invalidate();
|
|
175
|
+
const promise = c.value;
|
|
176
|
+
await new Promise(resolve => setTimeout(resolve, 60));
|
|
177
|
+
a.value = 10;
|
|
178
|
+
assert.strictEqual(await promise, 20);
|
|
179
|
+
assert.strictEqual(gate, 3);
|
|
180
|
+
});
|
|
181
|
+
it('fallback to primitive while computing', async function () {
|
|
182
|
+
const a = new Ref(5);
|
|
183
|
+
const b = new Ref(10);
|
|
184
|
+
const c = new Computed(async (value) => {
|
|
185
|
+
if (value(a) < 10) {
|
|
186
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
187
|
+
return value(b) + 5;
|
|
188
|
+
}
|
|
189
|
+
return 2;
|
|
190
|
+
});
|
|
191
|
+
const promise = c.value;
|
|
192
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
193
|
+
a.value = 10;
|
|
194
|
+
assert.strictEqual(await promise, 2);
|
|
195
|
+
});
|
|
196
|
+
it('throw error', async function () {
|
|
197
|
+
const a = new Computed(async () => {
|
|
198
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
199
|
+
throw new Error();
|
|
243
200
|
});
|
|
201
|
+
assert.rejects(() => a.value);
|
|
202
|
+
});
|
|
203
|
+
it('dispose computed', async function () {
|
|
204
|
+
const a = new Ref(5);
|
|
205
|
+
let gate = 0;
|
|
206
|
+
const b = new Computed(value => {
|
|
207
|
+
gate++;
|
|
208
|
+
return value(a) + 2;
|
|
209
|
+
});
|
|
210
|
+
b.value;
|
|
211
|
+
assert.strictEqual(gate, 1);
|
|
212
|
+
b.dispose();
|
|
213
|
+
b.value;
|
|
214
|
+
assert.strictEqual(gate, 2);
|
|
244
215
|
});
|
|
245
216
|
});
|
package/lib/ref.js
CHANGED
package/lib/tracker.js
CHANGED
package/lib/watcher.js
CHANGED
|
@@ -6,9 +6,11 @@ var WatchState;
|
|
|
6
6
|
})(WatchState || (WatchState = {}));
|
|
7
7
|
;
|
|
8
8
|
export default class Watcher extends Tracker {
|
|
9
|
+
onChange;
|
|
10
|
+
dependency;
|
|
11
|
+
state = WatchState.Valid;
|
|
9
12
|
constructor(dependency, onChange, immediate = true) {
|
|
10
13
|
super();
|
|
11
|
-
this.state = WatchState.Valid;
|
|
12
14
|
this.onChange = onChange;
|
|
13
15
|
this.dependency = dependency;
|
|
14
16
|
dependency.addDependent(this);
|
|
@@ -18,13 +20,18 @@ export default class Watcher extends Tracker {
|
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
invalidate() {
|
|
21
|
-
this.state = WatchState.Uncertain;
|
|
22
|
-
const oldValue = this._value;
|
|
23
|
-
this._value = this.dependency.value;
|
|
24
23
|
if (this.state === WatchState.Uncertain) {
|
|
25
|
-
|
|
26
|
-
this.state = WatchState.Valid;
|
|
24
|
+
return;
|
|
27
25
|
}
|
|
26
|
+
this.state = WatchState.Uncertain;
|
|
27
|
+
Promise.resolve().then(() => {
|
|
28
|
+
const oldValue = this._value;
|
|
29
|
+
this._value = this.dependency.value;
|
|
30
|
+
if (this.state === WatchState.Uncertain) {
|
|
31
|
+
this.onChange(this._value, oldValue);
|
|
32
|
+
this.state = WatchState.Valid;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
28
35
|
}
|
|
29
36
|
validate() {
|
|
30
37
|
this.state = WatchState.Valid;
|
package/lib/watcher.test.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
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
1
|
import 'mocha';
|
|
11
2
|
import assert from 'assert';
|
|
12
3
|
import { Computed, Ref, Watcher } from './index.js';
|
|
@@ -19,55 +10,53 @@ describe('watcher', function () {
|
|
|
19
10
|
}, false);
|
|
20
11
|
a.value = 6;
|
|
21
12
|
});
|
|
22
|
-
it('async', function () {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return 10;
|
|
27
|
-
}));
|
|
28
|
-
const result = yield new Promise(resolve => new Watcher(a, resolve));
|
|
29
|
-
assert.strictEqual(result, 10);
|
|
13
|
+
it('async', async function () {
|
|
14
|
+
const a = new Computed(async () => {
|
|
15
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
16
|
+
return 10;
|
|
30
17
|
});
|
|
18
|
+
const result = await new Promise(resolve => new Watcher(a, resolve));
|
|
19
|
+
assert.strictEqual(result, 10);
|
|
31
20
|
});
|
|
32
|
-
it('sync cancel', function () {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return value(a) % 2;
|
|
37
|
-
});
|
|
38
|
-
let gate = 0;
|
|
39
|
-
new Watcher(b, () => {
|
|
40
|
-
gate++;
|
|
41
|
-
}, false);
|
|
42
|
-
a.value = 6;
|
|
43
|
-
assert.strictEqual(gate, 1);
|
|
44
|
-
a.value = 7;
|
|
45
|
-
a.value = 9;
|
|
46
|
-
a.value = 11;
|
|
47
|
-
assert.strictEqual(gate, 2);
|
|
21
|
+
it('sync cancel', async function () {
|
|
22
|
+
const a = new Ref(5);
|
|
23
|
+
const b = new Computed((value) => {
|
|
24
|
+
return value(a) % 2;
|
|
48
25
|
});
|
|
26
|
+
let gate = 0;
|
|
27
|
+
new Watcher(b, () => {
|
|
28
|
+
gate++;
|
|
29
|
+
}, false);
|
|
30
|
+
a.value = 6;
|
|
31
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
32
|
+
assert.strictEqual(gate, 1);
|
|
33
|
+
a.value = 7;
|
|
34
|
+
a.value = 9;
|
|
35
|
+
a.value = 11;
|
|
36
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
37
|
+
assert.strictEqual(gate, 2);
|
|
49
38
|
});
|
|
50
|
-
it('async sync cancel', function () {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
let gate = 0;
|
|
58
|
-
new Watcher(b, () => {
|
|
59
|
-
gate++;
|
|
60
|
-
}, false);
|
|
61
|
-
yield new Promise(resolve => setTimeout(resolve, 10));
|
|
62
|
-
a.value = 6;
|
|
63
|
-
yield new Promise(resolve => setTimeout(resolve, 10));
|
|
64
|
-
assert.strictEqual(gate, 1);
|
|
65
|
-
a.value = 7;
|
|
66
|
-
yield new Promise(resolve => setTimeout(resolve, 10));
|
|
67
|
-
assert.strictEqual(gate, 2);
|
|
68
|
-
a.value = 9;
|
|
69
|
-
yield new Promise(resolve => setTimeout(resolve, 10));
|
|
70
|
-
assert.strictEqual(gate, 3);
|
|
39
|
+
it('async sync cancel', async function () {
|
|
40
|
+
const a = new Ref(5);
|
|
41
|
+
let computedGate = 0;
|
|
42
|
+
const b = new Computed(async (value) => {
|
|
43
|
+
computedGate++;
|
|
44
|
+
new Promise(resolve => setTimeout(resolve));
|
|
45
|
+
return value(a) % 2;
|
|
71
46
|
});
|
|
47
|
+
let watcherGate = 0;
|
|
48
|
+
new Watcher(b, () => {
|
|
49
|
+
watcherGate++;
|
|
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(computedGate, 2);
|
|
55
|
+
assert.strictEqual(watcherGate, 1);
|
|
56
|
+
a.value = 7;
|
|
57
|
+
a.value = 9;
|
|
58
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
59
|
+
assert.strictEqual(computedGate, 3);
|
|
60
|
+
assert.strictEqual(watcherGate, 2);
|
|
72
61
|
});
|
|
73
62
|
});
|
package/package.json
CHANGED
package/src/tsconfig.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
10
10
|
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
11
11
|
/* Language and Environment */
|
|
12
|
-
"target": "
|
|
12
|
+
"target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
13
13
|
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
14
14
|
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
15
15
|
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
package/src/watcher.test.ts
CHANGED
|
@@ -31,32 +31,36 @@ describe('watcher', function () {
|
|
|
31
31
|
gate++;
|
|
32
32
|
}, false);
|
|
33
33
|
a.value = 6;
|
|
34
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
34
35
|
assert.strictEqual(gate, 1);
|
|
35
36
|
a.value = 7;
|
|
36
37
|
a.value = 9;
|
|
37
38
|
a.value = 11;
|
|
39
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
38
40
|
assert.strictEqual(gate, 2);
|
|
39
41
|
});
|
|
40
42
|
|
|
41
43
|
it('async sync cancel', async function () {
|
|
42
44
|
const a = new Ref(5);
|
|
45
|
+
let computedGate = 0;
|
|
43
46
|
const b = new Computed(async (value) => {
|
|
47
|
+
computedGate++;
|
|
44
48
|
new Promise(resolve => setTimeout(resolve));
|
|
45
49
|
return value(a) % 2;
|
|
46
50
|
});
|
|
47
|
-
let
|
|
51
|
+
let watcherGate = 0;
|
|
48
52
|
new Watcher(b, () => {
|
|
49
|
-
|
|
53
|
+
watcherGate++;
|
|
50
54
|
}, false);
|
|
51
55
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
52
56
|
a.value = 6;
|
|
53
57
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
54
|
-
assert.strictEqual(
|
|
58
|
+
assert.strictEqual(computedGate, 2);
|
|
59
|
+
assert.strictEqual(watcherGate, 1);
|
|
55
60
|
a.value = 7;
|
|
56
|
-
await new Promise(resolve => setTimeout(resolve, 10));
|
|
57
|
-
assert.strictEqual(gate, 2);
|
|
58
61
|
a.value = 9;
|
|
59
62
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
60
|
-
assert.strictEqual(
|
|
63
|
+
assert.strictEqual(computedGate, 3);
|
|
64
|
+
assert.strictEqual(watcherGate, 2);
|
|
61
65
|
});
|
|
62
66
|
});
|
package/src/watcher.ts
CHANGED
|
@@ -28,13 +28,18 @@ export default class Watcher<T> extends Tracker<T> implements Dependent {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
public invalidate() {
|
|
31
|
-
this.state = WatchState.Uncertain;
|
|
32
|
-
const oldValue = this._value;
|
|
33
|
-
this._value = this.dependency.value;
|
|
34
31
|
if (this.state === WatchState.Uncertain) {
|
|
35
|
-
|
|
36
|
-
this.state = WatchState.Valid;
|
|
32
|
+
return;
|
|
37
33
|
}
|
|
34
|
+
this.state = WatchState.Uncertain;
|
|
35
|
+
Promise.resolve().then(() => {
|
|
36
|
+
const oldValue = this._value;
|
|
37
|
+
this._value = this.dependency.value;
|
|
38
|
+
if (this.state === WatchState.Uncertain) {
|
|
39
|
+
this.onChange(this._value, oldValue);
|
|
40
|
+
this.state = WatchState.Valid;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
public validate() {
|
package/types/watcher.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"watcher.d.ts","sourceRoot":"","sources":["../src/watcher.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AACzC,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,OAAO,MAAM,cAAc,CAAC;AAOnC,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC;AAE3D,MAAM,CAAC,OAAO,OAAO,OAAO,CAAC,CAAC,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAE,YAAW,SAAS;IAEnE,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,UAAU,CAAgB;IAClC,OAAO,CAAC,KAAK,CAAoB;gBAErB,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,SAAS,GAAE,OAAc;IAYpF,UAAU;
|
|
1
|
+
{"version":3,"file":"watcher.d.ts","sourceRoot":"","sources":["../src/watcher.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AACzC,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,OAAO,MAAM,cAAc,CAAC;AAOnC,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC;AAE3D,MAAM,CAAC,OAAO,OAAO,OAAO,CAAC,CAAC,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAE,YAAW,SAAS;IAEnE,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,UAAU,CAAgB;IAClC,OAAO,CAAC,KAAK,CAAoB;gBAErB,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,SAAS,GAAE,OAAc;IAYpF,UAAU;IAeV,QAAQ;IAIR,OAAO;CAGjB"}
|