| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 utils; | 5 library utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 /// A pair of values. | 9 /// A pair of values. |
| 10 class Pair<E, F> { | 10 class Pair<E, F> { |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 completer.completeError(e.error, e.stackTrace); | 97 completer.completeError(e.error, e.stackTrace); |
| 98 } | 98 } |
| 99 }, onDone: () { | 99 }, onDone: () { |
| 100 if (!cancelled) { | 100 if (!cancelled) { |
| 101 completer.completeError(new StateError("No elements")); | 101 completer.completeError(new StateError("No elements")); |
| 102 } | 102 } |
| 103 }, unsubscribeOnError: true); | 103 }, unsubscribeOnError: true); |
| 104 return completer.future; | 104 return completer.future; |
| 105 } | 105 } |
| 106 | 106 |
| 107 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that | 107 /// A function that can be called to cancel a [Stream] and send a done message. |
| 108 /// can be used to control the wrapped stream. | 108 typedef void StreamCanceller(); |
| 109 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { | 109 |
| 110 // TODO(nweiz): use a StreamSubscription when issue 9026 is fixed. |
| 111 /// Returns a wrapped version of [stream] along with a function that will cancel |
| 112 /// the wrapped stream. Unlike [StreamSubscription], this canceller will send a |
| 113 /// "done" message to the wrapped stream. |
| 114 Pair<Stream, StreamCanceller> streamWithCanceller(Stream stream) { |
| 110 var controller = stream.isBroadcast ? | 115 var controller = stream.isBroadcast ? |
| 111 new StreamController.broadcast() : | 116 new StreamController.broadcast() : |
| 112 new StreamController(); | 117 new StreamController(); |
| 113 var subscription = stream.listen(controller.add, | 118 var subscription = stream.listen((value) { |
| 114 onError: controller.signalError, | 119 if (!controller.isClosed) controller.add(value); |
| 115 onDone: controller.close); | 120 }, onError: (error) { |
| 116 return new Pair<Stream, StreamSubscription>(controller.stream, subscription); | 121 if (!controller.isClosed) controller.signalError(error); |
| 122 }, onDone: controller.close); |
| 123 return new Pair<Stream, StreamCanceller>(controller.stream, controller.close); |
| 117 } | 124 } |
| 118 | 125 |
| 119 // TODO(nweiz): remove this when issue 7787 is fixed. | 126 // TODO(nweiz): remove this when issue 7787 is fixed. |
| 120 /// Creates two single-subscription [Stream]s that each emit all values and | 127 /// Creates two single-subscription [Stream]s that each emit all values and |
| 121 /// errors from [stream]. This is useful if [stream] is single-subscription but | 128 /// errors from [stream]. This is useful if [stream] is single-subscription but |
| 122 /// multiple subscribers are necessary. | 129 /// multiple subscribers are necessary. |
| 123 Pair<Stream, Stream> tee(Stream stream) { | 130 Pair<Stream, Stream> tee(Stream stream) { |
| 124 var controller1 = new StreamController(); | 131 var controller1 = new StreamController(); |
| 125 var controller2 = new StreamController(); | 132 var controller2 = new StreamController(); |
| 126 stream.listen((value) { | 133 stream.listen((value) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 153 .then((resolved) => new Pair(key, resolved))); | 160 .then((resolved) => new Pair(key, resolved))); |
| 154 }); | 161 }); |
| 155 return Future.wait(pairs).then((resolvedPairs) { | 162 return Future.wait(pairs).then((resolvedPairs) { |
| 156 var map = {}; | 163 var map = {}; |
| 157 for (var pair in resolvedPairs) { | 164 for (var pair in resolvedPairs) { |
| 158 map[pair.first] = pair.last; | 165 map[pair.first] = pair.last; |
| 159 } | 166 } |
| 160 return map; | 167 return map; |
| 161 }); | 168 }); |
| 162 } | 169 } |
| OLD | NEW |