From e1de21036af7b422fe117eb0df17f540fc6ebb26 Mon Sep 17 00:00:00 2001 From: Kuitos Date: Sat, 14 Apr 2018 00:25:58 +0800 Subject: [PATCH 1/2] avoid the unnecessary reassign with the equivalent comparison result --- src/core/computedvalue.ts | 12 +++++++++--- test/base/babel-tests.js | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/core/computedvalue.ts b/src/core/computedvalue.ts index 8e9826d7a..7da1fafa7 100644 --- a/src/core/computedvalue.ts +++ b/src/core/computedvalue.ts @@ -197,13 +197,19 @@ export class ComputedValue implements IObservable, IComputedValue, IDeriva const oldValue = this.value const wasSuspended = /* see #1208 */ this.dependenciesState === IDerivationState.NOT_TRACKING - const newValue = (this.value = this.computeValue(true)) - return ( + const newValue = this.computeValue(true) + + const changed = wasSuspended || isCaughtException(oldValue) || isCaughtException(newValue) || !this.equals(oldValue, newValue) - ) + + if (changed) { + this.value = newValue + } + + return changed } computeValue(track: boolean) { diff --git a/test/base/babel-tests.js b/test/base/babel-tests.js index 4d78789b4..8d066a117 100644 --- a/test/base/babel-tests.js +++ b/test/base/babel-tests.js @@ -102,6 +102,29 @@ test("babel: parameterized computed decorator", () => { expect(changes).toEqual([{ sum: 6 }, { sum: 7 }, { sum: 9 }]) }) +test("computed value should be the same around comparer judged equivalent reassign", () => { + class TestClass { + @observable c = null + defaultCollection = [] + @computed.struct + get collection() { + return this.c || this.defaultCollection + } + } + + const t1 = new TestClass() + + const d = autorun(() => t1.collection) + + const oldCollection = t1.collection + t1.c = [] + const newCollection = t1.collection + + expect(oldCollection).toBe(newCollection) + + d() +}) + class Order { @observable price = 3 @observable amount = 2 From cff477c8bbc2757e5e53d77aa621f5cccf09b193 Mon Sep 17 00:00:00 2001 From: Kuitos Date: Sat, 14 Apr 2018 01:17:07 +0800 Subject: [PATCH 2/2] reword the test case description --- test/base/babel-tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/base/babel-tests.js b/test/base/babel-tests.js index 8d066a117..d5f20c4f6 100644 --- a/test/base/babel-tests.js +++ b/test/base/babel-tests.js @@ -102,7 +102,7 @@ test("babel: parameterized computed decorator", () => { expect(changes).toEqual([{ sum: 6 }, { sum: 7 }, { sum: 9 }]) }) -test("computed value should be the same around comparer judged equivalent reassign", () => { +test("computed value should be the same around changing which was considered equivalent", () => { class TestClass { @observable c = null defaultCollection = []