OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library shelf_proxy.utils; | |
6 | |
7 import 'dart:async'; | 5 import 'dart:async'; |
8 | 6 |
9 // TODO(nweiz): remove this when issue 7786 is fixed. | 7 // TODO(nweiz): remove this when issue 7786 is fixed. |
10 /// Pipes all data and errors from [stream] into [sink]. | 8 /// Pipes all data and errors from [stream] into [sink]. |
11 /// | 9 /// |
12 /// When [stream] is done, the returned [Future] is completed and [sink] is | 10 /// When [stream] is done, the returned [Future] is completed and [sink] is |
13 /// closed if [closeSink] is true. | 11 /// closed if [closeSink] is true. |
14 /// | 12 /// |
15 /// When an error occurs on [stream], that error is passed to [sink]. If | 13 /// When an error occurs on [stream], that error is passed to [sink]. If |
16 /// [cancelOnError] is true, [Future] will be completed successfully and no | 14 /// [cancelOnError] is true, [Future] will be completed successfully and no |
17 /// more data or errors will be piped from [stream] to [sink]. If | 15 /// more data or errors will be piped from [stream] to [sink]. If |
18 /// [cancelOnError] and [closeSink] are both true, [sink] will then be | 16 /// [cancelOnError] and [closeSink] are both true, [sink] will then be |
19 /// closed. | 17 /// closed. |
20 Future store(Stream stream, EventSink sink, | 18 Future store(Stream stream, EventSink sink, |
21 {bool cancelOnError: true, bool closeSink: true}) { | 19 {bool cancelOnError: true, bool closeSink: true}) { |
22 var completer = new Completer(); | 20 var completer = new Completer(); |
23 stream.listen(sink.add, onError: (e, stackTrace) { | 21 stream.listen(sink.add, onError: (e, stackTrace) { |
24 sink.addError(e, stackTrace); | 22 sink.addError(e, stackTrace); |
25 if (cancelOnError) { | 23 if (cancelOnError) { |
26 completer.complete(); | 24 completer.complete(); |
27 if (closeSink) sink.close(); | 25 if (closeSink) sink.close(); |
28 } | 26 } |
29 }, onDone: () { | 27 }, onDone: () { |
30 if (closeSink) sink.close(); | 28 if (closeSink) sink.close(); |
31 completer.complete(); | 29 completer.complete(); |
32 }, cancelOnError: cancelOnError); | 30 }, cancelOnError: cancelOnError); |
33 return completer.future; | 31 return completer.future; |
34 } | 32 } |
OLD | NEW |