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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 /// Capture the result of a future into a `Result` future. | 75 /// Capture the result of a future into a `Result` future. |
76 /// | 76 /// |
77 /// The resulting future will never have an error. | 77 /// The resulting future will never have an error. |
78 /// Errors have been converted to an [ErrorResult] value. | 78 /// Errors have been converted to an [ErrorResult] value. |
79 static Future<Result/*<T>*/> capture/*<T>*/(Future/*<T>*/ future) { | 79 static Future<Result<T>> capture<T>(Future<T> future) { |
80 return future.then((value) => new ValueResult(value), | 80 return future.then((value) => new ValueResult(value), |
81 onError: (error, stackTrace) => | 81 onError: (error, stackTrace) => |
82 new ErrorResult/*<T>*/(error, stackTrace)); | 82 new ErrorResult<T>(error, stackTrace)); |
83 } | 83 } |
84 | 84 |
85 /// Release the result of a captured future. | 85 /// Release the result of a captured future. |
86 /// | 86 /// |
87 /// 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 |
88 /// completion of the returned future. | 88 /// completion of the returned future. |
89 /// | 89 /// |
90 /// If [future] completes with an error, the returned future completes with | 90 /// If [future] completes with an error, the returned future completes with |
91 /// the same error. | 91 /// the same error. |
92 static Future/*<T>*/ release/*<T>*/(Future<Result/*<T>*/> future) => | 92 static Future<T> release<T>(Future<Result<T>> future) => |
93 future.then/*<Future<T>>*/((result) => result.asFuture); | 93 future.then<Future<T>>((result) => result.asFuture); |
94 | 94 |
95 /// 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. |
96 /// | 96 /// |
97 /// The returned stream will not have any error events. | 97 /// The returned stream will not have any error events. |
98 /// Errors from the source stream have been converted to [ErrorResult]s. | 98 /// Errors from the source stream have been converted to [ErrorResult]s. |
99 static Stream<Result/*<T>*/> captureStream/*<T>*/(Stream/*<T>*/ source) => | 99 static Stream<Result<T>> captureStream<T>(Stream<T> source) => |
100 source.transform(new CaptureStreamTransformer/*<T>*/()); | 100 source.transform(new CaptureStreamTransformer<T>()); |
101 | 101 |
102 /// 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. |
103 /// | 103 /// |
104 /// `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 |
105 /// the returned stream as appropriate. | 105 /// the returned stream as appropriate. |
106 /// Errors from the source stream become errors in the returned stream. | 106 /// Errors from the source stream become errors in the returned stream. |
107 static Stream/*<T>*/ releaseStream/*<T>*/(Stream<Result/*<T>*/> source) => | 107 static Stream<T> releaseStream<T>(Stream<Result<T>> source) => |
108 source.transform(new ReleaseStreamTransformer/*<T>*/()); | 108 source.transform(new ReleaseStreamTransformer<T>()); |
109 | 109 |
110 /// Converts a result of a result to a single result. | 110 /// Converts a result of a result to a single result. |
111 /// | 111 /// |
112 /// 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 |
113 /// 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. |
114 /// Otherwise both levels of results are value results, and a single | 114 /// Otherwise both levels of results are value results, and a single |
115 /// result with the value is returned. | 115 /// result with the value is returned. |
116 static Result/*<T>*/ flatten/*<T>*/(Result<Result/*<T>*/> result) { | 116 static Result<T> flatten<T>(Result<Result<T>> result) { |
117 if (result.isValue) return result.asValue.value; | 117 if (result.isValue) return result.asValue.value; |
118 return new ErrorResult/*<T>*/( | 118 return new ErrorResult<T>( |
119 result.asError.error, result.asError.stackTrace); | 119 result.asError.error, result.asError.stackTrace); |
120 } | 120 } |
121 | 121 |
122 /// Whether this result is a value result. | 122 /// Whether this result is a value result. |
123 /// | 123 /// |
124 /// Always the opposite of [isError]. | 124 /// Always the opposite of [isError]. |
125 bool get isValue; | 125 bool get isValue; |
126 | 126 |
127 /// Whether this result is an error result. | 127 /// Whether this result is an error result. |
128 /// | 128 /// |
(...skipping 14 matching lines...) Expand all Loading... |
143 void complete(Completer<T> completer); | 143 void complete(Completer<T> completer); |
144 | 144 |
145 /// Add this result to an [EventSink]. | 145 /// Add this result to an [EventSink]. |
146 /// | 146 /// |
147 /// Calls the sink's `add` or `addError` method as appropriate. | 147 /// Calls the sink's `add` or `addError` method as appropriate. |
148 void addTo(EventSink<T> sink); | 148 void addTo(EventSink<T> sink); |
149 | 149 |
150 /// 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. |
151 Future<T> get asFuture; | 151 Future<T> get asFuture; |
152 } | 152 } |
OLD | NEW |