async-reactivity 2.2.0 → 2.2.1
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 +5 -4
- package/package.json +1 -1
- package/src/computed.ts +5 -4
- package/types/computed.d.ts +1 -0
- package/types/computed.d.ts.map +1 -1
- package/lib/tracker.js +0 -18
- package/types/tracker.d.ts +0 -10
- package/types/tracker.d.ts.map +0 -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,7 +27,7 @@ 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
32
|
if (newValue === InSyncSymbol) {
|
|
32
33
|
this.validateDependents();
|
|
@@ -34,7 +35,7 @@ export default class Computed extends Effect {
|
|
|
34
35
|
else {
|
|
35
36
|
if (newValue instanceof Promise) {
|
|
36
37
|
// @ts-expect-error
|
|
37
|
-
this._value = Promise.all([newValue, oldValue])
|
|
38
|
+
this._value = Promise.all([newValue, this.oldValue])
|
|
38
39
|
.then(([newValue, oldValue]) => {
|
|
39
40
|
if (newValue === InSyncSymbol) {
|
|
40
41
|
this.validateDependents();
|
|
@@ -48,7 +49,7 @@ export default class Computed extends Effect {
|
|
|
48
49
|
}
|
|
49
50
|
else {
|
|
50
51
|
this._value = newValue;
|
|
51
|
-
if (this.dependents.size > 0 && this.isEqual(newValue, oldValue)) {
|
|
52
|
+
if (this.dependents.size > 0 && this.isEqual(newValue, this.oldValue)) {
|
|
52
53
|
this.validateDependents();
|
|
53
54
|
}
|
|
54
55
|
}
|
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,14 +39,14 @@ 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
44
|
if (newValue === InSyncSymbol) {
|
|
44
45
|
this.validateDependents();
|
|
45
46
|
} else {
|
|
46
47
|
if (newValue instanceof Promise) {
|
|
47
48
|
// @ts-expect-error
|
|
48
|
-
this._value = Promise.all([newValue, oldValue])
|
|
49
|
+
this._value = Promise.all([newValue, this.oldValue])
|
|
49
50
|
.then(([newValue, oldValue]) => {
|
|
50
51
|
if (newValue === InSyncSymbol) {
|
|
51
52
|
this.validateDependents();
|
|
@@ -58,7 +59,7 @@ export default class Computed<T> extends Effect implements Dependent, Dependency
|
|
|
58
59
|
});
|
|
59
60
|
} else {
|
|
60
61
|
this._value = newValue;
|
|
61
|
-
if (this.dependents.size > 0 && this.isEqual(newValue, oldValue)) {
|
|
62
|
+
if (this.dependents.size > 0 && this.isEqual(newValue, this.oldValue)) {
|
|
62
63
|
this.validateDependents();
|
|
63
64
|
}
|
|
64
65
|
}
|
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,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,IAAW,KAAK,IAAI,CAAC,CA8BpB;IAED,OAAO,CAAC,kBAAkB;IAMnB,YAAY,CAAC,SAAS,EAAE,SAAS;IAOjC,eAAe,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,gBAAoB,GAAG,IAAI;IAKxE,UAAU,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC;IAW3C,eAAe;IAaf,KAAK;IAKL,OAAO;CAGjB"}
|
package/lib/tracker.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export default class Tracker {
|
|
2
|
-
dependents = new Set();
|
|
3
|
-
_value;
|
|
4
|
-
addDependent(dependent) {
|
|
5
|
-
this.dependents.add(dependent);
|
|
6
|
-
}
|
|
7
|
-
removeDependent(dependent) {
|
|
8
|
-
this.dependents.delete(dependent);
|
|
9
|
-
}
|
|
10
|
-
invalidate() {
|
|
11
|
-
for (const dependent of [...this.dependents.keys()]) {
|
|
12
|
-
dependent.invalidate(this);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
get value() {
|
|
16
|
-
return this._value;
|
|
17
|
-
}
|
|
18
|
-
}
|
package/types/tracker.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import Dependent from "./dependent.js";
|
|
2
|
-
export default class Tracker<T> {
|
|
3
|
-
protected dependents: Set<Dependent>;
|
|
4
|
-
protected _value?: T;
|
|
5
|
-
addDependent(dependent: Dependent): void;
|
|
6
|
-
removeDependent(dependent: Dependent): void;
|
|
7
|
-
invalidate(): void;
|
|
8
|
-
get value(): T | undefined;
|
|
9
|
-
}
|
|
10
|
-
//# sourceMappingURL=tracker.d.ts.map
|
package/types/tracker.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tracker.d.ts","sourceRoot":"","sources":["../src/tracker.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,MAAM,CAAC,OAAO,OAAO,OAAO,CAAC,CAAC;IAC1B,SAAS,CAAC,UAAU,iBAAwB;IAC5C,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAEd,YAAY,CAAC,SAAS,EAAE,SAAS;IAIjC,eAAe,CAAC,SAAS,EAAE,SAAS;IAIpC,UAAU,IAAI,IAAI;IAMzB,IAAW,KAAK,kBAEf;CACJ"}
|