Skip to content

Commit

Permalink
Merge branch 'fix-side-effects-on-awake' of https://github.com/dancra…
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed Oct 1, 2017
2 parents 2606678 + 822f147 commit 195cdea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions spec/pureComputedBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,27 @@ describe('Pure Computed', function() {
expect(timesEvaluated).toEqual(2);
});

it('Should wake with the correct value when a chained pure computed has side effects for its awake event', function () {

var observableToUpdateOnAwake = ko.observable(null),
computed1 = ko.pureComputed(observableToUpdateOnAwake),
computed2 = ko.pureComputed(computed1);

computed1.subscribe(function () {
observableToUpdateOnAwake('foo');
}, null, 'awake');

// Reading from the computed before subscribing caused the subscription to
// ignore side-effects from the awake callback of chained pure computeds
computed2();

computed2.subscribe(function () {
});

expect(computed2()).toEqual('foo');

});

describe('Should maintain order of subscriptions', function () {
var data, dataPureComputed;

Expand Down
10 changes: 10 additions & 0 deletions src/subscribables/dependentObservable.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,16 @@ var pureComputedOverrides = {
state.dependencyTracking[id] = subscription;
});
}

// Repeat check since waking dependencies may have triggered effects
if (computedObservable.haveDependenciesChanged()) {
state.dependencyTracking = null;
state.dependenciesCount = 0;
if (computedObservable.evaluateImmediate()) {
computedObservable.updateVersion();
}
}

if (!state.isDisposed) { // test since evaluating could trigger disposal
computedObservable["notifySubscribers"](state.latestValue, "awake");
}
Expand Down

0 comments on commit 195cdea

Please sign in to comment.