Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:async'; | |
| 6 | |
| 7 import 'result/capture_transformer.dart'; | |
| 8 import 'result/error.dart'; | |
| 9 import 'result/release_transformer.dart'; | |
| 10 import 'result/value.dart'; | |
| 11 import 'stream_sink_transformer.dart'; | |
| 12 | |
| 13 /// The result of a computation. | |
| 14 /// | |
| 15 /// Capturing a result (either a returned value or a thrown error) means | |
| 16 /// converting it into a [Result] - either a [ValueResult] or an [ErrorResult]. | |
| 17 /// | |
| 18 /// This value can release itself by writing itself either to a [EventSink] or a | |
| 19 /// [Completer], or by becoming a [Future]. | |
| 20 abstract class Result<T> { | |
| 21 /// A stream transformer that captures a stream of events into [Result]s. | |
| 22 /// | |
| 23 /// The result of the transformation is a stream of [Result] values and no | |
| 24 /// error events. | |
|
Lasse Reichstein Nielsen
2016/03/08 10:40:37
You can refer to `captureStream` here. This is the
nweiz
2016/03/08 20:23:40
Done.
| |
| 25 static const StreamTransformer<Object, Result> captureStreamTransformer = | |
| 26 const CaptureStreamTransformer(); | |
| 27 | |
| 28 /// A stream transformer that releases a stream of result events. | |
| 29 /// | |
| 30 /// The result of the transformation is a stream of values and error events. | |
| 31 static const StreamTransformer<Object, Result> releaseStreamTransformer = | |
| 32 const ReleaseStreamTransformer(); | |
| 33 | |
| 34 /// A sink transformer that captures events into [Result]s. | |
| 35 /// | |
| 36 /// The result of the transformation is a sink that only forwards [Result] | |
| 37 /// values and no error events. | |
| 38 static const StreamSinkTransformer<Object, Result> captureSinkTransformer = | |
| 39 const StreamSinkTransformer.fromStreamTransformer( | |
| 40 const CaptureStreamTransformer()); | |
| 41 | |
| 42 /// A sink transformer that releases result events. | |
| 43 /// | |
| 44 /// The result of the transformation is a sink that forwards of values and | |
| 45 /// error events. | |
| 46 static const StreamSinkTransformer<Object, Result> releaseSinkTransformer = | |
| 47 const StreamSinkTransformer.fromStreamTransformer( | |
| 48 const ReleaseStreamTransformer()); | |
| 49 | |
| 50 /// Create a `Result` with the result of calling [computation]. | |
| 51 /// | |
| 52 /// This generates either a [ValueResult] with the value returned by | |
| 53 /// calling `computation`, or an [ErrorResult] with an error thrown by | |
| 54 /// the call. | |
| 55 factory Result(T computation()) { | |
| 56 try { | |
| 57 return new ValueResult(computation()); | |
| 58 } catch (e, s) { | |
| 59 return new ErrorResult(e, s); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 /// Create a `Result` holding a value. | |
| 64 /// | |
| 65 /// Alias for [ValueResult.ValueResult]. | |
| 66 factory Result.value(T value) = ValueResult<T>; | |
| 67 | |
| 68 /// Create a `Result` holding an error. | |
| 69 /// | |
| 70 /// Alias for [ErrorResult.ErrorResult]. | |
| 71 factory Result.error(Object error, [StackTrace stackTrace]) => | |
| 72 new ErrorResult(error, stackTrace); | |
| 73 | |
| 74 // Helper functions. | |
| 75 static _captureValue(value) => new ValueResult(value); | |
|
Lasse Reichstein Nielsen
2016/03/08 10:40:37
Can we use `ValueResult#` instead of `_captureValu
nweiz
2016/03/08 20:23:40
I believe it's not implemented anywhere but the VM
| |
| 76 static _captureError(error, stack) => new ErrorResult(error, stack); | |
| 77 static _release(Result v) { | |
| 78 if (v.isValue) return v.asValue.value; // Avoid wrapping in future. | |
| 79 return v.asFuture; | |
| 80 } | |
| 81 | |
| 82 /// Capture the result of a future into a `Result` future. | |
| 83 /// | |
| 84 /// The resulting future will never have an error. | |
| 85 /// Errors have been converted to an [ErrorResult] value. | |
| 86 static Future<Result> capture(Future future) { | |
| 87 return future.then(_captureValue, onError: _captureError); | |
| 88 } | |
| 89 | |
| 90 /// Release the result of a captured future. | |
| 91 /// | |
| 92 /// Converts the [Result] value of the given [future] to a value or error | |
| 93 /// completion of the returned future. | |
| 94 /// | |
| 95 /// If [future] completes with an error, the returned future completes with | |
| 96 /// the same error. | |
| 97 static Future release(Future<Result> future) { | |
| 98 return future.then(_release); | |
| 99 } | |
| 100 | |
| 101 /// Capture the results of a stream into a stream of [Result] values. | |
| 102 /// | |
| 103 /// The returned stream will not have any error events. | |
| 104 /// Errors from the source stream have been converted to [ErrorResult]s. | |
| 105 /// | |
| 106 /// Shorthand for transforming the stream using [CaptureStreamTransformer]. | |
| 107 static Stream<Result> captureStream(Stream source) { | |
| 108 return source.transform(const CaptureStreamTransformer()); | |
| 109 } | |
| 110 | |
| 111 /// Release a stream of [result] values into a stream of the results. | |
| 112 /// | |
| 113 /// `Result` values of the source stream become value or error events in | |
| 114 /// the returned stream as appropriate. | |
| 115 /// Errors from the source stream become errors in the returned stream. | |
| 116 /// | |
| 117 /// Shorthand for transforming the stream using [ReleaseStreamTransformer]. | |
| 118 static Stream releaseStream(Stream<Result> source) { | |
| 119 return source.transform(const ReleaseStreamTransformer()); | |
| 120 } | |
| 121 | |
| 122 /// Converts a result of a result to a single result. | |
| 123 /// | |
| 124 /// If the result is an error, or it is a `Result` value | |
| 125 /// which is then an error, then a result with that error is returned. | |
| 126 /// Otherwise both levels of results are value results, and a single | |
| 127 /// result with the value is returned. | |
| 128 static Result flatten(Result<Result> result) { | |
| 129 if (result.isError) return result; | |
| 130 return result.asValue.value; | |
| 131 } | |
| 132 | |
| 133 /// Whether this result is a value result. | |
| 134 /// | |
| 135 /// Always the opposite of [isError]. | |
| 136 bool get isValue; | |
| 137 | |
| 138 /// Whether this result is an error result. | |
| 139 /// | |
| 140 /// Always the opposite of [isValue]. | |
| 141 bool get isError; | |
| 142 | |
| 143 /// If this is a value result, return itself. | |
| 144 /// | |
| 145 /// Otherwise return `null`. | |
| 146 ValueResult<T> get asValue; | |
| 147 | |
| 148 /// If this is an error result, return itself. | |
| 149 /// | |
| 150 /// Otherwise return `null`. | |
| 151 ErrorResult get asError; | |
| 152 | |
| 153 /// Complete a completer with this result. | |
| 154 void complete(Completer<T> completer); | |
| 155 | |
| 156 /// Add this result to an [EventSink]. | |
| 157 /// | |
| 158 /// Calls the sink's `add` or `addError` method as appropriate. | |
| 159 void addTo(EventSink<T> sink); | |
| 160 | |
| 161 /// Creates a future completed with this result as a value or an error. | |
| 162 Future<T> get asFuture; | |
| 163 } | |
| OLD | NEW |