watch<T> method
T
watch<T>(
- Valuable<
T> valuable, { - ValuableContext? valuableContext,
- ValuableWatcherSelector<
T> ? selector,
inherited
Watch a valuable, that eventually change
Implementation
@protected
@mustCallSuper
T watch<T>(Valuable<T> valuable,
{ValuableContext? valuableContext,
ValuableWatcherSelector<T>? selector}) {
T result;
result = valuable.getValue(valuableContext ?? this.valuableContext);
if (!_watched.containsKey(valuable)) {
VoidCallback callback = () => _callValuableChange<T>(valuable);
_watched.putIfAbsent(
valuable,
() => _ValuableWatchedInfos<T>(
callback: callback, previousWatchedValue: result));
valuable.addListener(callback);
valuable.listenDispose(() {
unwatch(valuable);
});
}
if (_watched.containsKey(valuable)) {
_ValuableWatchedInfos<T> infos =
_watched[valuable] as _ValuableWatchedInfos<T>;
infos.previousWatchedValue = result; // Save value for future comparaison
if (selector != null) {
infos.selectors.add(selector);
}
}
return result;
}