Permalink
Browse files

Use new 2.1 subscription management (will still work with 2.0).

  • Loading branch information...
1 parent 73f4233 commit 8f9f2dd110e13d6523f148ca53778eba3ba08c2e @mbest committed Apr 19, 2012
Showing with 25 additions and 5 deletions.
  1. +25 −5 knockout-deferred-updates.js
@@ -178,10 +178,12 @@ subFnObj[subFnName] = function (callback, callbackTarget, event, deferUpdates) {
else
ko.ignoreDependencies(callback, callbackTarget, [valueToNotify]);
};
- return this.oldSubscribe(newCallback, undefined, event);
+ var subscription = this.oldSubscribe(newCallback, undefined, event);
} else {
- return this.oldSubscribe(callback, callbackTarget, event);
+ var subscription = this.oldSubscribe(callback, callbackTarget, event);
}
+ subscription.target = this;
+ return subscription;
}
@@ -258,12 +260,30 @@ var newComputed = function (evaluatorFunctionOrOptions, evaluatorFunctionTarget,
_isBeingEvaluated = true;
try {
- disposeAllSubscriptionsToDependencies();
- depDet[depDetBeginName](addDependency);
+ // Initially, we assume that none of the subscriptions are still being used (i.e., all are candidates for disposal).
+ // Then, during evaluation, we cross off any that are in fact still being used.
+ var disposalCandidates = ko.utils.arrayMap(_subscriptionsToDependencies, function(item) {return item.target;});
+
+ depDet[depDetBeginName](function(subscribable) {
+ var inOld;
+ if ((inOld = ko.utils.arrayIndexOf(disposalCandidates, subscribable)) >= 0)
+ disposalCandidates[inOld] = undefined; // Don't want to dispose this subscription, as it's still being used
+ else
+ addDependency(subscribable); // Brand new subscription - add it
+ });
+
var newValue = readFunction.call(evaluatorFunctionTarget);
+
+ // For each subscription no longer being used, remove it from the active subscriptions list and dispose it
+ for (var i = disposalCandidates.length - 1; i >= 0; i--) {
+ if (disposalCandidates[i])
+ _subscriptionsToDependencies.splice(i, 1)[0].dispose();
+ }
+
+ _needsEvaluation = false;
+
dependentObservable["notifySubscribers"](_latestValue, "beforeChange");
_latestValue = newValue;
- _needsEvaluation = false;
} finally {
depDet.end();
}

0 comments on commit 8f9f2dd

Please sign in to comment.