Search code examples
knockout.jscomputed-observable

Prevent computed from collecting dependencies


Is there a good way to roughly achieve the following concept:

var computed = ko.computed(function() {
    readSomeObservables(); //<-- if these change, notify computed
    ko.stopCollectingDependencies();
    readSomeMoreObservables(); //<-- if these change, do not notify computed
    ko.resumeCollectingDependencies();
});

I am aware of peek(), but in this case the computed is invoking methods that were provided from an external module, and the design calls for it to be purely incidental if those methods happen to involve observables.

I have one solution, which is to roughly do this:

window.setTimeout(function() {
    readSomeMoreObservables();
}, 0);

But for obvious reasons, that is hardly ideal and leads to undesired behavior in some situations.


Solution

  • What about a combination. Create a temp computed for the subscribeables you need to read but do not want to subscribe to. Changing them would update the temp computed but that could be a cheap operation. Your real computed reads the tempComputed with peek accessing the currently cached value.

    // this one is updated 
    // if any of the subscribeables used in readSomeMoreObservables changes
    // but that is hopefully cheap
    var tempComputed = ko.computed(function() {
        readSomeMoreObservables();
    });
    
    
    var computed = ko.computed(function() {
        readSomeObservables(); //<-- if these change, notify computed
    
        // do not update on readSomeMoreObservables
        tempComputed.peek(); 
    });