Should a Knockout computed that reads non-observable state like clock time be a pureComputed?
0 reputation · 20 Nov 2023, 06:46 UTC
I'm designing a view model in Knockout 3.x where a computed value derives partly from observables and partly from state that Knockout cannot track, such as the current clock time or a value held in an external store outside the observable graph.
My understanding is that ko.pureComputed sleeps whenever it has no subscribers: it stops re-evaluating, ignores changes to its dependencies while asleep, and only recomputes when something subscribes again. That seems fine for pure derivations over observables, but for non-observable state there is no dependency to wake it at all, so the value could go stale indefinitely while subscribed, or silently skip intermediate states after waking.
The alternative is a regular ko.computed, which stays live even without subscribers, but that still would not re-evaluate when untracked state changes, and it loses the memory benefits of the pure variant.
Is a computed that reads non-observable state ever a legitimate candidate for ko.pureComputed, or does the sleep/wake model rule it out by design? Is wrapping the external state in an observable refreshed by a timer or event the intended pattern, and does enabling ko.options.deferUpdates change any of these trade-offs?