clientStream<I, O> function

Future<O> clientStream<I, O>(
  1. Transport transport,
  2. Method method,
  3. Stream<I> requests,
  4. Codec<I> input,
  5. Codec<O> output, {
  6. CallOptions options = const CallOptions(),
})

Implementation

Future<O> clientStream<I, O>(Transport transport, Method method,
    Stream<I> requests, Codec<I> input, Codec<O> output,
    {CallOptions options = const CallOptions()}) async {
  final call = await transport.open(method, options: options);
  final iterator = StreamIterator(requests);
  final failure = Completer<O>();
  var stopped = false;
  // Observe send failures and terminal status concurrently, including servers
  // which reject a stream before all requests have been consumed.
  final sending = () async {
    while (!stopped && await iterator.moveNext()) {
      if (!stopped) await call.send(input.encode(iterator.current));
    }
    if (!stopped) await call.halfClose();
  }();
  unawaited(sending.then((_) {}, onError: (Object error, StackTrace stack) {
    if (error is RequestClosedError) return;
    if (!failure.isCompleted) failure.completeError(error, stack);
  }));
  try {
    return await Future.any([_single(call, output), failure.future]);
  } finally {
    stopped = true;
    await call.close();
    // User streams may delay their cancellation callback indefinitely. Request
    // cleanup without holding the completed RPC open on application work.
    unawaited(iterator.cancel().catchError((Object _) {}));
  }
}