| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 | 8 |
| 9 /// A pair of values. | 9 /// A pair of values. |
| 10 class Pair<E, F> { | 10 class Pair<E, F> { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 controller.addError(e); | 85 controller.addError(e); |
| 86 controller.close(); | 86 controller.close(); |
| 87 }); | 87 }); |
| 88 return controller.stream; | 88 return controller.stream; |
| 89 } | 89 } |
| 90 | 90 |
| 91 // TODO(nweiz): remove this when issue 7964 is fixed. | 91 // TODO(nweiz): remove this when issue 7964 is fixed. |
| 92 /// Returns a [Future] that will complete to the first element of [stream]. | 92 /// Returns a [Future] that will complete to the first element of [stream]. |
| 93 /// Unlike [Stream.first], this is safe to use with single-subscription streams. | 93 /// Unlike [Stream.first], this is safe to use with single-subscription streams. |
| 94 Future streamFirst(Stream stream) { | 94 Future streamFirst(Stream stream) { |
| 95 // TODO(nweiz): remove this when issue 8512 is fixed. | 95 var stackTrace; |
| 96 var cancelled = false; | 96 try { |
| 97 throw ''; |
| 98 } catch (_, thrownStackTrace) { |
| 99 stackTrace = thrownStackTrace; |
| 100 } |
| 101 |
| 97 var completer = new Completer(); | 102 var completer = new Completer(); |
| 98 var subscription; | 103 var subscription; |
| 99 subscription = stream.listen((value) { | 104 subscription = stream.listen((value) { |
| 100 if (!cancelled) { | 105 subscription.cancel(); |
| 101 cancelled = true; | 106 completer.complete(value); |
| 102 subscription.cancel(); | |
| 103 completer.complete(value); | |
| 104 } | |
| 105 }, onError: (e) { | 107 }, onError: (e) { |
| 106 if (!cancelled) { | 108 completer.completeError(e.error, e.stackTrace); |
| 107 completer.completeError(e.error, e.stackTrace); | |
| 108 } | |
| 109 }, onDone: () { | 109 }, onDone: () { |
| 110 if (!cancelled) { | 110 completer.completeError(new StateError("No elements"), stackTrace); |
| 111 completer.completeError(new StateError("No elements")); | |
| 112 } | |
| 113 }, unsubscribeOnError: true); | 111 }, unsubscribeOnError: true); |
| 114 return completer.future; | 112 return completer.future; |
| 115 } | 113 } |
| 116 | 114 |
| 117 /// A function that can be called to cancel a [Stream] and send a done message. | 115 /// A function that can be called to cancel a [Stream] and send a done message. |
| 118 typedef void StreamCanceller(); | 116 typedef void StreamCanceller(); |
| 119 | 117 |
| 120 // TODO(nweiz): use a StreamSubscription when issue 9026 is fixed. | 118 // TODO(nweiz): use a StreamSubscription when issue 9026 is fixed. |
| 121 /// Returns a wrapped version of [stream] along with a function that will cancel | 119 /// Returns a wrapped version of [stream] along with a function that will cancel |
| 122 /// the wrapped stream. Unlike [StreamSubscription], this canceller will send a | 120 /// the wrapped stream. Unlike [StreamSubscription], this canceller will send a |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 return map; | 175 return map; |
| 178 }); | 176 }); |
| 179 } | 177 } |
| 180 | 178 |
| 181 /// Returns whether [pattern] matches all of [string]. | 179 /// Returns whether [pattern] matches all of [string]. |
| 182 bool fullMatch(String string, Pattern pattern) { | 180 bool fullMatch(String string, Pattern pattern) { |
| 183 var matches = pattern.allMatches(string); | 181 var matches = pattern.allMatches(string); |
| 184 if (matches.isEmpty) return false; | 182 if (matches.isEmpty) return false; |
| 185 return matches.first.start == 0 && matches.first.end == string.length; | 183 return matches.first.start == 0 && matches.first.end == string.length; |
| 186 } | 184 } |
| OLD | NEW |