| 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 /// Capture asynchronous results into synchronous values. | 5 /// Capture asynchronous results into synchronous values. |
| 6 /// | 6 /// |
| 7 /// Capturing a result (either a returned value or a thrown error) | 7 /// Capturing a result (either a returned value or a thrown error) |
| 8 /// means converting it into a [Result] - | 8 /// means converting it into a [Result] - |
| 9 /// either a [ValueResult] or an [ErrorResult]. | 9 /// either a [ValueResult] or an [ErrorResult]. |
| 10 /// | 10 /// |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 /// Complete a completer with this result. | 122 /// Complete a completer with this result. |
| 123 void complete(Completer<T> completer); | 123 void complete(Completer<T> completer); |
| 124 | 124 |
| 125 /// Add this result to an [EventSink]. | 125 /// Add this result to an [EventSink]. |
| 126 /// | 126 /// |
| 127 /// Calls the sink's `add` or `addError` method as appropriate. | 127 /// Calls the sink's `add` or `addError` method as appropriate. |
| 128 void addTo(EventSink<T> sink); | 128 void addTo(EventSink<T> sink); |
| 129 | 129 |
| 130 /// Creates a future completed with this result as a value or an error. | 130 /// Creates a future completed with this result as a value or an error. |
| 131 Future<T> get asFuture; | 131 Future<T> get asFuture; |
| 132 |
| 133 /// This result is equal to another result with the same value or error. |
| 134 /// |
| 135 /// If this result is a value result, it is equal to another value result |
| 136 /// with an equal value. |
| 137 /// |
| 138 /// If this result is an error result, it is equal to another error result |
| 139 /// with equal error object and stack trace. |
| 140 /// Notice that error objects and stack traces rarely override equality. |
| 141 bool operator==(Object other); |
| 142 |
| 143 /// A hash code compatible with [operator==]. |
| 144 int get hashCode; |
| 132 } | 145 } |
| 133 | 146 |
| 134 /// A result representing a returned value. | 147 /// A result representing a returned value. |
| 135 class ValueResult<T> implements Result<T> { | 148 class ValueResult<T> implements Result<T> { |
| 136 final T value; | 149 final T value; |
| 137 ValueResult(this.value); | 150 ValueResult(this.value); |
| 138 bool get isValue => true; | 151 bool get isValue => true; |
| 139 bool get isError => false; | 152 bool get isError => false; |
| 140 ValueResult<T> get asValue => this; | 153 ValueResult<T> get asValue => this; |
| 141 ErrorResult get asError => null; | 154 ErrorResult get asError => null; |
| 142 void complete(Completer<T> completer) { | 155 void complete(Completer<T> completer) { |
| 143 completer.complete(value); | 156 completer.complete(value); |
| 144 } | 157 } |
| 145 void addTo(EventSink<T> sink) { | 158 void addTo(EventSink<T> sink) { |
| 146 sink.add(value); | 159 sink.add(value); |
| 147 } | 160 } |
| 148 Future<T> get asFuture => new Future.value(value); | 161 Future<T> get asFuture => new Future.value(value); |
| 162 |
| 163 bool operator==(Object other) { |
| 164 if (identical(this, other)) return true; |
| 165 if (other is ValueResult) { |
| 166 return value == other.value; |
| 167 } |
| 168 return false; |
| 169 } |
| 170 |
| 171 int get hashCode => value.hashCode; |
| 172 |
| 173 String toString() => "Value($value)"; |
| 149 } | 174 } |
| 150 | 175 |
| 151 /// A result representing a thrown error. | 176 /// A result representing a thrown error. |
| 152 class ErrorResult implements Result { | 177 class ErrorResult implements Result { |
| 153 final error; | 178 final error; |
| 154 final StackTrace stackTrace; | 179 final StackTrace stackTrace; |
| 155 ErrorResult(this.error, this.stackTrace); | 180 ErrorResult(this.error, this.stackTrace); |
| 156 bool get isValue => false; | 181 bool get isValue => false; |
| 157 bool get isError => true; | 182 bool get isError => true; |
| 158 ValueResult get asValue => null; | 183 ValueResult get asValue => null; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 171 /// arguments, which will be called with the error and the stack trace, | 196 /// arguments, which will be called with the error and the stack trace, |
| 172 /// or it has to be a function expecting only one argument, | 197 /// or it has to be a function expecting only one argument, |
| 173 /// which will be called with only the error. | 198 /// which will be called with only the error. |
| 174 void handle(Function errorHandler) { | 199 void handle(Function errorHandler) { |
| 175 if (errorHandler is ZoneBinaryCallback) { | 200 if (errorHandler is ZoneBinaryCallback) { |
| 176 errorHandler(error, stackTrace); | 201 errorHandler(error, stackTrace); |
| 177 } else { | 202 } else { |
| 178 errorHandler(error); | 203 errorHandler(error); |
| 179 } | 204 } |
| 180 } | 205 } |
| 206 |
| 207 bool operator==(Object other) { |
| 208 if (identical(this, other)) return true; |
| 209 if (other is ErrorResult) { |
| 210 return error == other.error && stackTrace == other.stackTrace; |
| 211 } |
| 212 return false; |
| 213 } |
| 214 |
| 215 int get hashCode => |
| 216 ((error.hashCode * 31) + stackTrace.hashCode) & 0x3FFFFFFF; |
| 217 |
| 218 String toString() => "Error($error${stackTrace == null ? "" : ", ..."})"; |
| 181 } | 219 } |
| 182 | 220 |
| 183 /// A stream transformer that captures a stream of events into [Result]s. | 221 /// A stream transformer that captures a stream of events into [Result]s. |
| 184 /// | 222 /// |
| 185 /// The result of the transformation is a stream of [Result] values and | 223 /// The result of the transformation is a stream of [Result] values and |
| 186 /// no error events. | 224 /// no error events. |
| 187 class CaptureStreamTransformer<T> implements StreamTransformer<T, Result<T>> { | 225 class CaptureStreamTransformer<T> implements StreamTransformer<T, Result<T>> { |
| 188 const CaptureStreamTransformer(); | 226 const CaptureStreamTransformer(); |
| 189 | 227 |
| 190 Stream<Result<T>> bind(Stream<T> source) { | 228 Stream<Result<T>> bind(Stream<T> source) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 } | 285 } |
| 248 } | 286 } |
| 249 void addError(Object error, [StackTrace stackTrace]) { | 287 void addError(Object error, [StackTrace stackTrace]) { |
| 250 // Errors may be added by intermediate processing, even if it is never | 288 // Errors may be added by intermediate processing, even if it is never |
| 251 // added by CaptureSink. | 289 // added by CaptureSink. |
| 252 _sink.addError(error, stackTrace); | 290 _sink.addError(error, stackTrace); |
| 253 } | 291 } |
| 254 | 292 |
| 255 void close() { _sink.close(); } | 293 void close() { _sink.close(); } |
| 256 } | 294 } |
| OLD | NEW |