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

Side by Side Diff: pkg/json_rpc_2/lib/src/utils.dart

Issue 309503005: Convert json_rpc.Server to take a Stream and StreamSink. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 6 months 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 unified diff | Download patch | Annotate | Revision Log
« 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 }
OLDNEW
« 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