knockout/knockout — pull request #1576 by mbest (2014-10-08 03:58:21+00:00)
Title: computed observables notify "awake" and "asleep" events

As described in #1511, this would facilitate the ability to have "spectator" subscriptions using a fairly simple wrapper.

1. A pure computed only awakens when a `change` subscription is registered and goes to sleep when there are no more `change` subscriptions. This means we can use other subscription types that don't affect the computed state. A deferred computed "awakens" when read or when a `change` or `beforeChange` subscription is registered.
2. When awakening, a pure or deferred computed will notify an `awake` event with the current computed value. And when going to sleep, a pure computed will notify an `asleep` event with an `undefined` value.

By overwriting `notifySubscribers`, you could use these new events and the `change` event to generate `spectate` events:

```
ko.subscribable.fn.notifySubscribers = (function (original) {
return function (value, event) {
if (!event || event == 'change' || event == 'awake' || event == 'asleep') {
original.call(this, value, 'spectate');
}
original.apply(this, arguments);
};
}(ko.subscribable.fn.notifySubscribers));
```