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:io'; | 8 import 'dart:io'; |
9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
10 import 'dart:utf'; | 10 import 'dart:utf'; |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 166 |
167 /// Returns a [Future] that asynchronously completes to `null`. | 167 /// Returns a [Future] that asynchronously completes to `null`. |
168 Future get async => new Future.value(); | 168 Future get async => new Future.value(); |
169 | 169 |
170 /// Returns a closed [Stream] with no elements. | 170 /// Returns a closed [Stream] with no elements. |
171 Stream get emptyStream => streamFromIterable([]); | 171 Stream get emptyStream => streamFromIterable([]); |
172 | 172 |
173 /// Creates a single-subscription stream that emits the items in [iter] and then | 173 /// Creates a single-subscription stream that emits the items in [iter] and then |
174 /// ends. | 174 /// ends. |
175 Stream streamFromIterable(Iterable iter) { | 175 Stream streamFromIterable(Iterable iter) { |
176 var controller = new StreamController(); | 176 var controller = new StreamController(sync: true); |
177 iter.forEach(controller.add); | 177 iter.forEach(controller.add); |
178 controller.close(); | 178 controller.close(); |
179 return controller.stream; | 179 return controller.stream; |
180 } | 180 } |
181 | 181 |
182 // TODO(nweiz): remove this when issue 7787 is fixed. | 182 // TODO(nweiz): remove this when issue 7787 is fixed. |
183 /// Creates two single-subscription [Stream]s that each emit all values and | 183 /// Creates two single-subscription [Stream]s that each emit all values and |
184 /// errors from [stream]. This is useful if [stream] is single-subscription but | 184 /// errors from [stream]. This is useful if [stream] is single-subscription but |
185 /// multiple subscribers are necessary. | 185 /// multiple subscribers are necessary. |
186 Pair<Stream, Stream> tee(Stream stream) { | 186 Pair<Stream, Stream> tee(Stream stream) { |
187 var controller1 = new StreamController(); | 187 var controller1 = new StreamController(sync: true); |
188 var controller2 = new StreamController(); | 188 var controller2 = new StreamController(sync: true); |
189 stream.listen((value) { | 189 stream.listen((value) { |
190 controller1.add(value); | 190 controller1.add(value); |
191 controller2.add(value); | 191 controller2.add(value); |
192 }, onError: (error) { | 192 }, onError: (error) { |
193 controller1.addError(error); | 193 controller1.addError(error); |
194 controller2.addError(error); | 194 controller2.addError(error); |
195 }, onDone: () { | 195 }, onDone: () { |
196 controller1.close(); | 196 controller1.close(); |
197 controller2.close(); | 197 controller2.close(); |
198 }); | 198 }); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 /// The return values of all [Future]s are discarded. Any errors will cause the | 233 /// The return values of all [Future]s are discarded. Any errors will cause the |
234 /// iteration to stop and will be piped through the return value. | 234 /// iteration to stop and will be piped through the return value. |
235 Future forEachFuture(Iterable input, Future fn(element)) { | 235 Future forEachFuture(Iterable input, Future fn(element)) { |
236 var iterator = input.iterator; | 236 var iterator = input.iterator; |
237 Future nextElement(_) { | 237 Future nextElement(_) { |
238 if (!iterator.moveNext()) return new Future.value(); | 238 if (!iterator.moveNext()) return new Future.value(); |
239 return fn(iterator.current).then(nextElement); | 239 return fn(iterator.current).then(nextElement); |
240 } | 240 } |
241 return nextElement(null); | 241 return nextElement(null); |
242 } | 242 } |
OLD | NEW |