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