watch method

T watch(
  1. BuildContext context
)

The current value, rebuilding the reading element when it changes.

Implementation

T watch(BuildContext context) {
  final Element element = context as Element;
  final Map<DVMediaSignal<Object?>, StreamSubscription<Object?>> subs =
      _watchers[element] ??=
          <DVMediaSignal<Object?>, StreamSubscription<Object?>>{};
  subs.putIfAbsent(this, () {
    late final StreamSubscription<T> subscription;
    subscription = changes.listen((T _) {
      if (element.mounted) {
        element.markNeedsBuild();
        return;
      }
      // Nothing tells a signal an element went away; the first change
      // after it did is when the subscription goes.
      subs.remove(this);
      unawaited(subscription.cancel());
    });
    return subscription;
  });
  return value;
}