| 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:crypto'; | 8 import 'dart:crypto'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import 'dart:scalarlist'; | 10 import 'dart:scalarlist'; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 pair.first.listen((_) {}, onError: (_) {}, onDone: onDone); | 148 pair.first.listen((_) {}, onError: (_) {}, onDone: onDone); |
| 149 return pair.last; | 149 return pair.last; |
| 150 } | 150 } |
| 151 | 151 |
| 152 // TODO(nweiz): remove this once issue 7785 is fixed. | 152 // TODO(nweiz): remove this once issue 7785 is fixed. |
| 153 /// Wraps [stream] in a single-subscription [ByteStream] that emits the same | 153 /// Wraps [stream] in a single-subscription [ByteStream] that emits the same |
| 154 /// data. | 154 /// data. |
| 155 ByteStream wrapInputStream(InputStream stream) { | 155 ByteStream wrapInputStream(InputStream stream) { |
| 156 if (stream.closed) return emptyStream; | 156 if (stream.closed) return emptyStream; |
| 157 | 157 |
| 158 var controller = new StreamController.singleSubscription(); | 158 var controller = new StreamController(); |
| 159 stream.onClosed = controller.close; | 159 stream.onClosed = controller.close; |
| 160 stream.onData = () => controller.add(stream.read()); | 160 stream.onData = () => controller.add(stream.read()); |
| 161 stream.onError = (e) => controller.signalError(new AsyncError(e)); | 161 stream.onError = (e) => controller.signalError(new AsyncError(e)); |
| 162 return new ByteStream(controller); | 162 return new ByteStream(controller); |
| 163 } | 163 } |
| 164 | 164 |
| 165 // TODO(nweiz): remove this once issue 7785 is fixed. | 165 // TODO(nweiz): remove this once issue 7785 is fixed. |
| 166 /// Wraps [stream] in a [StreamConsumer] so that [Stream]s can by piped into it | 166 /// Wraps [stream] in a [StreamConsumer] so that [Stream]s can by piped into it |
| 167 /// using [Stream.pipe]. | 167 /// using [Stream.pipe]. |
| 168 StreamConsumer<List<int>, dynamic> wrapOutputStream(OutputStream stream) => | 168 StreamConsumer<List<int>, dynamic> wrapOutputStream(OutputStream stream) => |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 226 |
| 227 /// Returns a [Future] that asynchronously completes to `null`. | 227 /// Returns a [Future] that asynchronously completes to `null`. |
| 228 Future get async => new Future.immediate(null); | 228 Future get async => new Future.immediate(null); |
| 229 | 229 |
| 230 /// Returns a closed [Stream] with no elements. | 230 /// Returns a closed [Stream] with no elements. |
| 231 Stream get emptyStream => streamFromIterable([]); | 231 Stream get emptyStream => streamFromIterable([]); |
| 232 | 232 |
| 233 /// Creates a single-subscription stream that emits the items in [iter] and then | 233 /// Creates a single-subscription stream that emits the items in [iter] and then |
| 234 /// ends. | 234 /// ends. |
| 235 Stream streamFromIterable(Iterable iter) { | 235 Stream streamFromIterable(Iterable iter) { |
| 236 var stream = new StreamController.singleSubscription(); | 236 var stream = new StreamController(); |
| 237 iter.forEach(stream.add); | 237 iter.forEach(stream.add); |
| 238 stream.close(); | 238 stream.close(); |
| 239 return stream.stream; | 239 return stream.stream; |
| 240 } | 240 } |
| 241 | 241 |
| 242 // TODO(nweiz): remove this when issue 7787 is fixed. | 242 // TODO(nweiz): remove this when issue 7787 is fixed. |
| 243 /// Creates two single-subscription [Stream]s that each emit all values and | 243 /// Creates two single-subscription [Stream]s that each emit all values and |
| 244 /// errors from [stream]. This is useful if [stream] is single-subscription but | 244 /// errors from [stream]. This is useful if [stream] is single-subscription but |
| 245 /// multiple subscribers are necessary. | 245 /// multiple subscribers are necessary. |
| 246 Pair<Stream, Stream> tee(Stream stream) { | 246 Pair<Stream, Stream> tee(Stream stream) { |
| 247 var controller1 = new StreamController.singleSubscription(); | 247 var controller1 = new StreamController(); |
| 248 var controller2 = new StreamController.singleSubscription(); | 248 var controller2 = new StreamController(); |
| 249 stream.listen((value) { | 249 stream.listen((value) { |
| 250 controller1.add(value); | 250 controller1.add(value); |
| 251 controller2.add(value); | 251 controller2.add(value); |
| 252 }, onError: (error) { | 252 }, onError: (error) { |
| 253 controller1.signalError(error); | 253 controller1.signalError(error); |
| 254 controller2.signalError(error); | 254 controller2.signalError(error); |
| 255 }, onDone: () { | 255 }, onDone: () { |
| 256 controller1.close(); | 256 controller1.close(); |
| 257 controller2.close(); | 257 controller2.close(); |
| 258 }); | 258 }); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 /// The return values of all [Future]s are discarded. Any errors will cause the | 293 /// The return values of all [Future]s are discarded. Any errors will cause the |
| 294 /// iteration to stop and will be piped through the return value. | 294 /// iteration to stop and will be piped through the return value. |
| 295 Future forEachFuture(Iterable input, Future fn(element)) { | 295 Future forEachFuture(Iterable input, Future fn(element)) { |
| 296 var iterator = input.iterator; | 296 var iterator = input.iterator; |
| 297 Future nextElement(_) { | 297 Future nextElement(_) { |
| 298 if (!iterator.moveNext()) return new Future.immediate(null); | 298 if (!iterator.moveNext()) return new Future.immediate(null); |
| 299 return fn(iterator.current).then(nextElement); | 299 return fn(iterator.current).then(nextElement); |
| 300 } | 300 } |
| 301 return nextElement(null); | 301 return nextElement(null); |
| 302 } | 302 } |
| OLD | NEW |