async-reactivity 2.0.21 → 2.0.22
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 -2
- package/lib/computed.test.js +17 -0
- package/package.json +1 -1
- package/src/computed.test.ts +18 -0
- package/src/computed.ts +9 -4
- package/types/computed.d.ts +3 -2
- package/types/computed.d.ts.map +1 -1
package/lib/computed.js
CHANGED
|
@@ -9,7 +9,6 @@ var ComputedState;
|
|
|
9
9
|
ComputedState[ComputedState["Uncertain"] = 2] = "Uncertain";
|
|
10
10
|
ComputedState[ComputedState["Computing"] = 3] = "Computing";
|
|
11
11
|
})(ComputedState || (ComputedState = {}));
|
|
12
|
-
;
|
|
13
12
|
class CircularDependencyError extends Error {
|
|
14
13
|
}
|
|
15
14
|
export default class Computed extends Tracker {
|
|
@@ -21,6 +20,7 @@ export default class Computed extends Tracker {
|
|
|
21
20
|
computePromiseActions;
|
|
22
21
|
lastComputeAttemptPromise;
|
|
23
22
|
deferrer;
|
|
23
|
+
abortController;
|
|
24
24
|
constructor(getter, isEqual = (defaultIsEqual), timeToLive) {
|
|
25
25
|
super();
|
|
26
26
|
this.getter = getter;
|
|
@@ -70,9 +70,13 @@ export default class Computed extends Tracker {
|
|
|
70
70
|
return this._value;
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
+
else if (this.state === ComputedState.Computing) {
|
|
74
|
+
this.abortController?.abort();
|
|
75
|
+
}
|
|
73
76
|
this.state = ComputedState.Computing;
|
|
74
77
|
this.clearDependencies(true);
|
|
75
|
-
|
|
78
|
+
this.abortController = new AbortController();
|
|
79
|
+
const newValue = this.getter(this.trackDependency, this._value, this.abortController.signal);
|
|
76
80
|
if (this.isEqual(newValue, this._value)) {
|
|
77
81
|
this.handlePromiseThen(this.lastComputeAttemptPromise, this._value);
|
|
78
82
|
this.validateDependents();
|
|
@@ -120,6 +124,7 @@ export default class Computed extends Tracker {
|
|
|
120
124
|
finalizeComputing() {
|
|
121
125
|
this.state = ComputedState.Valid;
|
|
122
126
|
this.lastComputeAttemptPromise = undefined;
|
|
127
|
+
this.abortController = undefined;
|
|
123
128
|
this.prepareComputePromise();
|
|
124
129
|
}
|
|
125
130
|
invalidate() {
|
package/lib/computed.test.js
CHANGED
|
@@ -186,6 +186,23 @@ describe('async computed', function () {
|
|
|
186
186
|
a.value = 8;
|
|
187
187
|
assert.strictEqual(await b.value, 13);
|
|
188
188
|
});
|
|
189
|
+
it('dependency changed while computing - abortSignal', async function () {
|
|
190
|
+
const a = new Ref(5);
|
|
191
|
+
let gate = 0;
|
|
192
|
+
const b = new Computed(async (value, _previousValue, abortSignal) => {
|
|
193
|
+
const dep = value(a);
|
|
194
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
195
|
+
if (abortSignal.aborted) {
|
|
196
|
+
throw new Error();
|
|
197
|
+
}
|
|
198
|
+
gate++;
|
|
199
|
+
return dep + 5;
|
|
200
|
+
});
|
|
201
|
+
b.value; // trigger compute
|
|
202
|
+
a.value = 8;
|
|
203
|
+
await b.value;
|
|
204
|
+
assert.strictEqual(gate, 1);
|
|
205
|
+
});
|
|
189
206
|
it('old dependency changed while computing', async function () {
|
|
190
207
|
let gate = 0;
|
|
191
208
|
const a = new Ref(5);
|
package/package.json
CHANGED
package/src/computed.test.ts
CHANGED
|
@@ -219,6 +219,24 @@ describe('async computed', function () {
|
|
|
219
219
|
assert.strictEqual(await b.value, 13);
|
|
220
220
|
});
|
|
221
221
|
|
|
222
|
+
it('dependency changed while computing - abortSignal', async function () {
|
|
223
|
+
const a = new Ref(5);
|
|
224
|
+
let gate = 0;
|
|
225
|
+
const b = new Computed(async (value, _previousValue, abortSignal) => {
|
|
226
|
+
const dep = value(a);
|
|
227
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
228
|
+
if (abortSignal.aborted) {
|
|
229
|
+
throw new Error();
|
|
230
|
+
}
|
|
231
|
+
gate++;
|
|
232
|
+
return dep + 5;
|
|
233
|
+
});
|
|
234
|
+
b.value; // trigger compute
|
|
235
|
+
a.value = 8;
|
|
236
|
+
await b.value;
|
|
237
|
+
assert.strictEqual(gate, 1);
|
|
238
|
+
});
|
|
239
|
+
|
|
222
240
|
it('old dependency changed while computing', async function () {
|
|
223
241
|
let gate = 0;
|
|
224
242
|
const a = new Ref(5);
|
package/src/computed.ts
CHANGED
|
@@ -6,15 +6,15 @@ import { Deferrer } from "./deferrer.js";
|
|
|
6
6
|
import { debounce } from 'lodash-es';
|
|
7
7
|
|
|
8
8
|
export declare type TrackValue = <T>(dependency: Dependency<T>) => T;
|
|
9
|
-
export declare type ComputeFunc<T> = (value: TrackValue, previousValue
|
|
10
|
-
export declare type ComputeFuncScoped<T1, T2> = (value: TrackValue, scope: T1, previousValue
|
|
9
|
+
export declare type ComputeFunc<T> = (value: TrackValue, previousValue: T | undefined, abortSignal: AbortSignal) => T;
|
|
10
|
+
export declare type ComputeFuncScoped<T1, T2> = (value: TrackValue, scope: T1, previousValue: T2 | undefined, abortSignal: AbortSignal) => T2;
|
|
11
11
|
|
|
12
12
|
enum ComputedState {
|
|
13
13
|
Invalid,
|
|
14
14
|
Valid,
|
|
15
15
|
Uncertain,
|
|
16
16
|
Computing
|
|
17
|
-
}
|
|
17
|
+
}
|
|
18
18
|
|
|
19
19
|
class CircularDependencyError extends Error { }
|
|
20
20
|
|
|
@@ -27,6 +27,7 @@ export default class Computed<T> extends Tracker<T> implements Dependent, Depend
|
|
|
27
27
|
private computePromiseActions?: { resolve: Function, reject: Function };
|
|
28
28
|
private lastComputeAttemptPromise?: Promise<void>;
|
|
29
29
|
private deferrer?: Deferrer;
|
|
30
|
+
private abortController?: AbortController;
|
|
30
31
|
|
|
31
32
|
constructor(getter: ComputeFunc<T>, isEqual = defaultIsEqual<T>, timeToLive?: number) {
|
|
32
33
|
super();
|
|
@@ -80,12 +81,15 @@ export default class Computed<T> extends Tracker<T> implements Dependent, Depend
|
|
|
80
81
|
this.validateDependents();
|
|
81
82
|
return this._value!;
|
|
82
83
|
}
|
|
84
|
+
} else if (this.state === ComputedState.Computing) {
|
|
85
|
+
this.abortController?.abort();
|
|
83
86
|
}
|
|
84
87
|
|
|
85
88
|
this.state = ComputedState.Computing;
|
|
86
89
|
this.clearDependencies(true);
|
|
90
|
+
this.abortController = new AbortController();
|
|
87
91
|
|
|
88
|
-
const newValue: T = this.getter(this.trackDependency, this._value);
|
|
92
|
+
const newValue: T = this.getter(this.trackDependency, this._value, this.abortController.signal);
|
|
89
93
|
if (this.isEqual(newValue, this._value!)) {
|
|
90
94
|
this.handlePromiseThen(this.lastComputeAttemptPromise!, this._value);
|
|
91
95
|
this.validateDependents();
|
|
@@ -137,6 +141,7 @@ export default class Computed<T> extends Tracker<T> implements Dependent, Depend
|
|
|
137
141
|
private finalizeComputing() {
|
|
138
142
|
this.state = ComputedState.Valid;
|
|
139
143
|
this.lastComputeAttemptPromise = undefined;
|
|
144
|
+
this.abortController = undefined;
|
|
140
145
|
this.prepareComputePromise();
|
|
141
146
|
}
|
|
142
147
|
|
package/types/computed.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ import Dependent from "./dependent.js";
|
|
|
3
3
|
import Tracker from "./tracker.js";
|
|
4
4
|
import defaultIsEqual from "./defaultIsEqual.js";
|
|
5
5
|
export declare type TrackValue = <T>(dependency: Dependency<T>) => T;
|
|
6
|
-
export declare type ComputeFunc<T> = (value: TrackValue, previousValue
|
|
7
|
-
export declare type ComputeFuncScoped<T1, T2> = (value: TrackValue, scope: T1, previousValue
|
|
6
|
+
export declare type ComputeFunc<T> = (value: TrackValue, previousValue: T | undefined, abortSignal: AbortSignal) => T;
|
|
7
|
+
export declare type ComputeFuncScoped<T1, T2> = (value: TrackValue, scope: T1, previousValue: T2 | undefined, abortSignal: AbortSignal) => T2;
|
|
8
8
|
export default class Computed<T> extends Tracker<T> implements Dependent, Dependency<T> {
|
|
9
9
|
getter: ComputeFunc<T>;
|
|
10
10
|
isEqual: typeof defaultIsEqual<T>;
|
|
@@ -14,6 +14,7 @@ export default class Computed<T> extends Tracker<T> implements Dependent, Depend
|
|
|
14
14
|
private computePromiseActions?;
|
|
15
15
|
private lastComputeAttemptPromise?;
|
|
16
16
|
private deferrer?;
|
|
17
|
+
private abortController?;
|
|
17
18
|
constructor(getter: ComputeFunc<T>, isEqual?: (v1: T, v2: T) => boolean, timeToLive?: number);
|
|
18
19
|
private prepareComputePromise;
|
|
19
20
|
get value(): T;
|
package/types/computed.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computed.d.ts","sourceRoot":"","sources":["../src/computed.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AACzC,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,OAAO,MAAM,cAAc,CAAC;AACnC,OAAO,cAAc,MAAM,qBAAqB,CAAC;AAIjD,MAAM,CAAC,OAAO,MAAM,UAAU,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AACrE,MAAM,CAAC,OAAO,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,UAAU,EAAE,aAAa,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"computed.d.ts","sourceRoot":"","sources":["../src/computed.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AACzC,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,OAAO,MAAM,cAAc,CAAC;AACnC,OAAO,cAAc,MAAM,qBAAqB,CAAC;AAIjD,MAAM,CAAC,OAAO,MAAM,UAAU,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AACrE,MAAM,CAAC,OAAO,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,CAAC,GAAG,SAAS,EAAE,WAAW,EAAE,W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