subscriptionTransformer<T> function

StreamTransformer<T, T> subscriptionTransformer<T>({
  1. Future handleCancel(
    1. StreamSubscription<T>
    )?,
  2. void handlePause(
    1. StreamSubscription<T>
    )?,
  3. void handleResume(
    1. StreamSubscription<T>
    )?,
})

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();
            });
  });
}