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 25 matching lines...) Expand all Loading... |
36 /// A regular expression to match the exception prefix that some exceptions' | 36 /// A regular expression to match the exception prefix that some exceptions' |
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 |
| 47 /// Returns a [StreamSink] that wraps [sink] and maps each event added using |
| 48 /// [callback]. |
| 49 StreamSink mapStreamSink(StreamSink sink, callback(event)) => |
| 50 new _MappedStreamSink(sink, callback); |
| 51 |
| 52 /// A [StreamSink] wrapper that maps each event added to the sink. |
| 53 class _MappedStreamSink implements StreamSink { |
| 54 final StreamSink _inner; |
| 55 final Function _callback; |
| 56 |
| 57 Future get done => _inner.done; |
| 58 |
| 59 _MappedStreamSink(this._inner, this._callback); |
| 60 |
| 61 void add(event) => _inner.add(_callback(event)); |
| 62 void addError(error, [StackTrace stackTrace]) => |
| 63 _inner.addError(error, stackTrace); |
| 64 Future addStream(Stream stream) => _inner.addStream(stream.map(_callback)); |
| 65 Future close() => _inner.close(); |
| 66 } |
OLD | NEW |