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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 _values.add(value); | 52 _values.add(value); |
53 | 53 |
54 if (_pending <= 0) { | 54 if (_pending <= 0) { |
55 completed = true; | 55 completed = true; |
56 _completer.complete(_values); | 56 _completer.complete(_values); |
57 } | 57 } |
58 }).catchError((e) { | 58 }).catchError((e) { |
59 if (completed) return; | 59 if (completed) return; |
60 | 60 |
61 completed = true; | 61 completed = true; |
62 _completer.completeError(e.error, e.stackTrace); | 62 _completer.completeError(e); |
63 })); | 63 })); |
64 | 64 |
65 return task; | 65 return task; |
66 } | 66 } |
67 | 67 |
68 Future<List> get future => _completer.future; | 68 Future<List> get future => _completer.future; |
69 } | 69 } |
70 | 70 |
71 // TODO(rnystrom): Move into String? | 71 // TODO(rnystrom): Move into String? |
72 /// Pads [source] to [length] by adding spaces at the end. | 72 /// Pads [source] to [length] by adding spaces at the end. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 Future sleep(int milliseconds) { | 149 Future sleep(int milliseconds) { |
150 var completer = new Completer(); | 150 var completer = new Completer(); |
151 new Timer(new Duration(milliseconds: milliseconds), completer.complete); | 151 new Timer(new Duration(milliseconds: milliseconds), completer.complete); |
152 return completer.future; | 152 return completer.future; |
153 } | 153 } |
154 | 154 |
155 /// Configures [future] so that its result (success or exception) is passed on | 155 /// Configures [future] so that its result (success or exception) is passed on |
156 /// to [completer]. | 156 /// to [completer]. |
157 void chainToCompleter(Future future, Completer completer) { | 157 void chainToCompleter(Future future, Completer completer) { |
158 future.then((value) => completer.complete(value), | 158 future.then((value) => completer.complete(value), |
159 onError: (e) => completer.completeError(e.error, e.stackTrace)); | 159 onError: (e) => completer.completeError(e)); |
160 } | 160 } |
161 | 161 |
162 // TODO(nweiz): remove this when issue 7964 is fixed. | 162 // TODO(nweiz): remove this when issue 7964 is fixed. |
163 /// Returns a [Future] that will complete to the first element of [stream]. | 163 /// Returns a [Future] that will complete to the first element of [stream]. |
164 /// Unlike [Stream.first], this is safe to use with single-subscription streams. | 164 /// Unlike [Stream.first], this is safe to use with single-subscription streams. |
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); |
173 }, onDone: () { | 173 }, onDone: () { |
174 completer.completeError(new StateError("No elements")); | 174 completer.completeError(new StateError("No elements")); |
175 }, cancelOnError: 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 ? |
(...skipping 167 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 |