StreamBridge<T> constructor

StreamBridge<T>({
  1. required Stream<T> source,
  2. bool subscribeOnFirstListen = false,
  3. void onFirstListen()?,
  4. void onSourceDone()?,
  5. void onSourceError(
    1. Object,
    2. StackTrace
    )?,
  6. void onConsumerCancel()?,
  7. void onCancelFailed(
    1. Object,
    2. StackTrace
    )?,
})

Mirrors source. Eager subscription is the default; a cold operation can opt into subscribeOnFirstListen. Hooks run before forwarding the event.

Implementation

StreamBridge({
  required Stream<T> source,
  bool subscribeOnFirstListen = false,
  void Function()? onFirstListen,
  void Function()? onSourceDone,
  void Function(Object, StackTrace)? onSourceError,
  void Function()? onConsumerCancel,
  void Function(Object, StackTrace)? onCancelFailed,
}) : _onFirstListen = onFirstListen,
     _onSourceDone = onSourceDone,
     _onSourceError = onSourceError,
     _onConsumerCancel = onConsumerCancel {
  _controller = StreamController<T>(
    onListen: () {
      if (_ended) return;
      final first = _onFirstListen;
      _onFirstListen = null;
      first?.call();
      if (!_ended && subscribeOnFirstListen) _input.start();
    },
    onPause: () => _input.pause(),
    onResume: () => _input.resume(),
    onCancel: () => _finish(notify: false),
  );
  _input = StreamSource<T>(
    source: _factory(source),
    onData: _controller.add,
    onError: (error, stack) {
      if (_ended) return;
      _onSourceError?.call(error, stack);
      if (!_ended) _controller.addError(error, stack);
    },
    onDone: () => _finish(sourceDone: true),
    onCancelFailed: onCancelFailed,
  );
  if (!subscribeOnFirstListen) _input.start();
}