Skip to content

Commit

Permalink
When checking if an observable has a pending notification, also compa…
Browse files Browse the repository at this point in the history
…re the value. If it hasn't actually changed, don't force the notification.
  • Loading branch information
mbest committed Feb 10, 2017
1 parent c2eaf27 commit e9cc711
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/subscribables/dependentObservable.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function computedBeginDependencyDetectionCallback(subscribable, id) {
}
// If the observable we've accessed has a pending notification, ensure we get notified of the actual final value (bypass equality checks)
if (subscribable._notificationIsPending) {
subscribable._notifyNextChange = true;
subscribable._notifyNextChangeIfValueIsDifferent();
}
}
}
Expand Down Expand Up @@ -333,10 +333,11 @@ var computedFn = {
state.isStale = state.isDirty = false;
}
},
peek: function () {
// Peek won't re-evaluate, except while the computed is sleeping or to get the initial value when "deferEvaluation" is set.
peek: function (evaluate) {
// By default, peek won't re-evaluate, except while the computed is sleeping or to get the initial value when "deferEvaluation" is set.
// Pass in true to evaluate if needed.
var state = this[computedState];
if ((state.isDirty && !state.dependenciesCount) || (state.isSleeping && this.haveDependenciesChanged())) {
if ((state.isDirty && (evaluate || !state.dependenciesCount)) || (state.isSleeping && this.haveDependenciesChanged())) {
this.evaluateImmediate();
}
return state.latestValue;
Expand Down
11 changes: 8 additions & 3 deletions src/subscribables/subscribable.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var ko_subscribable_fn = {

limit: function(limitFunction) {
var self = this, selfIsObservable = ko.isObservable(self),
ignoreBeforeChange, previousValue, pendingValue, beforeChange = 'beforeChange';
ignoreBeforeChange, notifyNextChange, previousValue, pendingValue, beforeChange = 'beforeChange';

if (!self._origNotifySubscribers) {
self._origNotifySubscribers = self["notifySubscribers"];
Expand All @@ -107,9 +107,9 @@ var ko_subscribable_fn = {
if (selfIsObservable && pendingValue === self) {
pendingValue = self._evalIfChanged ? self._evalIfChanged() : self();
}
var shouldNotify = self._notifyNextChange || self.isDifferent(previousValue, pendingValue);
var shouldNotify = notifyNextChange || self.isDifferent(previousValue, pendingValue);

self._notifyNextChange = ignoreBeforeChange = false;
notifyNextChange = ignoreBeforeChange = false;

if (shouldNotify) {
self._origNotifySubscribers(previousValue = pendingValue);
Expand All @@ -128,6 +128,11 @@ var ko_subscribable_fn = {
self._origNotifySubscribers(value, beforeChange);
}
};
self._notifyNextChangeIfValueIsDifferent = function() {
if (self.isDifferent(previousValue, self.peek(true /*evaluate*/))) {
notifyNextChange = true;
}
};
},

hasSubscriptionsForEvent: function(event) {
Expand Down

0 comments on commit e9cc711

Please sign in to comment.