async-reactivity 2.0.7 → 2.0.8
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 +8 -7
- package/lib/computed.test.js +9 -0
- package/package.json +1 -1
- package/src/computed.test.ts +12 -0
- package/src/computed.ts +7 -7
package/lib/computed.js
CHANGED
|
@@ -62,20 +62,21 @@ export default class Computed extends Tracker {
|
|
|
62
62
|
}
|
|
63
63
|
this.state = ComputedState.Computing;
|
|
64
64
|
this.clearDependencies();
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
const newValue = this.getter(this.trackDependency);
|
|
66
|
+
if (this.isEqual(newValue, this._value)) {
|
|
67
|
+
this.handlePromiseThen(this.lastComputeAttemptPromise, this._value);
|
|
68
|
+
this.validateDependents();
|
|
69
|
+
}
|
|
70
|
+
else if (newValue instanceof Promise) {
|
|
71
|
+
const computeAttemptPromise = newValue
|
|
69
72
|
.then(result => this.handlePromiseThen(computeAttemptPromise, result))
|
|
70
73
|
.catch(error => this.handlePromiseCatch(computeAttemptPromise, error));
|
|
71
74
|
this.lastComputeAttemptPromise = computeAttemptPromise;
|
|
72
75
|
this._value = this.computePromise;
|
|
73
76
|
}
|
|
74
77
|
else {
|
|
78
|
+
this._value = newValue;
|
|
75
79
|
this.handlePromiseThen(this.lastComputeAttemptPromise, this._value);
|
|
76
|
-
if (this.isEqual(lastValue, this._value)) {
|
|
77
|
-
this.validateDependents();
|
|
78
|
-
}
|
|
79
80
|
}
|
|
80
81
|
return this._value;
|
|
81
82
|
}
|
package/lib/computed.test.js
CHANGED
|
@@ -130,6 +130,15 @@ describe('computed', function () {
|
|
|
130
130
|
assert.strictEqual(c.value, 6);
|
|
131
131
|
assert.strictEqual(gate, 2);
|
|
132
132
|
});
|
|
133
|
+
it('isEqual', function () {
|
|
134
|
+
const a = new Ref(1);
|
|
135
|
+
const b = new Computed((value) => {
|
|
136
|
+
return value(a);
|
|
137
|
+
}, (_newValue, oldValue) => oldValue !== undefined);
|
|
138
|
+
assert.strictEqual(b.value, 1);
|
|
139
|
+
a.value = 2;
|
|
140
|
+
assert.strictEqual(b.value, 1);
|
|
141
|
+
});
|
|
133
142
|
});
|
|
134
143
|
describe('async computed', function () {
|
|
135
144
|
it('getter', async function () {
|
package/package.json
CHANGED
package/src/computed.test.ts
CHANGED
|
@@ -152,6 +152,18 @@ describe('computed', function () {
|
|
|
152
152
|
assert.strictEqual(c.value, 6);
|
|
153
153
|
assert.strictEqual(gate, 2);
|
|
154
154
|
});
|
|
155
|
+
|
|
156
|
+
it('isEqual', function() {
|
|
157
|
+
const a = new Ref(1);
|
|
158
|
+
const b = new Computed((value) => {
|
|
159
|
+
return value(a);
|
|
160
|
+
}, (_newValue, oldValue) => oldValue !== undefined);
|
|
161
|
+
|
|
162
|
+
assert.strictEqual(b.value, 1);
|
|
163
|
+
|
|
164
|
+
a.value = 2;
|
|
165
|
+
assert.strictEqual(b.value, 1);
|
|
166
|
+
});
|
|
155
167
|
});
|
|
156
168
|
|
|
157
169
|
describe('async computed', function () {
|
package/src/computed.ts
CHANGED
|
@@ -74,19 +74,19 @@ export default class Computed<T> extends Tracker<T> implements Dependent, Depend
|
|
|
74
74
|
this.state = ComputedState.Computing;
|
|
75
75
|
this.clearDependencies();
|
|
76
76
|
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
const newValue: T = this.getter(this.trackDependency);
|
|
78
|
+
if (this.isEqual(newValue, this._value)) {
|
|
79
|
+
this.handlePromiseThen(this.lastComputeAttemptPromise!, this._value);
|
|
80
|
+
this.validateDependents();
|
|
81
|
+
} else if (newValue instanceof Promise) {
|
|
82
|
+
const computeAttemptPromise = newValue
|
|
81
83
|
.then(result => this.handlePromiseThen(computeAttemptPromise, result))
|
|
82
84
|
.catch(error => this.handlePromiseCatch(computeAttemptPromise, error)) as Promise<void>;
|
|
83
85
|
this.lastComputeAttemptPromise = computeAttemptPromise;
|
|
84
86
|
this._value = this.computePromise!;
|
|
85
87
|
} else {
|
|
88
|
+
this._value = newValue;
|
|
86
89
|
this.handlePromiseThen(this.lastComputeAttemptPromise!, this._value);
|
|
87
|
-
if (this.isEqual(lastValue, this._value)) {
|
|
88
|
-
this.validateDependents();
|
|
89
|
-
}
|
|
90
90
|
}
|
|
91
91
|
return this._value!;
|
|
92
92
|
}
|