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

Unified Diff: pkg/barback/lib/src/serialize.dart

Issue 55983005: Serialize barback assets more efficiently. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 7 years, 1 month 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/barback/lib/src/internal_asset.dart ('k') | pkg/barback/lib/src/utils.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/barback/lib/src/serialize.dart
diff --git a/pkg/barback/lib/src/serialize.dart b/pkg/barback/lib/src/serialize.dart
new file mode 100644
index 0000000000000000000000000000000000000000..049afc9613743bf89b1b465e4953e6f49f4b8253
--- /dev/null
+++ b/pkg/barback/lib/src/serialize.dart
@@ -0,0 +1,69 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library barback.serialize;
+
+import 'dart:async';
+import 'dart:isolate';
+
+import 'asset_id.dart';
+import 'utils.dart';
+
+/// Converts [id] into a serializable map.
+Map serializeId(AssetId id) => {'package': id.package, 'path': id.path};
+
+/// Converts [stream] into a [SendPort] with which another isolate can request
+/// the data from [stream].
+SendPort serializeStream(Stream stream) {
+ var receivePort = new ReceivePort();
+ receivePort.first.then((sendPort) {
+ stream.listen((data) => sendPort.send({'type': 'data', 'data': data}),
+ onDone: () => sendPort.send({'type': 'done'}),
+ onError: (error, stackTrace) {
+ sendPort.send({
+ 'type': 'error',
+ 'error': CrossIsolateException.serialize(error, stackTrace)
+ });
+ });
+ });
+
+ return receivePort.sendPort;
+}
+
+/// Converts a serializable map into an [AssetId].
+AssetId deserializeId(Map id) => new AssetId(id['package'], id['path']);
+
+/// Convert a [SendPort] whose opposite is waiting to send us a stream into a
+/// [Stream].
+///
+/// No stream data will actually be sent across the isolate boundary until
+/// someone subscribes to the returned stream.
+Stream deserializeStream(SendPort sendPort) {
+ return callbackStream(() {
+ var receivePort = new ReceivePort();
+ sendPort.send(receivePort.sendPort);
+ return receivePort.transform(
+ const StreamTransformer(_deserializeTransformer));
+ });
+}
+
+/// The body of a [StreamTransformer] that deserializes the values in a stream
+/// sent by [serializeStream].
+StreamSubscription _deserializeTransformer(Stream input, bool cancelOnError) {
+ var subscription;
+ var transformed = input.transform(new StreamTransformer.fromHandlers(
+ handleData: (data, sink) {
+ if (data['type'] == 'data') {
+ sink.add(data['data']);
+ } else if (data['type'] == 'error') {
+ sink.addError(CrossIsolateException.deserialize(data['error']));
+ } else {
+ assert(data['type'] == 'done');
+ sink.close();
+ subscription.cancel();
+ }
+ }));
+ subscription = transformed.listen(null, cancelOnError: cancelOnError);
+ return subscription;
+}
« no previous file with comments | « pkg/barback/lib/src/internal_asset.dart ('k') | pkg/barback/lib/src/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698