async-reactivity 2.0.28 → 2.0.30
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 +2 -2
- package/lib/computed.test.js +7 -3
- package/lib/ref.js +5 -5
- package/lib/ref.test.js +5 -1
- package/package.json +1 -1
- package/src/computed.test.ts +10 -4
- package/src/computed.ts +2 -2
- package/src/ref.test.ts +6 -1
- package/src/ref.ts +5 -5
- package/types/ref.d.ts +2 -2
- package/types/ref.d.ts.map +1 -1
package/lib/computed.js
CHANGED
|
@@ -30,7 +30,7 @@ export default class Computed extends Effect {
|
|
|
30
30
|
const newValue = this.run();
|
|
31
31
|
if (newValue !== InSyncSymbol) {
|
|
32
32
|
this._value = newValue;
|
|
33
|
-
if (this.isEqual(this._value, oldValue)) {
|
|
33
|
+
if (this.dependents.size > 0 && this.isEqual(this._value, oldValue)) {
|
|
34
34
|
this.validateDependents();
|
|
35
35
|
}
|
|
36
36
|
}
|
|
@@ -38,7 +38,7 @@ export default class Computed extends Effect {
|
|
|
38
38
|
return this._value;
|
|
39
39
|
}
|
|
40
40
|
validateDependents() {
|
|
41
|
-
for (const dependent of this.dependents
|
|
41
|
+
for (const dependent of this.dependents) {
|
|
42
42
|
dependent.validate(this);
|
|
43
43
|
}
|
|
44
44
|
}
|
package/lib/computed.test.js
CHANGED
|
@@ -152,14 +152,18 @@ describe('computed', function () {
|
|
|
152
152
|
});
|
|
153
153
|
it('isEqual', function () {
|
|
154
154
|
const a = new Ref(1);
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}, (_newValue, oldValue) => oldValue !== undefined);
|
|
155
|
+
let isEqual = false;
|
|
156
|
+
const b = new Computed((value) => value(a), () => isEqual);
|
|
158
157
|
const c = new Computed(value => value(b));
|
|
159
158
|
assert.strictEqual(c.value, 1);
|
|
159
|
+
isEqual = true;
|
|
160
160
|
a.value = 2;
|
|
161
161
|
assert.strictEqual(c.value, 1);
|
|
162
162
|
});
|
|
163
|
+
it('should not call isEqual when there are no dependents (optimization)', function () {
|
|
164
|
+
const a = new Computed(() => 5, () => assert.fail());
|
|
165
|
+
assert.strictEqual(a.value, 5);
|
|
166
|
+
});
|
|
163
167
|
});
|
|
164
168
|
describe('async', function () {
|
|
165
169
|
it('getter', async function () {
|
package/lib/ref.js
CHANGED
|
@@ -3,14 +3,14 @@ export default class Ref {
|
|
|
3
3
|
isEqual;
|
|
4
4
|
dependents = new Set();
|
|
5
5
|
_value;
|
|
6
|
-
constructor(
|
|
7
|
-
this._value =
|
|
6
|
+
constructor(value, isEqual = (defaultIsEqual)) {
|
|
7
|
+
this._value = value;
|
|
8
8
|
this.isEqual = isEqual;
|
|
9
9
|
}
|
|
10
|
-
set value(
|
|
10
|
+
set value(value) {
|
|
11
11
|
const oldValue = this._value;
|
|
12
|
-
this._value =
|
|
13
|
-
if (!this.isEqual(
|
|
12
|
+
this._value = value;
|
|
13
|
+
if (this.dependents.size > 0 && !this.isEqual(value, oldValue)) {
|
|
14
14
|
this.invalidate();
|
|
15
15
|
}
|
|
16
16
|
}
|
package/lib/ref.test.js
CHANGED
|
@@ -11,11 +11,15 @@ describe('ref', function () {
|
|
|
11
11
|
a.value = 4;
|
|
12
12
|
assert.strictEqual(a.value, 4);
|
|
13
13
|
});
|
|
14
|
-
it('
|
|
14
|
+
it('should not invalidate when isEqual returns true', function () {
|
|
15
15
|
const a = new Ref(5, () => true);
|
|
16
16
|
new Watcher(a, () => {
|
|
17
17
|
assert.fail();
|
|
18
18
|
}, false);
|
|
19
19
|
a.value = 4;
|
|
20
20
|
});
|
|
21
|
+
it('should not call isEqual when there are no dependents (optimization)', function () {
|
|
22
|
+
const a = new Ref(5, () => assert.fail());
|
|
23
|
+
a.value = 4;
|
|
24
|
+
});
|
|
21
25
|
});
|
package/package.json
CHANGED
package/src/computed.test.ts
CHANGED
|
@@ -179,16 +179,22 @@ describe('computed', function () {
|
|
|
179
179
|
|
|
180
180
|
it('isEqual', function () {
|
|
181
181
|
const a = new Ref(1);
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}, (_newValue, oldValue) => oldValue !== undefined);
|
|
182
|
+
let isEqual = false;
|
|
183
|
+
const b = new Computed((value) => value(a), () => isEqual);
|
|
185
184
|
const c = new Computed(value => value(b));
|
|
186
185
|
|
|
187
186
|
assert.strictEqual(c.value, 1);
|
|
188
|
-
|
|
187
|
+
isEqual = true;
|
|
189
188
|
a.value = 2;
|
|
189
|
+
|
|
190
190
|
assert.strictEqual(c.value, 1);
|
|
191
191
|
});
|
|
192
|
+
|
|
193
|
+
it('should not call isEqual when there are no dependents (optimization)', function () {
|
|
194
|
+
const a = new Computed(() => 5, () => assert.fail());
|
|
195
|
+
|
|
196
|
+
assert.strictEqual(a.value, 5);
|
|
197
|
+
});
|
|
192
198
|
});
|
|
193
199
|
|
|
194
200
|
describe('async', function () {
|
package/src/computed.ts
CHANGED
|
@@ -42,7 +42,7 @@ export default class Computed<T> extends Effect implements Dependent, Dependency
|
|
|
42
42
|
const newValue = this.run() as T;
|
|
43
43
|
if (newValue !== InSyncSymbol) {
|
|
44
44
|
this._value = newValue;
|
|
45
|
-
if (this.isEqual(this._value!, oldValue)) {
|
|
45
|
+
if (this.dependents.size > 0 && this.isEqual(this._value!, oldValue)) {
|
|
46
46
|
this.validateDependents();
|
|
47
47
|
}
|
|
48
48
|
}
|
|
@@ -52,7 +52,7 @@ export default class Computed<T> extends Effect implements Dependent, Dependency
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
private validateDependents() {
|
|
55
|
-
for (const dependent of this.dependents
|
|
55
|
+
for (const dependent of this.dependents) {
|
|
56
56
|
dependent.validate(this);
|
|
57
57
|
}
|
|
58
58
|
}
|
package/src/ref.test.ts
CHANGED
|
@@ -14,11 +14,16 @@ describe('ref', function () {
|
|
|
14
14
|
assert.strictEqual(a.value, 4);
|
|
15
15
|
});
|
|
16
16
|
|
|
17
|
-
it('
|
|
17
|
+
it('should not invalidate when isEqual returns true', function () {
|
|
18
18
|
const a = new Ref(5, () => true);
|
|
19
19
|
new Watcher(a, () => {
|
|
20
20
|
assert.fail();
|
|
21
21
|
}, false);
|
|
22
22
|
a.value = 4;
|
|
23
23
|
});
|
|
24
|
+
|
|
25
|
+
it('should not call isEqual when there are no dependents (optimization)', function () {
|
|
26
|
+
const a = new Ref(5, () => assert.fail());
|
|
27
|
+
a.value = 4;
|
|
28
|
+
});
|
|
24
29
|
});
|
package/src/ref.ts
CHANGED
|
@@ -7,15 +7,15 @@ export default class Ref<T> implements Dependency<T> {
|
|
|
7
7
|
protected dependents = new Set<Dependent>();
|
|
8
8
|
protected _value?: T;
|
|
9
9
|
|
|
10
|
-
constructor(
|
|
11
|
-
this._value =
|
|
10
|
+
constructor(value: T, isEqual = defaultIsEqual<T>) {
|
|
11
|
+
this._value = value;
|
|
12
12
|
this.isEqual = isEqual;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
public set value(
|
|
15
|
+
public set value(value: T) {
|
|
16
16
|
const oldValue = this._value!;
|
|
17
|
-
this._value =
|
|
18
|
-
if (!this.isEqual(
|
|
17
|
+
this._value = value;
|
|
18
|
+
if (this.dependents.size > 0 && !this.isEqual(value, oldValue)) {
|
|
19
19
|
this.invalidate();
|
|
20
20
|
}
|
|
21
21
|
}
|
package/types/ref.d.ts
CHANGED
|
@@ -4,8 +4,8 @@ export default class Ref<T> implements Dependency<T> {
|
|
|
4
4
|
private isEqual;
|
|
5
5
|
protected dependents: Set<Dependent>;
|
|
6
6
|
protected _value?: T;
|
|
7
|
-
constructor(
|
|
8
|
-
set value(
|
|
7
|
+
constructor(value: T, isEqual?: (v1: T, v2: T) => boolean);
|
|
8
|
+
set value(value: T);
|
|
9
9
|
get value(): T;
|
|
10
10
|
addDependent(dependent: Dependent): void;
|
|
11
11
|
removeDependent(dependent: Dependent): void;
|
package/types/ref.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ref.d.ts","sourceRoot":"","sources":["../src/ref.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,MAAM,CAAC,OAAO,OAAO,GAAG,CAAC,CAAC,CAAE,YAAW,UAAU,CAAC,CAAC,CAAC;IAChD,OAAO,CAAC,OAAO,CAA2B;IAC1C,SAAS,CAAC,UAAU,iBAAwB;IAC5C,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;gBAET,
|
|
1
|
+
{"version":3,"file":"ref.d.ts","sourceRoot":"","sources":["../src/ref.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,MAAM,CAAC,OAAO,OAAO,GAAG,CAAC,CAAC,CAAE,YAAW,UAAU,CAAC,CAAC,CAAC;IAChD,OAAO,CAAC,OAAO,CAA2B;IAC1C,SAAS,CAAC,UAAU,iBAAwB;IAC5C,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;gBAET,KAAK,EAAE,CAAC,EAAE,OAAO,4BAAoB;IAKjD,IAAW,KAAK,CAAC,KAAK,EAAE,CAAC,EAMxB;IAED,IAAW,KAAK,IAAI,CAAC,CAEpB;IAEM,YAAY,CAAC,SAAS,EAAE,SAAS;IAIjC,eAAe,CAAC,SAAS,EAAE,SAAS;IAIpC,UAAU,IAAI,IAAI;CAK5B"}
|