| 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:io'; | 9 import 'dart:io'; |
| 10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 completer.completeError(e); | 176 completer.completeError(e); |
| 177 }, onDone: () { | 177 }, onDone: () { |
| 178 completer.completeError(new StateError("No elements")); | 178 completer.completeError(new StateError("No elements")); |
| 179 }, cancelOnError: true); | 179 }, cancelOnError: true); |
| 180 return completer.future; | 180 return completer.future; |
| 181 } | 181 } |
| 182 | 182 |
| 183 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that | 183 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that |
| 184 /// can be used to control the wrapped stream. | 184 /// can be used to control the wrapped stream. |
| 185 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { | 185 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { |
| 186 var controller = new StreamController(); | 186 var controller = new StreamController(sync: true); |
| 187 var controllerStream = stream.isBroadcast ? | 187 var controllerStream = stream.isBroadcast ? |
| 188 controller.stream.asBroadcastStream() : | 188 controller.stream.asBroadcastStream() : |
| 189 controller.stream; | 189 controller.stream; |
| 190 var subscription = stream.listen(controller.add, | 190 var subscription = stream.listen(controller.add, |
| 191 onError: controller.addError, | 191 onError: controller.addError, |
| 192 onDone: controller.close); | 192 onDone: controller.close); |
| 193 return new Pair<Stream, StreamSubscription>(controllerStream, subscription); | 193 return new Pair<Stream, StreamSubscription>(controllerStream, subscription); |
| 194 } | 194 } |
| 195 | 195 |
| 196 // TODO(nweiz): remove this when issue 7787 is fixed. | 196 // TODO(nweiz): remove this when issue 7787 is fixed. |
| 197 /// Creates two single-subscription [Stream]s that each emit all values and | 197 /// Creates two single-subscription [Stream]s that each emit all values and |
| 198 /// errors from [stream]. This is useful if [stream] is single-subscription but | 198 /// errors from [stream]. This is useful if [stream] is single-subscription but |
| 199 /// multiple subscribers are necessary. | 199 /// multiple subscribers are necessary. |
| 200 Pair<Stream, Stream> tee(Stream stream) { | 200 Pair<Stream, Stream> tee(Stream stream) { |
| 201 var controller1 = new StreamController(); | 201 var controller1 = new StreamController(sync: true); |
| 202 var controller2 = new StreamController(); | 202 var controller2 = new StreamController(sync: true); |
| 203 stream.listen((value) { | 203 stream.listen((value) { |
| 204 controller1.add(value); | 204 controller1.add(value); |
| 205 controller2.add(value); | 205 controller2.add(value); |
| 206 }, onError: (error) { | 206 }, onError: (error) { |
| 207 controller1.addError(error); | 207 controller1.addError(error); |
| 208 controller2.addError(error); | 208 controller2.addError(error); |
| 209 }, onDone: () { | 209 }, onDone: () { |
| 210 controller1.close(); | 210 controller1.close(); |
| 211 controller2.close(); | 211 controller2.close(); |
| 212 }); | 212 }); |
| 213 return new Pair<Stream, Stream>(controller1.stream, controller2.stream); | 213 return new Pair<Stream, Stream>(controller1.stream, controller2.stream); |
| 214 } | 214 } |
| 215 | 215 |
| 216 /// Merges [stream1] and [stream2] into a single stream that emits events from | 216 /// Merges [stream1] and [stream2] into a single stream that emits events from |
| 217 /// both sources. | 217 /// both sources. |
| 218 Stream mergeStreams(Stream stream1, Stream stream2) { | 218 Stream mergeStreams(Stream stream1, Stream stream2) { |
| 219 var doneCount = 0; | 219 var doneCount = 0; |
| 220 var controller = new StreamController(); | 220 var controller = new StreamController(sync: true); |
| 221 | 221 |
| 222 for (var stream in [stream1, stream2]) { | 222 for (var stream in [stream1, stream2]) { |
| 223 stream.listen((value) { | 223 stream.listen((value) { |
| 224 controller.add(value); | 224 controller.add(value); |
| 225 }, onError: (error) { | 225 }, onError: (error) { |
| 226 controller.addError(error); | 226 controller.addError(error); |
| 227 }, onDone: () { | 227 }, onDone: () { |
| 228 doneCount++; | 228 doneCount++; |
| 229 if (doneCount == 2) controller.close(); | 229 if (doneCount == 2) controller.close(); |
| 230 }); | 230 }); |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 error is FileIOException || | 538 error is FileIOException || |
| 539 error is HttpException || | 539 error is HttpException || |
| 540 error is HttpParserException || | 540 error is HttpParserException || |
| 541 error is LinkIOException || | 541 error is LinkIOException || |
| 542 error is MimeMultipartException || | 542 error is MimeMultipartException || |
| 543 error is OSError || | 543 error is OSError || |
| 544 error is ProcessException || | 544 error is ProcessException || |
| 545 error is SocketIOException || | 545 error is SocketIOException || |
| 546 error is WebSocketException; | 546 error is WebSocketException; |
| 547 } | 547 } |
| OLD | NEW |