Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2378)

Unified Diff: pkg/json_rpc_2/lib/src/utils.dart

Issue 691053006: Add a Client class to json_rpc_2. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/json_rpc_2/lib/src/server.dart ('k') | pkg/json_rpc_2/pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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)) =>
« no previous file with comments | « pkg/json_rpc_2/lib/src/server.dart ('k') | pkg/json_rpc_2/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698