Open szagi3891 opened 8 months ago
The TC39 Signals proposal includes a similar feature:
interface SignalOptions<T> {
// Custom comparison function between old and new value. Default: Object.is.
// The signal is passed in as the this value for context.
equals?: (this: Signal<T>, t: T, t2: T) => boolean;
// Callback called when isWatched becomes true, if it was previously false
[Signal.subtle.watched]?: (this: Signal<T>) => void;
// Callback called whenever isWatched becomes false, if it was previously true
[Signal.subtle.unwatched]?: (this: Signal<T>) => void;
}
watched
and unwatched
can be used, for example, to start (or stop) observing a foreign data source on demand, only when the signal is actually being used.
This callback could behave like this:
For example, MobX implements it like this: https://mobx.js.org/lazy-observables.html