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 /** | 5 /** |
6 * Capture asynchronous results into synchronous values, and release them again. | 6 * Capture asynchronous results into synchronous values, and release them again. |
7 * | 7 * |
8 * Capturing a result (either a returned value or a thrown error) | 8 * Capturing a result (either a returned value or a thrown error) |
9 * means converting it into a [Result] - | 9 * means converting it into a [Result] - |
10 * either a [ValueResult] or an [ErrorResult]. | 10 * either a [ValueResult] or an [ErrorResult]. |
11 * | 11 * |
12 * This value can release itself by writing itself either to a | 12 * This value can release itself by writing itself either to a |
13 * [EventSink] or a [Completer], or by becoming a [Future]. | 13 * [EventSink] or a [Completer], or by becoming a [Future]. |
14 */ | 14 */ |
15 library dart.pkg.async.results; | 15 library dart.pkg.async.results; |
16 | 16 |
17 import "dart:async"; | 17 import "dart:async"; |
18 | 18 |
19 /** | 19 /** |
20 * The result of a computation. | 20 * The result of a computation. |
21 */ | 21 */ |
22 abstract class Result<T> { | 22 abstract class Result<T> { |
23 /** | 23 /** |
24 * Create a `Result` with the result of calling [computation]. | 24 * Create a `Result` with the result of calling [computation]. |
25 * | 25 * |
26 * This generates either a [ValueResult] with the value returned by | 26 * This generates either a [ValueResult] with the value returned by |
27 * calling `computation`, or an [ErrorResult] with an error thrown by | 27 * calling `computation`, or an [ErrorResult] with an error thrown by |
28 * the call. | 28 * the call. |
29 */ | 29 */ |
30 Result(T computation()) { | 30 factory Result(T computation()) { |
31 try { | 31 try { |
32 return new ValueResult(computation()); | 32 return new ValueResult(computation()); |
33 } catch (e, s) { | 33 } catch (e, s) { |
34 return new ErrorResult(e, s); | 34 return new ErrorResult(e, s); |
35 } | 35 } |
36 } | 36 } |
37 | 37 |
38 /** | 38 /** |
39 * Create a `Result` holding a value. | 39 * Create a `Result` holding a value. |
40 * | 40 * |
(...skipping 23 matching lines...) Expand all Loading... |
64 * The resulting future will never have an error. | 64 * The resulting future will never have an error. |
65 * Errors have been converted to an [ErrorResult] value. | 65 * Errors have been converted to an [ErrorResult] value. |
66 */ | 66 */ |
67 static Future<Result> capture(Future future) { | 67 static Future<Result> capture(Future future) { |
68 return future.then(_captureValue, onError: _captureError); | 68 return future.then(_captureValue, onError: _captureError); |
69 } | 69 } |
70 | 70 |
71 /** | 71 /** |
72 * Release the result of a captured future. | 72 * Release the result of a captured future. |
73 * | 73 * |
74 * Converts the [Result] value of the given [future] to a result or error | 74 * Converts the [Result] value of the given [future] to a value or error |
75 * completion of the returned future. | 75 * completion of the returned future. |
76 * | 76 * |
77 * If [future] completes with an error, the returned future completes with | 77 * If [future] completes with an error, the returned future completes with |
78 * the same error. | 78 * the same error. |
79 */ | 79 */ |
80 static Future release(Future<Result> future) { | 80 static Future release(Future<Result> future) { |
81 return future.then(_release); | 81 return future.then(_release); |
82 } | 82 } |
83 | 83 |
84 /** | 84 /** |
(...skipping 15 matching lines...) Expand all Loading... |
100 * the retuned stream as appropriate. | 100 * the retuned stream as appropriate. |
101 * Errors from the source stream become errors in the returned stream. | 101 * Errors from the source stream become errors in the returned stream. |
102 * | 102 * |
103 * Shorthand for transforming the stream using [ReleaseStreamTransformer]. | 103 * Shorthand for transforming the stream using [ReleaseStreamTransformer]. |
104 */ | 104 */ |
105 static Stream releaseStream(Stream<Result> source) { | 105 static Stream releaseStream(Stream<Result> source) { |
106 return source.transform(const ReleaseStreamTransformer()); | 106 return source.transform(const ReleaseStreamTransformer()); |
107 } | 107 } |
108 | 108 |
109 /** | 109 /** |
| 110 * Converts a result of a result to a single result. |
| 111 * |
| 112 * If the result is an error, or it is a `Result` value |
| 113 * which is then an error, then a result with that error is returned. |
| 114 * Otherwise both levels of results are value results, and a single |
| 115 * result with the value is returned. |
| 116 */ |
| 117 static Result flatten(Result<Result> result) { |
| 118 if (result.isError) return result; |
| 119 return result.asValue.value; |
| 120 } |
| 121 |
| 122 /** |
110 * Whether this result is a value result. | 123 * Whether this result is a value result. |
111 * | 124 * |
112 * Always the opposite of [isError]. | 125 * Always the opposite of [isError]. |
113 */ | 126 */ |
114 bool get isValue; | 127 bool get isValue; |
115 | 128 |
116 /** | 129 /** |
117 * Whether this result is an error result. | 130 * Whether this result is an error result. |
118 * | 131 * |
119 * Always the opposite of [isValue]. | 132 * Always the opposite of [isValue]. |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 } | 282 } |
270 } | 283 } |
271 void addError(Object error, StackTrace stackTrace) { | 284 void addError(Object error, StackTrace stackTrace) { |
272 // Errors may be added by intermediate processing, even if it is never | 285 // Errors may be added by intermediate processing, even if it is never |
273 // added by CaptureSink. | 286 // added by CaptureSink. |
274 _sink.addError(error, stackTrace); | 287 _sink.addError(error, stackTrace); |
275 } | 288 } |
276 | 289 |
277 void close() { _sink.close(); } | 290 void close() { _sink.close(); } |
278 } | 291 } |
OLD | NEW |