Skip to content

Commit

Permalink
Merge branch 'andykog-fix-916', fixes #916
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Apr 3, 2017
2 parents 2c3921c + 1c79a51 commit 14c14f4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 3.1.8

* Fixed edge case where `autorun` was not triggered again if a computed value was invalidated by the reaction itself, see [#916](https://github.com/mobxjs/mobx/issues/916)

# 3.1.7

* Reverted ES2015 module changes, as they broke with webpack 2 (will be re-released later)
Expand Down
14 changes: 14 additions & 0 deletions src/core/derivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ function bindDependencies(derivation: IDerivation) {

const prevObserving = derivation.observing;
const observing = derivation.observing = derivation.newObserving!;
let lowestNewObservingDerivationState = IDerivationState.UP_TO_DATE;

derivation.newObserving = null; // newObserving shouldn't be needed outside tracking

Expand All @@ -162,6 +163,12 @@ function bindDependencies(derivation: IDerivation) {
if (i0 !== i) observing[i0] = dep;
i0++;
}

// Upcast is 'safe' here, because if dep is IObservable, `dependenciesState` will be undefined,
// not hitting the condition
if ((dep as any as IDerivation).dependenciesState > lowestNewObservingDerivationState) {
lowestNewObservingDerivationState = (dep as any as IDerivation).dependenciesState;
}
}
observing.length = i0;

Expand All @@ -187,6 +194,13 @@ function bindDependencies(derivation: IDerivation) {
addObserver(dep, derivation);
}
}

// Some new observed derivations might become stale during this derivation computation
// so say had no chance to propagate staleness (#916)
if (lowestNewObservingDerivationState !== IDerivationState.UP_TO_DATE) {
derivation.dependenciesState = lowestNewObservingDerivationState;
derivation.onBecomeStale();
}
}

export function clearObserving(derivation: IDerivation) {
Expand Down
19 changes: 18 additions & 1 deletion test/autorun.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,21 @@ test('autorun batches automatically', function(t) {
d1()
d2()
t.end();
})
})


test('autorun tracks invalidation of unbound dependencies', function(t) {
var a = m.observable(0);
var b = m.observable(0);
var c = m.computed(() => a.get() + b.get());
var values = [];

m.autorun(() => {
values.push(c.get());
b.set(100);
});

a.set(1);
t.deepEqual(values, [0, 100, 101]);
t.end();
})

0 comments on commit 14c14f4

Please sign in to comment.