toStream method

Stream<T> toStream()

Convert a signal to a Stream to be consumed as a read only stream.

Implementation

Stream<T> toStream() {
  final existing = _streamCache[globalId];

  if (existing != null) {
    return existing as Stream<T>;
  }

  final controller = StreamController<T>();

  final stream = controller.stream.asBroadcastStream();

  _streamCache[globalId] = stream;

  subscribe(controller.add);

  onDispose(() {
    _streamCache.remove(globalId);
    controller.close();
  });

  return stream;
}