StreamBridge<T> constructor
StreamBridge<T> ({})
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();
}