OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'result/capture_transformer.dart'; | 7 import 'result/capture_transformer.dart'; |
8 import 'result/error.dart'; | 8 import 'result/error.dart'; |
9 import 'result/release_transformer.dart'; | 9 import 'result/release_transformer.dart'; |
10 import 'result/value.dart'; | 10 import 'result/value.dart'; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 /// | 65 /// |
66 /// Alias for [ValueResult.ValueResult]. | 66 /// Alias for [ValueResult.ValueResult]. |
67 factory Result.value(T value) = ValueResult<T>; | 67 factory Result.value(T value) = ValueResult<T>; |
68 | 68 |
69 /// Create a `Result` holding an error. | 69 /// Create a `Result` holding an error. |
70 /// | 70 /// |
71 /// Alias for [ErrorResult.ErrorResult]. | 71 /// Alias for [ErrorResult.ErrorResult]. |
72 factory Result.error(Object error, [StackTrace stackTrace]) => | 72 factory Result.error(Object error, [StackTrace stackTrace]) => |
73 new ErrorResult(error, stackTrace); | 73 new ErrorResult(error, stackTrace); |
74 | 74 |
75 // Helper functions. | |
76 static _captureValue(value) => new ValueResult(value); | |
77 static _captureError(error, stack) => new ErrorResult(error, stack); | |
78 static _release(Result v) { | |
79 if (v.isValue) return v.asValue.value; // Avoid wrapping in future. | |
80 return v.asFuture; | |
81 } | |
82 | |
83 /// Capture the result of a future into a `Result` future. | 75 /// Capture the result of a future into a `Result` future. |
84 /// | 76 /// |
85 /// The resulting future will never have an error. | 77 /// The resulting future will never have an error. |
86 /// Errors have been converted to an [ErrorResult] value. | 78 /// Errors have been converted to an [ErrorResult] value. |
87 static Future<Result> capture(Future future) { | 79 static Future<Result/*<T>*/> capture/*<T>*/(Future/*<T>*/ future) { |
88 return future.then(_captureValue, onError: _captureError); | 80 return future.then((value) => new ValueResult(value), |
| 81 onError: (error, stackTrace) => |
| 82 new ErrorResult/*<T>*/(error, stackTrace)); |
89 } | 83 } |
90 | 84 |
91 /// Release the result of a captured future. | 85 /// Release the result of a captured future. |
92 /// | 86 /// |
93 /// Converts the [Result] value of the given [future] to a value or error | 87 /// Converts the [Result] value of the given [future] to a value or error |
94 /// completion of the returned future. | 88 /// completion of the returned future. |
95 /// | 89 /// |
96 /// If [future] completes with an error, the returned future completes with | 90 /// If [future] completes with an error, the returned future completes with |
97 /// the same error. | 91 /// the same error. |
98 static Future release(Future<Result> future) { | 92 static Future/*<T>*/ release/*<T>*/(Future<Result/*<T>*/> future) => |
99 return future.then(_release); | 93 future.then/*<Future<T>>*/((result) => result.asFuture); |
100 } | |
101 | 94 |
102 /// Capture the results of a stream into a stream of [Result] values. | 95 /// Capture the results of a stream into a stream of [Result] values. |
103 /// | 96 /// |
104 /// The returned stream will not have any error events. | 97 /// The returned stream will not have any error events. |
105 /// Errors from the source stream have been converted to [ErrorResult]s. | 98 /// Errors from the source stream have been converted to [ErrorResult]s. |
106 /// | 99 static Stream<Result/*<T>*/> captureStream/*<T>*/(Stream/*<T>*/ source) => |
107 /// Shorthand for transforming the stream using [captureStreamTransformer]. | 100 source.transform(new CaptureStreamTransformer/*<T>*/()); |
108 static Stream<Result> captureStream(Stream source) { | |
109 return source.transform(captureStreamTransformer); | |
110 } | |
111 | 101 |
112 /// Release a stream of [result] values into a stream of the results. | 102 /// Release a stream of [result] values into a stream of the results. |
113 /// | 103 /// |
114 /// `Result` values of the source stream become value or error events in | 104 /// `Result` values of the source stream become value or error events in |
115 /// the returned stream as appropriate. | 105 /// the returned stream as appropriate. |
116 /// Errors from the source stream become errors in the returned stream. | 106 /// Errors from the source stream become errors in the returned stream. |
117 /// | 107 static Stream/*<T>*/ releaseStream/*<T>*/(Stream<Result/*<T>*/> source) => |
118 /// Shorthand for transforming the stream using [releaseStreamTransformer]. | 108 source.transform(new ReleaseStreamTransformer/*<T>*/()); |
119 static Stream releaseStream(Stream<Result> source) { | |
120 return source.transform(releaseStreamTransformer); | |
121 } | |
122 | 109 |
123 /// Converts a result of a result to a single result. | 110 /// Converts a result of a result to a single result. |
124 /// | 111 /// |
125 /// If the result is an error, or it is a `Result` value | 112 /// If the result is an error, or it is a `Result` value |
126 /// which is then an error, then a result with that error is returned. | 113 /// which is then an error, then a result with that error is returned. |
127 /// Otherwise both levels of results are value results, and a single | 114 /// Otherwise both levels of results are value results, and a single |
128 /// result with the value is returned. | 115 /// result with the value is returned. |
129 static Result flatten(Result<Result> result) { | 116 static Result/*<T>*/ flatten/*<T>*/(Result<Result/*<T>*/> result) { |
130 if (result.isError) return result; | 117 if (result.isValue) return result.asValue.value; |
131 return result.asValue.value; | 118 return new ErrorResult/*<T>*/( |
| 119 result.asError.error, result.asError.stackTrace); |
132 } | 120 } |
133 | 121 |
134 /// Whether this result is a value result. | 122 /// Whether this result is a value result. |
135 /// | 123 /// |
136 /// Always the opposite of [isError]. | 124 /// Always the opposite of [isError]. |
137 bool get isValue; | 125 bool get isValue; |
138 | 126 |
139 /// Whether this result is an error result. | 127 /// Whether this result is an error result. |
140 /// | 128 /// |
141 /// Always the opposite of [isValue]. | 129 /// Always the opposite of [isValue]. |
142 bool get isError; | 130 bool get isError; |
143 | 131 |
144 /// If this is a value result, return itself. | 132 /// If this is a value result, return itself. |
145 /// | 133 /// |
146 /// Otherwise return `null`. | 134 /// Otherwise return `null`. |
147 ValueResult<T> get asValue; | 135 ValueResult<T> get asValue; |
148 | 136 |
149 /// If this is an error result, return itself. | 137 /// If this is an error result, return itself. |
150 /// | 138 /// |
151 /// Otherwise return `null`. | 139 /// Otherwise return `null`. |
152 ErrorResult get asError; | 140 ErrorResult<T> get asError; |
153 | 141 |
154 /// Complete a completer with this result. | 142 /// Complete a completer with this result. |
155 void complete(Completer<T> completer); | 143 void complete(Completer<T> completer); |
156 | 144 |
157 /// Add this result to an [EventSink]. | 145 /// Add this result to an [EventSink]. |
158 /// | 146 /// |
159 /// Calls the sink's `add` or `addError` method as appropriate. | 147 /// Calls the sink's `add` or `addError` method as appropriate. |
160 void addTo(EventSink<T> sink); | 148 void addTo(EventSink<T> sink); |
161 | 149 |
162 /// Creates a future completed with this result as a value or an error. | 150 /// Creates a future completed with this result as a value or an error. |
163 Future<T> get asFuture; | 151 Future<T> get asFuture; |
164 } | 152 } |
OLD | NEW |