clientStream<I, O> function
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 _) {}));
}
}