fromSubscription method Null safety

CancelableOperation<void> fromSubscription(
  1. StreamSubscription<void> subscription
)

Creates a CancelableOperation wrapping subscription.

This overrides subscription.onDone and subscription.onError so that the returned operation will complete when the subscription completes or emits an error. When this operation is canceled or when it emits an error, the subscription will be canceled (unlike CancelableOperation.fromFuture(subscription.asFuture())).

Implementation

static CancelableOperation<void> fromSubscription(
    StreamSubscription<void> subscription) {
  var completer = CancelableCompleter<void>(onCancel: subscription.cancel);
  subscription.onDone(completer.complete);
  subscription.onError((Object error, StackTrace stackTrace) {
    subscription.cancel().whenComplete(() {
      completer.completeError(error, stackTrace);
    });
  });
  return completer.operation;
}