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

Unified 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 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 1eff004f1fb1407d31ac175e4843c99a73f338cd..a212f58d8e2d8620893ae9ccf645c677e59814da 100644
--- a/pkg/json_rpc_2/lib/src/utils.dart
+++ b/pkg/json_rpc_2/lib/src/utils.dart
@@ -43,3 +43,24 @@ final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): ');
/// [toString], so we remove that if it exists.
String getErrorMessage(error) =>
error.toString().replaceFirst(_exceptionPrefix, '');
+
+/// Returns a [StreamSink] that wraps [sink] and maps each event added using
+/// [callback].
+StreamSink mapStreamSink(StreamSink sink, callback(event)) =>
+ new _MappedStreamSink(sink, callback);
+
+/// A [StreamSink] wrapper that maps each event added to the sink.
+class _MappedStreamSink implements StreamSink {
+ final StreamSink _inner;
+ final Function _callback;
+
+ Future get done => _inner.done;
+
+ _MappedStreamSink(this._inner, this._callback);
+
+ void add(event) => _inner.add(_callback(event));
+ void addError(error, [StackTrace stackTrace]) =>
+ _inner.addError(error, stackTrace);
+ Future addStream(Stream stream) => _inner.addStream(stream.map(_callback));
+ Future close() => _inner.close();
+}
« 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