Skip to content

Commit

Permalink
When using deferred updates, a sleeping pure computed should see pend…
Browse files Browse the repository at this point in the history
…ing changes to its dependencies
  • Loading branch information
mbest committed Feb 13, 2017
1 parent 92f62a2 commit 7c1f400
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion spec/asyncBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ describe('Deferred', function() {
});

it('Should *not* delay update of dependent deferred computed observable', function () {
var data = ko.observable('A'),
var data = ko.observable('A').extend({deferred:true}),
timesEvaluated = 0,
computed1 = ko.computed(function () { return data() + 'X'; }).extend({deferred:true}),
computed2 = ko.computed(function () { timesEvaluated++; return computed1() + 'Y'; }).extend({deferred:true}),
Expand All @@ -1010,6 +1010,24 @@ describe('Deferred', function() {
expect(notifySpy.argsForCall).toEqual([ ['BXY'] ]);
});

it('Should *not* delay update of dependent deferred pure computed observable', function () {
var data = ko.observable('A').extend({deferred:true}),
timesEvaluated = 0,
computed1 = ko.pureComputed(function () { return data() + 'X'; }).extend({deferred:true}),
computed2 = ko.pureComputed(function () { timesEvaluated++; return computed1() + 'Y'; }).extend({deferred:true});

expect(computed2()).toEqual('AXY');
expect(timesEvaluated).toEqual(1);

data('B');
expect(computed2()).toEqual('BXY');
expect(timesEvaluated).toEqual(2);

jasmine.Clock.tick(1);
expect(computed2()).toEqual('BXY');
expect(timesEvaluated).toEqual(2); // Verify that the computed wasn't evaluated again unnecessarily
});

it('Should *not* delay update of dependent rate-limited computed observable', function() {
var data = ko.observable('A'),
deferredComputed = ko.computed(data).extend({deferred:true}),
Expand Down
2 changes: 1 addition & 1 deletion src/subscribables/dependentObservable.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ var computedFn = {
for (id in dependencyTracking) {
if (Object.prototype.hasOwnProperty.call(dependencyTracking, id)) {
dependency = dependencyTracking[id];
if (dependency._target.hasChanged(dependency._version)) {
if ((this._evalDelayed && dependency._target._notificationIsPending) || dependency._target.hasChanged(dependency._version)) {
return true;
}
}
Expand Down

0 comments on commit 7c1f400

Please sign in to comment.