toStream method

Stream<T> toStream({
  1. bool synchronous = false,
})

Returns a broadcast Stream that emits the current value and all subsequent updates to the value of this beacon. If synchronous is true, autobatching will be disabled and all updates will be emitted immediately.

Implementation

Stream<T> toStream({bool synchronous = false}) {
  _controller ??= StreamController<T>.broadcast(
    onListen: () {
      // onListen is only called when sub count goes from 0 to 1.
      // If sub count goes from 1 to 0, onCancel runs and sets _unsub to null.
      // so _unsub will always be null here but checking doesn't hurt
      _unsubFromSelf ??= subscribe(
        _controller!.add,
        synchronous: synchronous,
      );
    },
    onCancel: () {
      _unsubFromSelf?.call();
      _unsubFromSelf = null;
    },
  );

  return _controller!.stream;
}