stream<T> method

Stream<T> stream<T>(
  1. String key
)

Returns a Stream that emits values when a specific cache key is updated.

  • Parameters:
  • key The cache key to watch.
  • Returns: A Stream that emits values of type T when the key is updated.
  • Throws: An ArgumentError if the key is already being observed by a different type.

Implementation

Stream<T> stream<T>(String key) {
  final mObservers = observers[key]?.streamObservers();
  if (mObservers != null && mObservers.isNotEmpty) {
    for (final observer in mObservers) {
      if (observer.controller.stream is Stream<T>) {
        return observer.controller.stream as Stream<T>;
      }
    }
    throw ArgumentError(
        'Key $key is already being observed by a different type');
  }
  // No existing observer of the correct type was found, create a new one
  final controller = StreamController<T>.broadcast();
  final observer = StreamCacheObserver<T>(controller);
  watch(key, observer);
  return controller.stream;
}