From a6221cc29a229990b32ad862b502b273375b10a4 Mon Sep 17 00:00:00 2001 From: Andrey Kogut Date: Fri, 31 Mar 2017 03:24:53 +0300 Subject: [PATCH] Fix #916: new observed derivations bocomes stale --- src/core/derivation.ts | 11 +++++++++++ test/autorun.js | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/core/derivation.ts b/src/core/derivation.ts index 8ea56c692..b0830e9b3 100644 --- a/src/core/derivation.ts +++ b/src/core/derivation.ts @@ -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 @@ -162,6 +163,9 @@ function bindDependencies(derivation: IDerivation) { if (i0 !== i) observing[i0] = dep; i0++; } + if (dep['dependenciesState'] > lowestNewObservingDerivationState) { + lowestNewObservingDerivationState = dep['dependenciesState']; + } } observing.length = i0; @@ -187,6 +191,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) { diff --git a/test/autorun.js b/test/autorun.js index cd137d9da..bd4636698 100644 --- a/test/autorun.js +++ b/test/autorun.js @@ -84,4 +84,21 @@ test('autorun batches automatically', function(t) { d1() d2() t.end(); -}) \ No newline at end of file +}) + + +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(); +})