Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't pass undefined to custom equality checks on becoming observed a… #1213

Merged
merged 2 commits into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/computedvalue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva

onBecomeUnobserved() {
clearObserving(this)
this.value = undefined
this.value = new CaughtException(null)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/errorhandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ test("peeking inside autorun doesn't bork (global) state", t => {
t.equal(b.diffValue, 0)
t.equal(b.lowestObserverState, 0)
t.equal(b.unboundDepsCount, 1)
t.equal(b.value, undefined)
t.notEqual(b.value, 3)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not particularly happy with this assertion. I wanted to write something like t.ok(isCaughtException(b.value)) but as far as I can tell neither CaughtException nor isCaughtException are exported from mobx.ts. I considered exporting them from mobx.ts, but that's changing public API. Also considered importing them from core/derivation.ts directly, but that's inconsistent with all other tests.

This feels like it's still achieving the goal of the assertion (ensuring that the cached value is cleared) while avoiding those problems.

I'm open to suggestions of other approaches.

t.equal(b.isComputing, false)

t.equal(c.dependenciesState, -1)
Expand Down
9 changes: 7 additions & 2 deletions test/observables.js
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,7 @@ test("computed equals function only invoked when necessary", t => {
)

const values = []
const disposeAutorun = mobx.autorun(() => values.push(combinedToLowerCase.get()))
let disposeAutorun = mobx.autorun(() => values.push(combinedToLowerCase.get()))

// No comparison should be made on the first value
t.deepEqual(comparisons, [])
Expand All @@ -1879,7 +1879,12 @@ test("computed equals function only invoked when necessary", t => {
right.set("F")
t.deepEqual(comparisons, [{ from: "ab", to: "cb" }, { from: "de", to: "df" }])

t.deepEqual(values, ["ab", "cb", "de", "df"])
// Becoming unobserved, then observed won't cause a comparison
disposeAutorun()
disposeAutorun = mobx.autorun(() => values.push(combinedToLowerCase.get()));
t.deepEqual(comparisons, [{ from: "ab", to: "cb" }, { from: "de", to: "df" }])

t.deepEqual(values, ["ab", "cb", "de", "df", "df"])

disposeAutorun()

Expand Down