Skip to content

Commit

Permalink
Merge pull request #1499 from kuitos/fork-master
Browse files Browse the repository at this point in the history
avoid the unnecessary reassign with the equivalent comparison result
  • Loading branch information
mweststrate authored Apr 16, 2018
2 parents 09493ee + cff477c commit f6df576
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/core/computedvalue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,19 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, 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) {
Expand Down
23 changes: 23 additions & 0 deletions test/base/babel-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 changing which was considered equivalent", () => {
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
Expand Down

0 comments on commit f6df576

Please sign in to comment.