Skip to content

Commit

Permalink
Migrate out some of the dispose callback stuff. This is all quite tan…
Browse files Browse the repository at this point in the history
…gled up and could use further refactoring.
  • Loading branch information
SteveSanderson committed Aug 10, 2015
1 parent df34187 commit 74229a3
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/subscribables/dependentObservable.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,13 @@ ko.computed = ko.dependentObservable = function (evaluatorFunctionOrOptions, eva
disposeWhenNodeIsRemoved: options["disposeWhenNodeIsRemoved"] || options.disposeWhenNodeIsRemoved || null,
disposeWhenOption: options["disposeWhen"] || options.disposeWhen,
disposeWhen: options["disposeWhen"] || options.disposeWhen,
disposeHandler: disposeComputed,
dependencyTracking: {},
_dependenciesCount: 0,
evaluationTimeoutInstance: null,
evaluatorFunctionTarget: evaluatorFunctionTarget || options["owner"],
options: options
};

function disposeComputed() {
if (!state.isSleeping && state.dependencyTracking) {
ko.utils.objectForEach(state.dependencyTracking, function (id, dependency) {
if (dependency.dispose)
dependency.dispose();
});
}
state.dependencyTracking = null;
state._dependenciesCount = 0;
state._isDisposed = true;
state._needsEvaluation = false;
state.isSleeping = false;
}

function dependentObservable() {
if (arguments.length > 0) {
if (typeof state.writeFunction === "function") {
Expand All @@ -69,6 +54,9 @@ ko.computed = ko.dependentObservable = function (evaluatorFunctionOrOptions, eva
dependentObservable.state = state;
dependentObservable.hasWriteFunction = typeof state.writeFunction === "function";

// Unfortunately this function has to be bound to the dependentObservable. Ideally this should be refactored.
state.disposeHandler = function() { dependentObservable.disposeComputed(); };

ko.subscribable.call(dependentObservable);
ko.utils.setPrototypeOfOrExtend(dependentObservable, ko.dependentObservable['fn']);

Expand Down Expand Up @@ -112,7 +100,7 @@ ko.computed = ko.dependentObservable = function (evaluatorFunctionOrOptions, eva
if (state.disposeWhenNodeIsRemoved && dependentObservable.isActive() && state.disposeWhenNodeIsRemoved.nodeType) {
state.disposeHandler = function() {
ko.utils.domNodeDisposal.removeDisposeCallback(state.disposeWhenNodeIsRemoved, state.disposeHandler);
disposeComputed();
dependentObservable.disposeComputed();
};
ko.utils.domNodeDisposal.addDisposeCallback(state.disposeWhenNodeIsRemoved, state.disposeHandler);
}
Expand Down Expand Up @@ -329,6 +317,20 @@ ko.dependentObservable['fn'] = {
},
dispose: function() {
this.state.disposeHandler();
},
disposeComputed: function() {
var state = this.state;
if (!state.isSleeping && state.dependencyTracking) {
ko.utils.objectForEach(state.dependencyTracking, function (id, dependency) {
if (dependency.dispose)
dependency.dispose();
});
}
state.dependencyTracking = null;
state._dependenciesCount = 0;
state._isDisposed = true;
state._needsEvaluation = false;
state.isSleeping = false;
}
};

Expand Down

0 comments on commit 74229a3

Please sign in to comment.