blob: f30545e343cd14147bd2d1bf3ed8727f4ede437e [file] [log] [blame]
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library shelf_proxy.utils;
import 'dart:async';
// TODO(nweiz): remove this when issue 7786 is fixed.
/// Pipes all data and errors from [stream] into [sink].
/// When [stream] is done, the returned [Future] is completed and [sink] is
/// closed if [closeSink] is true.
/// When an error occurs on [stream], that error is passed to [sink]. If
/// [cancelOnError] is true, [Future] will be completed successfully and no
/// more data or errors will be piped from [stream] to [sink]. If
/// [cancelOnError] and [closeSink] are both true, [sink] will then be
/// closed.
Future store(Stream stream, EventSink sink,
{bool cancelOnError: true, bool closeSink: true}) {
var completer = new Completer();
stream.listen(sink.add, onError: (e, stackTrace) {
sink.addError(e, stackTrace);
if (cancelOnError) {
if (closeSink) sink.close();
}, onDone: () {
if (closeSink) sink.close();
}, cancelOnError: cancelOnError);
return completer.future;