subscriptionTransformer<T> function
- Future handleCancel()?,
- void handlePause()?,
- void handleResume()?,
Creates a StreamTransformer that modifies the behavior of subscriptions to a stream.
When StreamSubscription.cancel, StreamSubscription.pause, or StreamSubscription.resume is called, the corresponding handler is invoked. By default, handlers just forward to the underlying subscription.
Guarantees that none of the StreamSubscription callbacks and none of the
callbacks passed to subscriptionTransformer()
will be invoked once the
transformed StreamSubscription has been canceled and handleCancel()
has
run. The handlePause
and handleResume
are invoked regardless of whether
the subscription is paused already or not.
In order to preserve StreamSubscription guarantees, all callbacks must
synchronously call the corresponding method on the inner
StreamSubscription: handleCancel
must call cancel()
, handlePause
must call pause()
, and handleResume
must call resume()
.
Implementation
StreamTransformer<T, T> subscriptionTransformer<T>(
{Future Function(StreamSubscription<T>)? handleCancel,
void Function(StreamSubscription<T>)? handlePause,
void Function(StreamSubscription<T>)? handleResume}) {
return StreamTransformer((stream, cancelOnError) {
return _TransformedSubscription(
stream.listen(null, cancelOnError: cancelOnError),
handleCancel ?? (inner) => inner.cancel(),
handlePause ??
(inner) {
inner.pause();
},
handleResume ??
(inner) {
inner.resume();
});
});
}