watch<T> method

  1. @protected
  2. @mustCallSuper
T watch<T>(
  1. Valuable<T> valuable, {
  2. ValuableContext? valuableContext,
  3. 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;
}