Index: dart/sdk/lib/_internal/pub/asset/dart/serialize.dart |
diff --git a/dart/sdk/lib/_internal/pub/asset/dart/serialize.dart b/dart/sdk/lib/_internal/pub/asset/dart/serialize.dart |
index 43328e141be23978bfacfb1d36d320ec860f8992..5e3237818c7ff255a8d14c664885d514900e6b67 100644 |
--- a/dart/sdk/lib/_internal/pub/asset/dart/serialize.dart |
+++ b/dart/sdk/lib/_internal/pub/asset/dart/serialize.dart |
@@ -11,7 +11,9 @@ import 'package:barback/barback.dart'; |
import 'package:source_maps/span.dart'; |
import 'serialize/exception.dart'; |
+import 'utils.dart'; |
+export 'serialize/aggregate_transform.dart'; |
export 'serialize/exception.dart'; |
export 'serialize/transform.dart'; |
export 'serialize/transformer.dart'; |
@@ -61,6 +63,60 @@ Location deserializeLocation(Map location) { |
location['line'], location['column']); |
} |
+/// Converts [stream] into a serializable map. |
+/// |
+/// [serializeEvent] is used to serialize each event from the stream. |
+Map serializeStream(Stream stream, serializeEvent(event)) { |
+ var receivePort = new ReceivePort(); |
+ var map = {'replyTo': receivePort.sendPort}; |
+ |
+ receivePort.first.then((message) { |
+ var sendPort = message['replyTo']; |
+ stream.listen((event) { |
+ sendPort.send({ |
+ 'type': 'event', |
+ 'value': serializeEvent(event) |
+ }); |
+ }, onError: (error, stackTrace) { |
+ sendPort.send({ |
+ 'type': 'error', |
+ 'error': serializeException(error, stackTrace) |
+ }); |
+ }, onDone: () => sendPort.send({'type': 'done'})); |
+ }); |
+ |
+ return map; |
+} |
+ |
+/// Converts a serializable map into a [Stream]. |
+/// |
+/// [deserializeEvent] is used to deserialize each event from the stream. |
+Stream deserializeStream(Map stream, deserializeEvent(event)) { |
+ return callbackStream(() { |
+ var receivePort = new ReceivePort(); |
+ stream['replyTo'].send({'replyTo': receivePort.sendPort}); |
+ |
+ var controller = new StreamController(sync: true); |
+ receivePort.listen((event) { |
+ switch (event['type']) { |
+ case 'event': |
+ controller.add(deserializeEvent(event['value'])); |
+ break; |
+ case 'error': |
+ var exception = deserializeException(event['error']); |
+ controller.addError(exception, exception.stackTrace); |
+ break; |
+ case 'done': |
+ controller.close(); |
+ receivePort.close(); |
+ break; |
+ } |
+ }); |
+ |
+ return controller.stream; |
+ }); |
+} |
+ |
/// Wraps [message] and sends it across [port], then waits for a response which |
/// should be sent using [respond]. |
/// |