Skip to content

Commit

Permalink
Fix #916: new observed derivations bocomes stale
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Kogut committed Apr 1, 2017
1 parent 1b229f5 commit a6221cc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 11 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,9 @@ function bindDependencies(derivation: IDerivation) {
if (i0 !== i) observing[i0] = dep;
i0++;
}
if (dep['dependenciesState'] > lowestNewObservingDerivationState) {
lowestNewObservingDerivationState = dep['dependenciesState'];
}
}
observing.length = i0;

Expand All @@ -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) {
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 a6221cc

Please sign in to comment.