OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library json_rpc_2.utils; | 5 library json_rpc_2.utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 /// [Object.toString] values contain. | 37 /// [Object.toString] values contain. |
38 final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); | 38 final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); |
39 | 39 |
40 /// Get a string description of an exception. | 40 /// Get a string description of an exception. |
41 /// | 41 /// |
42 /// Many exceptions include the exception class name at the beginning of their | 42 /// Many exceptions include the exception class name at the beginning of their |
43 /// [toString], so we remove that if it exists. | 43 /// [toString], so we remove that if it exists. |
44 String getErrorMessage(error) => | 44 String getErrorMessage(error) => |
45 error.toString().replaceFirst(_exceptionPrefix, ''); | 45 error.toString().replaceFirst(_exceptionPrefix, ''); |
46 | 46 |
| 47 /// Like `try`/`finally`, run [body] and ensure that [whenComplete] runs |
| 48 /// afterwards, regardless of whether [body] succeeded. |
| 49 /// |
| 50 /// This is synchronicity-agnostic relative to [body]. If [body] returns a |
| 51 /// [Future], this wil run asynchronously; otherwise it will run synchronously. |
| 52 tryFinally(body(), whenComplete()) { |
| 53 var result; |
| 54 try { |
| 55 result = body(); |
| 56 } catch (_) { |
| 57 whenComplete(); |
| 58 rethrow; |
| 59 } |
| 60 |
| 61 if (result is! Future) { |
| 62 whenComplete(); |
| 63 return result; |
| 64 } else { |
| 65 return result.whenComplete(whenComplete); |
| 66 } |
| 67 } |
| 68 |
47 /// Returns a [StreamSink] that wraps [sink] and maps each event added using | 69 /// Returns a [StreamSink] that wraps [sink] and maps each event added using |
48 /// [callback]. | 70 /// [callback]. |
49 StreamSink mapStreamSink(StreamSink sink, callback(event)) => | 71 StreamSink mapStreamSink(StreamSink sink, callback(event)) => |
50 new _MappedStreamSink(sink, callback); | 72 new _MappedStreamSink(sink, callback); |
51 | 73 |
52 /// A [StreamSink] wrapper that maps each event added to the sink. | 74 /// A [StreamSink] wrapper that maps each event added to the sink. |
53 class _MappedStreamSink implements StreamSink { | 75 class _MappedStreamSink implements StreamSink { |
54 final StreamSink _inner; | 76 final StreamSink _inner; |
55 final Function _callback; | 77 final Function _callback; |
56 | 78 |
57 Future get done => _inner.done; | 79 Future get done => _inner.done; |
58 | 80 |
59 _MappedStreamSink(this._inner, this._callback); | 81 _MappedStreamSink(this._inner, this._callback); |
60 | 82 |
61 void add(event) => _inner.add(_callback(event)); | 83 void add(event) => _inner.add(_callback(event)); |
62 void addError(error, [StackTrace stackTrace]) => | 84 void addError(error, [StackTrace stackTrace]) => |
63 _inner.addError(error, stackTrace); | 85 _inner.addError(error, stackTrace); |
64 Future addStream(Stream stream) => _inner.addStream(stream.map(_callback)); | 86 Future addStream(Stream stream) => _inner.addStream(stream.map(_callback)); |
65 Future close() => _inner.close(); | 87 Future close() => _inner.close(); |
66 } | 88 } |
OLD | NEW |