stream<T> method
Returns a Stream that emits values when a specific cache key is updated.
- Parameters:
keyThe cache key to watch.- Returns: A Stream that emits values of type
Twhen 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;
}