| // 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. |
| |
| 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 = Completer(); |
| stream.listen(sink.add, onError: (e, StackTrace stackTrace) { |
| sink.addError(e, stackTrace); |
| if (cancelOnError) { |
| completer.complete(); |
| if (closeSink) sink.close(); |
| } |
| }, onDone: () { |
| if (closeSink) sink.close(); |
| completer.complete(); |
| }, cancelOnError: cancelOnError); |
| return completer.future; |
| } |