subscriptionTransformer< T> function
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 handleCancel(StreamSubscription<T> inner),
void handlePause(StreamSubscription<T> inner),
void handleResume(StreamSubscription<T> inner)}) {
return new StreamTransformer((stream, cancelOnError) {
return new _TransformedSubscription(
stream.listen(null, cancelOnError: cancelOnError),
handleCancel ?? (inner) => inner.cancel(),
handlePause ??
(inner) {
inner.pause();
},
handleResume ??
(inner) {
inner.resume();
});
});
}