async-reactivity 2.2.0 → 2.2.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.
- package/lib/computed.js +24 -18
- package/package.json +1 -1
- package/src/computed.ts +24 -18
- package/types/computed.d.ts +1 -0
- package/types/computed.d.ts.map +1 -1
package/lib/computed.js
CHANGED
|
@@ -5,6 +5,7 @@ import Effect, { EffectState, InSyncSymbol } from "./effect.js";
|
|
|
5
5
|
class CircularDependencyError extends Error {
|
|
6
6
|
}
|
|
7
7
|
export default class Computed extends Effect {
|
|
8
|
+
oldValue;
|
|
8
9
|
_value;
|
|
9
10
|
getter;
|
|
10
11
|
isEqual;
|
|
@@ -12,7 +13,7 @@ export default class Computed extends Effect {
|
|
|
12
13
|
deferrer;
|
|
13
14
|
constructor(getter, isEqual = (defaultIsEqual), timeToLive) {
|
|
14
15
|
super(((value, _firstRun, abortSignal) => {
|
|
15
|
-
return getter(value, this.
|
|
16
|
+
return getter(value, this.oldValue, abortSignal);
|
|
16
17
|
}), true);
|
|
17
18
|
this.getter = getter;
|
|
18
19
|
this.isEqual = isEqual;
|
|
@@ -26,32 +27,37 @@ export default class Computed extends Effect {
|
|
|
26
27
|
}
|
|
27
28
|
get value() {
|
|
28
29
|
if (this.state === EffectState.Initial || this.state === EffectState.Scheduled) {
|
|
29
|
-
|
|
30
|
+
this.oldValue = this._value;
|
|
30
31
|
const newValue = this.run();
|
|
31
|
-
if (newValue
|
|
32
|
-
|
|
32
|
+
if (newValue instanceof Promise) {
|
|
33
|
+
// @ts-expect-error
|
|
34
|
+
this._value = Promise.all([newValue, this.oldValue])
|
|
35
|
+
.then(([newValue, oldValue]) => {
|
|
36
|
+
if (newValue === InSyncSymbol) {
|
|
37
|
+
this.validateDependents();
|
|
38
|
+
return oldValue;
|
|
39
|
+
}
|
|
40
|
+
if (this.dependents.size > 0 && this.isEqual(newValue, oldValue)) {
|
|
41
|
+
this.validateDependents();
|
|
42
|
+
}
|
|
43
|
+
return newValue;
|
|
44
|
+
}).finally(() => {
|
|
45
|
+
if (this.state === EffectState.Waiting) {
|
|
46
|
+
this.oldValue = undefined;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
33
49
|
}
|
|
34
50
|
else {
|
|
35
|
-
if (newValue
|
|
36
|
-
|
|
37
|
-
this._value = Promise.all([newValue, oldValue])
|
|
38
|
-
.then(([newValue, oldValue]) => {
|
|
39
|
-
if (newValue === InSyncSymbol) {
|
|
40
|
-
this.validateDependents();
|
|
41
|
-
return oldValue;
|
|
42
|
-
}
|
|
43
|
-
if (this.dependents.size > 0 && this.isEqual(newValue, oldValue)) {
|
|
44
|
-
this.validateDependents();
|
|
45
|
-
}
|
|
46
|
-
return newValue;
|
|
47
|
-
});
|
|
51
|
+
if (newValue === InSyncSymbol) {
|
|
52
|
+
this.validateDependents();
|
|
48
53
|
}
|
|
49
54
|
else {
|
|
50
55
|
this._value = newValue;
|
|
51
|
-
if (this.dependents.size > 0 && this.isEqual(newValue, oldValue)) {
|
|
56
|
+
if (this.dependents.size > 0 && this.isEqual(newValue, this.oldValue)) {
|
|
52
57
|
this.validateDependents();
|
|
53
58
|
}
|
|
54
59
|
}
|
|
60
|
+
this.oldValue = undefined;
|
|
55
61
|
}
|
|
56
62
|
}
|
|
57
63
|
return this._value;
|
package/package.json
CHANGED
package/src/computed.ts
CHANGED
|
@@ -12,6 +12,7 @@ export declare type ComputeFuncScoped<T1, T2> = (value: TrackValue, scope: T1, p
|
|
|
12
12
|
class CircularDependencyError extends Error { }
|
|
13
13
|
|
|
14
14
|
export default class Computed<T> extends Effect implements Dependent, Dependency<T> {
|
|
15
|
+
private oldValue?: T;
|
|
15
16
|
private _value?: T;
|
|
16
17
|
getter: ComputeFunc<T>;
|
|
17
18
|
isEqual: typeof defaultIsEqual<T>;
|
|
@@ -21,7 +22,7 @@ export default class Computed<T> extends Effect implements Dependent, Dependency
|
|
|
21
22
|
constructor(getter: ComputeFunc<T>, isEqual = defaultIsEqual<T>, timeToLive?: number) {
|
|
22
23
|
|
|
23
24
|
super(((value, _firstRun, abortSignal) => {
|
|
24
|
-
return getter(value, this.
|
|
25
|
+
return getter(value, this.oldValue, abortSignal);
|
|
25
26
|
}), true);
|
|
26
27
|
|
|
27
28
|
this.getter = getter;
|
|
@@ -38,30 +39,35 @@ export default class Computed<T> extends Effect implements Dependent, Dependency
|
|
|
38
39
|
|
|
39
40
|
public get value(): T {
|
|
40
41
|
if (this.state === EffectState.Initial || this.state === EffectState.Scheduled) {
|
|
41
|
-
|
|
42
|
+
this.oldValue = this._value!;
|
|
42
43
|
const newValue = this.run() as T;
|
|
43
|
-
if (newValue
|
|
44
|
-
|
|
44
|
+
if (newValue instanceof Promise) {
|
|
45
|
+
// @ts-expect-error
|
|
46
|
+
this._value = Promise.all([newValue, this.oldValue])
|
|
47
|
+
.then(([newValue, oldValue]) => {
|
|
48
|
+
if (newValue === InSyncSymbol) {
|
|
49
|
+
this.validateDependents();
|
|
50
|
+
return oldValue;
|
|
51
|
+
}
|
|
52
|
+
if (this.dependents.size > 0 && this.isEqual(newValue, oldValue)) {
|
|
53
|
+
this.validateDependents();
|
|
54
|
+
}
|
|
55
|
+
return newValue;
|
|
56
|
+
}).finally(() => {
|
|
57
|
+
if (this.state === EffectState.Waiting) {
|
|
58
|
+
this.oldValue = undefined;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
45
61
|
} else {
|
|
46
|
-
if (newValue
|
|
47
|
-
|
|
48
|
-
this._value = Promise.all([newValue, oldValue])
|
|
49
|
-
.then(([newValue, oldValue]) => {
|
|
50
|
-
if (newValue === InSyncSymbol) {
|
|
51
|
-
this.validateDependents();
|
|
52
|
-
return oldValue;
|
|
53
|
-
}
|
|
54
|
-
if (this.dependents.size > 0 && this.isEqual(newValue, oldValue)) {
|
|
55
|
-
this.validateDependents();
|
|
56
|
-
}
|
|
57
|
-
return newValue;
|
|
58
|
-
});
|
|
62
|
+
if (newValue === InSyncSymbol) {
|
|
63
|
+
this.validateDependents();
|
|
59
64
|
} else {
|
|
60
65
|
this._value = newValue;
|
|
61
|
-
if (this.dependents.size > 0 && this.isEqual(newValue, oldValue)) {
|
|
66
|
+
if (this.dependents.size > 0 && this.isEqual(newValue, this.oldValue)) {
|
|
62
67
|
this.validateDependents();
|
|
63
68
|
}
|
|
64
69
|
}
|
|
70
|
+
this.oldValue = undefined;
|
|
65
71
|
}
|
|
66
72
|
}
|
|
67
73
|
|
package/types/computed.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export declare type TrackValue = (<T>(dependency: Dependency<T>) => T) & (<T>(de
|
|
|
6
6
|
export declare type ComputeFunc<T> = (value: TrackValue, previousValue: T | undefined, abortSignal: AbortSignal) => T;
|
|
7
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 Effect implements Dependent, Dependency<T> {
|
|
9
|
+
private oldValue?;
|
|
9
10
|
private _value?;
|
|
10
11
|
getter: ComputeFunc<T>;
|
|
11
12
|
isEqual: typeof defaultIsEqual<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,cAAc,MAAM,qBAAqB,CAAC;AAGjD,OAAO,MAAqC,MAAM,aAAa,CAAC;AAEhE,MAAM,CAAC,OAAO,MAAM,UAAU,GAAG,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,KAAK,CAAC,GAAG,SAAS,CAAC,CAAC;AACvI,MAAM,CAAC,OAAO,MAAM,WAAW,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