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 library utils; | 5 library utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 // TODO(nweiz): remove this when issue 7787 is fixed. | 179 // TODO(nweiz): remove this when issue 7787 is fixed. |
180 /// Creates two single-subscription [Stream]s that each emit all values and | 180 /// Creates two single-subscription [Stream]s that each emit all values and |
181 /// errors from [stream]. This is useful if [stream] is single-subscription but | 181 /// errors from [stream]. This is useful if [stream] is single-subscription but |
182 /// multiple subscribers are necessary. | 182 /// multiple subscribers are necessary. |
183 Pair<Stream, Stream> tee(Stream stream) { | 183 Pair<Stream, Stream> tee(Stream stream) { |
184 var controller1 = new StreamController(sync: true); | 184 var controller1 = new StreamController(sync: true); |
185 var controller2 = new StreamController(sync: true); | 185 var controller2 = new StreamController(sync: true); |
186 stream.listen((value) { | 186 stream.listen((value) { |
187 controller1.add(value); | 187 controller1.add(value); |
188 controller2.add(value); | 188 controller2.add(value); |
189 }, onError: (error) { | 189 }, onError: (error, [StackTrace stackTrace]) { |
190 controller1.addError(error); | 190 controller1.addError(error, stackTrace); |
191 controller2.addError(error); | 191 controller2.addError(error, stackTrace); |
192 }, onDone: () { | 192 }, onDone: () { |
193 controller1.close(); | 193 controller1.close(); |
194 controller2.close(); | 194 controller2.close(); |
195 }); | 195 }); |
196 return new Pair<Stream, Stream>(controller1.stream, controller2.stream); | 196 return new Pair<Stream, Stream>(controller1.stream, controller2.stream); |
197 } | 197 } |
198 | 198 |
199 /// A pair of values. | 199 /// A pair of values. |
200 class Pair<E, F> { | 200 class Pair<E, F> { |
201 E first; | 201 E first; |
(...skipping 28 matching lines...) Expand all Loading... |
230 /// The return values of all [Future]s are discarded. Any errors will cause the | 230 /// The return values of all [Future]s are discarded. Any errors will cause the |
231 /// iteration to stop and will be piped through the return value. | 231 /// iteration to stop and will be piped through the return value. |
232 Future forEachFuture(Iterable input, Future fn(element)) { | 232 Future forEachFuture(Iterable input, Future fn(element)) { |
233 var iterator = input.iterator; | 233 var iterator = input.iterator; |
234 Future nextElement(_) { | 234 Future nextElement(_) { |
235 if (!iterator.moveNext()) return new Future.value(); | 235 if (!iterator.moveNext()) return new Future.value(); |
236 return fn(iterator.current).then(nextElement); | 236 return fn(iterator.current).then(nextElement); |
237 } | 237 } |
238 return nextElement(null); | 238 return nextElement(null); |
239 } | 239 } |
OLD | NEW |