async-reactivity 2.0.4 → 2.0.6
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 +14 -2
- package/lib/computed.test.js +34 -0
- package/package.json +1 -1
- package/src/computed.test.ts +40 -0
- package/src/computed.ts +14 -2
- package/types/computed.d.ts +1 -0
- package/types/computed.d.ts.map +1 -1
package/lib/computed.js
CHANGED
|
@@ -41,8 +41,16 @@ export default class Computed extends Tracker {
|
|
|
41
41
|
this.dependencies.set(dependency, true);
|
|
42
42
|
}
|
|
43
43
|
if ([...this.dependencies.keys()].every(d => {
|
|
44
|
-
d
|
|
45
|
-
|
|
44
|
+
if (d instanceof Computed) {
|
|
45
|
+
if (d.state === ComputedState.Valid) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
else if (d.state === ComputedState.Uncertain) {
|
|
49
|
+
d.value;
|
|
50
|
+
return !this.dependencies.get(d);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
46
54
|
})) {
|
|
47
55
|
this.finalizeComputing();
|
|
48
56
|
this.validateDependents();
|
|
@@ -110,6 +118,10 @@ export default class Computed extends Tracker {
|
|
|
110
118
|
super.invalidate();
|
|
111
119
|
}
|
|
112
120
|
}
|
|
121
|
+
forceInvalidate() {
|
|
122
|
+
this.invalidate();
|
|
123
|
+
this.state = ComputedState.Invalid;
|
|
124
|
+
}
|
|
113
125
|
validate(dependency) {
|
|
114
126
|
this.dependencies.set(dependency, false);
|
|
115
127
|
}
|
package/lib/computed.test.js
CHANGED
|
@@ -92,10 +92,44 @@ describe('computed', function () {
|
|
|
92
92
|
return value(b) + 5;
|
|
93
93
|
});
|
|
94
94
|
assert.strictEqual(c.value, 6);
|
|
95
|
+
assert.strictEqual(gate, 1);
|
|
96
|
+
a.value = 7;
|
|
97
|
+
assert.strictEqual(c.value, 6);
|
|
98
|
+
assert.strictEqual(gate, 1);
|
|
99
|
+
});
|
|
100
|
+
it('ignore same computed values', function () {
|
|
101
|
+
let gate = 0;
|
|
102
|
+
const a = new Ref(5);
|
|
103
|
+
const b1 = new Computed((value) => {
|
|
104
|
+
return value(a) % 2;
|
|
105
|
+
});
|
|
106
|
+
const b2 = new Computed(() => 5);
|
|
107
|
+
const c = new Computed((value) => {
|
|
108
|
+
gate++;
|
|
109
|
+
return value(b1) + value(b2);
|
|
110
|
+
});
|
|
111
|
+
assert.strictEqual(c.value, 6);
|
|
112
|
+
assert.strictEqual(gate, 1);
|
|
95
113
|
a.value = 7;
|
|
96
114
|
assert.strictEqual(c.value, 6);
|
|
97
115
|
assert.strictEqual(gate, 1);
|
|
98
116
|
});
|
|
117
|
+
it('compute when forced', function () {
|
|
118
|
+
let gate = 0;
|
|
119
|
+
const a = new Ref(5);
|
|
120
|
+
const b = new Computed((value) => {
|
|
121
|
+
return value(a) % 2;
|
|
122
|
+
});
|
|
123
|
+
const c = new Computed((value) => {
|
|
124
|
+
gate++;
|
|
125
|
+
return value(b) + 5;
|
|
126
|
+
});
|
|
127
|
+
assert.strictEqual(c.value, 6);
|
|
128
|
+
assert.strictEqual(gate, 1);
|
|
129
|
+
c.forceInvalidate();
|
|
130
|
+
assert.strictEqual(c.value, 6);
|
|
131
|
+
assert.strictEqual(gate, 2);
|
|
132
|
+
});
|
|
99
133
|
});
|
|
100
134
|
describe('async computed', function () {
|
|
101
135
|
it('getter', async function () {
|
package/package.json
CHANGED
package/src/computed.test.ts
CHANGED
|
@@ -107,11 +107,51 @@ describe('computed', function () {
|
|
|
107
107
|
});
|
|
108
108
|
|
|
109
109
|
assert.strictEqual(c.value, 6);
|
|
110
|
+
assert.strictEqual(gate, 1);
|
|
111
|
+
|
|
112
|
+
a.value = 7;
|
|
113
|
+
assert.strictEqual(c.value, 6);
|
|
114
|
+
assert.strictEqual(gate, 1);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('ignore same computed values', function () {
|
|
118
|
+
let gate = 0;
|
|
119
|
+
const a = new Ref(5);
|
|
120
|
+
const b1 = new Computed((value) => {
|
|
121
|
+
return value(a) % 2;
|
|
122
|
+
});
|
|
123
|
+
const b2 = new Computed(() => 5);
|
|
124
|
+
const c = new Computed((value) => {
|
|
125
|
+
gate++;
|
|
126
|
+
return value(b1) + value(b2);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
assert.strictEqual(c.value, 6);
|
|
130
|
+
assert.strictEqual(gate, 1);
|
|
110
131
|
|
|
111
132
|
a.value = 7;
|
|
112
133
|
assert.strictEqual(c.value, 6);
|
|
113
134
|
assert.strictEqual(gate, 1);
|
|
114
135
|
});
|
|
136
|
+
|
|
137
|
+
it('compute when forced', function () {
|
|
138
|
+
let gate = 0;
|
|
139
|
+
const a = new Ref(5);
|
|
140
|
+
const b = new Computed((value) => {
|
|
141
|
+
return value(a) % 2;
|
|
142
|
+
});
|
|
143
|
+
const c = new Computed((value) => {
|
|
144
|
+
gate++;
|
|
145
|
+
return value(b) + 5;
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
assert.strictEqual(c.value, 6);
|
|
149
|
+
assert.strictEqual(gate, 1);
|
|
150
|
+
|
|
151
|
+
c.forceInvalidate();
|
|
152
|
+
assert.strictEqual(c.value, 6);
|
|
153
|
+
assert.strictEqual(gate, 2);
|
|
154
|
+
});
|
|
115
155
|
});
|
|
116
156
|
|
|
117
157
|
describe('async computed', function () {
|
package/src/computed.ts
CHANGED
|
@@ -52,8 +52,15 @@ export default class Computed<T> extends Tracker<T> implements Dependent, Depend
|
|
|
52
52
|
this.dependencies.set(dependency, true);
|
|
53
53
|
}
|
|
54
54
|
if ([...this.dependencies.keys()].every(d => {
|
|
55
|
-
d
|
|
56
|
-
|
|
55
|
+
if (d instanceof Computed) {
|
|
56
|
+
if (d.state === ComputedState.Valid) {
|
|
57
|
+
return true;
|
|
58
|
+
} else if (d.state === ComputedState.Uncertain) {
|
|
59
|
+
d.value;
|
|
60
|
+
return !this.dependencies.get(d);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
57
64
|
})) {
|
|
58
65
|
this.finalizeComputing();
|
|
59
66
|
this.validateDependents();
|
|
@@ -129,6 +136,11 @@ export default class Computed<T> extends Tracker<T> implements Dependent, Depend
|
|
|
129
136
|
}
|
|
130
137
|
}
|
|
131
138
|
|
|
139
|
+
public forceInvalidate() {
|
|
140
|
+
this.invalidate();
|
|
141
|
+
this.state = ComputedState.Invalid;
|
|
142
|
+
}
|
|
143
|
+
|
|
132
144
|
public validate(dependency: Dependency<any>) {
|
|
133
145
|
this.dependencies.set(dependency, false);
|
|
134
146
|
}
|
package/types/computed.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export default class Computed<T> extends Tracker<T> implements Dependent, Depend
|
|
|
22
22
|
private trackDependency;
|
|
23
23
|
private finalizeComputing;
|
|
24
24
|
invalidate(): void;
|
|
25
|
+
forceInvalidate(): void;
|
|
25
26
|
validate(dependency: Dependency<any>): void;
|
|
26
27
|
private validateDependents;
|
|
27
28
|
dispose(): void;
|
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;AAEnC,OAAO,MAAM,UAAU,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AAC9D,MAAM,CAAC,OAAO,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,UAAU,KAAK,CAAC,CAAC;AAC9D,MAAM,CAAC,OAAO,MAAM,iBAAiB,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,CAAC;AAWrF,MAAM,CAAC,OAAO,OA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