| 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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 Future streamFirst(Stream stream) { | 165 Future streamFirst(Stream stream) { |
| 166 var completer = new Completer(); | 166 var completer = new Completer(); |
| 167 var subscription; | 167 var subscription; |
| 168 subscription = stream.listen((value) { | 168 subscription = stream.listen((value) { |
| 169 subscription.cancel(); | 169 subscription.cancel(); |
| 170 completer.complete(value); | 170 completer.complete(value); |
| 171 }, onError: (e) { | 171 }, onError: (e) { |
| 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 }, cancelOnError: 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 = stream.isBroadcast ? |
| 183 new StreamController.broadcast() : | 183 new StreamController.broadcast() : |
| 184 new StreamController(); | 184 new StreamController(); |
| 185 var subscription = stream.listen(controller.add, | 185 var subscription = stream.listen(controller.add, |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 .then((resolved) => new Pair(key, resolved))); | 350 .then((resolved) => new Pair(key, resolved))); |
| 351 }); | 351 }); |
| 352 return Future.wait(pairs).then((resolvedPairs) { | 352 return Future.wait(pairs).then((resolvedPairs) { |
| 353 var map = {}; | 353 var map = {}; |
| 354 for (var pair in resolvedPairs) { | 354 for (var pair in resolvedPairs) { |
| 355 map[pair.first] = pair.last; | 355 map[pair.first] = pair.last; |
| 356 } | 356 } |
| 357 return map; | 357 return map; |
| 358 }); | 358 }); |
| 359 } | 359 } |
| OLD | NEW |