OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
6 library utils; | 6 library utils; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:crypto'; | 9 import 'dart:crypto'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 completer.completeError(e.error, e.stackTrace); | 172 completer.completeError(e.error, e.stackTrace); |
173 }, onDone: () { | 173 }, onDone: () { |
174 completer.completeError(new StateError("No elements")); | 174 completer.completeError(new StateError("No elements")); |
175 }, unsubscribeOnError: true); | 175 }, unsubscribeOnError: true); |
176 return completer.future; | 176 return completer.future; |
177 } | 177 } |
178 | 178 |
179 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that | 179 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that |
180 /// can be used to control the wrapped stream. | 180 /// can be used to control the wrapped stream. |
181 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { | 181 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { |
182 var controller = stream.isBroadcast ? | 182 var controller = new StreamController(); |
183 new StreamController.broadcast() : | 183 var controllerStream = stream.isBroadcast ? |
184 new StreamController(); | 184 controller.stream.asBroadcastStream() : |
| 185 controller.stream; |
185 var subscription = stream.listen(controller.add, | 186 var subscription = stream.listen(controller.add, |
186 onError: controller.addError, | 187 onError: controller.addError, |
187 onDone: controller.close); | 188 onDone: controller.close); |
188 return new Pair<Stream, StreamSubscription>(controller.stream, subscription); | 189 return new Pair<Stream, StreamSubscription>(controllerStream, subscription); |
189 } | 190 } |
190 | 191 |
191 // TODO(nweiz): remove this when issue 7787 is fixed. | 192 // TODO(nweiz): remove this when issue 7787 is fixed. |
192 /// Creates two single-subscription [Stream]s that each emit all values and | 193 /// Creates two single-subscription [Stream]s that each emit all values and |
193 /// errors from [stream]. This is useful if [stream] is single-subscription but | 194 /// errors from [stream]. This is useful if [stream] is single-subscription but |
194 /// multiple subscribers are necessary. | 195 /// multiple subscribers are necessary. |
195 Pair<Stream, Stream> tee(Stream stream) { | 196 Pair<Stream, Stream> tee(Stream stream) { |
196 var controller1 = new StreamController(); | 197 var controller1 = new StreamController(); |
197 var controller2 = new StreamController(); | 198 var controller2 = new StreamController(); |
198 stream.listen((value) { | 199 stream.listen((value) { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 .then((resolved) => new Pair(key, resolved))); | 351 .then((resolved) => new Pair(key, resolved))); |
351 }); | 352 }); |
352 return Future.wait(pairs).then((resolvedPairs) { | 353 return Future.wait(pairs).then((resolvedPairs) { |
353 var map = {}; | 354 var map = {}; |
354 for (var pair in resolvedPairs) { | 355 for (var pair in resolvedPairs) { |
355 map[pair.first] = pair.last; | 356 map[pair.first] = pair.last; |
356 } | 357 } |
357 return map; | 358 return map; |
358 }); | 359 }); |
359 } | 360 } |
OLD | NEW |