Skip to content

Commit

Permalink
Fixes mbest#8 - computed doesn't update if dependent on throttled com…
Browse files Browse the repository at this point in the history
…puted.
  • Loading branch information
mbest committed Mar 8, 2013
1 parent b2dc914 commit 7c3fccf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
8 changes: 6 additions & 2 deletions knockout-deferred-updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,12 @@ var newComputed = function (evaluatorFunctionOrOptions, evaluatorFunctionTarget,
}
}

function markAsChanged() {
_needsEvaluation = true;
function markAsChanged(value) {
if (!_possiblyNeedsEvaluation && !_needsEvaluation) {
evaluatePossiblyAsync(value, 'change');
} else {
_needsEvaluation = true;
}
}

function addDependency(subscribable) {
Expand Down
13 changes: 12 additions & 1 deletion spec/asyncBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ asyncTest("Should notify subscribers asynchronously after dependencies stop upda
var asyncDepObs = ko.dependentObservable(function() {
return lastUpdateValue = underlying();
}).extend({ throttle: 100 });
var notifiedValues = []
var notifiedValues = [];
asyncDepObs.subscribe(function(value) {
notifiedValues.push(value);
});
var computedNotifiedValues = [];
var secondComputed = ko.computed(function() {
var value = asyncDepObs();
if (value)
computedNotifiedValues.push(value);
return value;
});

// Check initial state
start();
Expand All @@ -55,6 +62,7 @@ asyncTest("Should notify subscribers asynchronously after dependencies stop upda
underlying('New value');
equal(lastUpdateValue, undefined, 'Should not update synchronously');
equal(notifiedValues.length, 0);
equal(computedNotifiedValues.length, 0);
stop();

// Wait
Expand All @@ -63,6 +71,7 @@ asyncTest("Should notify subscribers asynchronously after dependencies stop upda
start();
equal(lastUpdateValue, undefined, 'Should not update until throttle timeout');
equal(notifiedValues.length, 0);
equal(computedNotifiedValues.length, 0);
stop();

// Wait again
Expand All @@ -71,6 +80,8 @@ asyncTest("Should notify subscribers asynchronously after dependencies stop upda
equal(lastUpdateValue, 'New value');
equal(notifiedValues.length, 1);
equal(notifiedValues[0], 'New value');
equal(computedNotifiedValues.length, 1);
equal(computedNotifiedValues[0], 'New value');
}, 60);
}, 50);
});
Expand Down

0 comments on commit 7c3fccf

Please sign in to comment.