Index: pkg/json_rpc_2/lib/src/utils.dart |
diff --git a/pkg/json_rpc_2/lib/src/utils.dart b/pkg/json_rpc_2/lib/src/utils.dart |
index a212f58d8e2d8620893ae9ccf645c677e59814da..f862cb78447ba2b9eff86fc239bd48ec29ef42c4 100644 |
--- a/pkg/json_rpc_2/lib/src/utils.dart |
+++ b/pkg/json_rpc_2/lib/src/utils.dart |
@@ -44,6 +44,28 @@ final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); |
String getErrorMessage(error) => |
error.toString().replaceFirst(_exceptionPrefix, ''); |
+/// Like `try`/`finally`, run [body] and ensure that [whenComplete] runs |
+/// afterwards, regardless of whether [body] succeeded. |
+/// |
+/// This is synchronicity-agnostic relative to [body]. If [body] returns a |
+/// [Future], this wil run asynchronously; otherwise it will run synchronously. |
+tryFinally(body(), whenComplete()) { |
+ var result; |
+ try { |
+ result = body(); |
+ } catch (_) { |
+ whenComplete(); |
+ rethrow; |
+ } |
+ |
+ if (result is! Future) { |
+ whenComplete(); |
+ return result; |
+ } else { |
+ return result.whenComplete(whenComplete); |
+ } |
+} |
+ |
/// Returns a [StreamSink] that wraps [sink] and maps each event added using |
/// [callback]. |
StreamSink mapStreamSink(StreamSink sink, callback(event)) => |