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

Unified Diff: sdk/lib/_internal/pub/asset/dart/serialize/transform.dart

Issue 239853002: Support declaring and lazy transformers in pub. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 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
Index: sdk/lib/_internal/pub/asset/dart/serialize/transform.dart
diff --git a/sdk/lib/_internal/pub/asset/dart/serialize/transform.dart b/sdk/lib/_internal/pub/asset/dart/serialize/transform.dart
index cc5e0527df8dd4bb90ff51cf61e77ad7a77d4508..55b4f18fdf0a0aa9b4a79126b83071e03aaacf4d 100644
--- a/sdk/lib/_internal/pub/asset/dart/serialize/transform.dart
+++ b/sdk/lib/_internal/pub/asset/dart/serialize/transform.dart
@@ -15,20 +15,19 @@ import 'package:barback/src/internal_asset.dart';
import '../serialize.dart';
import '../utils.dart';
-/// Converts [transform] into a serializable map.
-Map serializeTransform(Transform transform) {
+/// Serialize the methods shared between [Transform] and [DeclaringTransform].
+///
+/// [additionalFields] contains additional serialized fields to add to the
+/// serialized transform. [handleMessage] handles additional methods that differ
+/// between the two. It should return a [Future] if it handles a method, or
+/// `null` if it doesn't.
+Map _serializeBaseTransform(transform, Map additionalFields,
+ Future handleMessage(message)) {
Bob Nystrom 2014/04/16 16:21:00 Since handleMessage is specifically for handling m
nweiz 2014/04/16 20:28:21 Done.
var receivePort = new ReceivePort();
receivePort.listen((wrappedMessage) {
respond(wrappedMessage, (message) {
- if (message['type'] == 'getInput') {
- return transform.getInput(deserializeId(message['id']))
- .then((asset) => serializeAsset(asset));
- }
-
- if (message['type'] == 'addOutput') {
- transform.addOutput(deserializeAsset(message['output']));
- return null;
- }
+ var handled = handleMessage(message);
+ if (handled != null) return handled;
if (message['type'] == 'consumePrimary') {
transform.consumePrimary();
@@ -56,30 +55,50 @@ Map serializeTransform(Transform transform) {
});
});
- return {
- 'port': receivePort.sendPort,
+ return {'port': receivePort.sendPort}..addAll(additionalFields);
+}
+
+/// Converts [transform] into a serializable map.
+Map serializeTransform(Transform transform) {
+ return _serializeBaseTransform(transform, {
'primaryInput': serializeAsset(transform.primaryInput)
- };
+ }, (message) {
Bob Nystrom 2014/04/16 16:21:00 ...and then here, do: { 'getInput': () => retur
nweiz 2014/04/16 20:28:21 Done.
+ if (message['type'] == 'getInput') {
+ return transform.getInput(deserializeId(message['id']))
+ .then((asset) => serializeAsset(asset));
+ }
+
+ if (message['type'] != 'addOutput') return null;
+
+ transform.addOutput(deserializeAsset(message['output']));
+ return new Future.value();
+ });
}
-/// A wrapper for a [Transform] that's in the host isolate.
-///
-/// This retrieves inputs from and sends outputs and logs to the host isolate.
-class ForeignTransform implements Transform {
+/// Converts [transform] into a serializable map.
+Map serializeDeclaringTransform(DeclaringTransform transform) {
+ return _serializeBaseTransform(transform, {
+ 'primaryId': serializeId(transform.primaryId)
+ }, (message) {
+ if (message['type'] != 'declareOutput') return null;
+
+ transform.declareOutput(deserializeId(message['output']));
+ return new Future.value();
+ });
+}
+
+/// The base class for wrappers for [Transform]s that are in the host isolate.
+class _ForeignBaseTransform {
/// The port with which we communicate with the host isolate.
///
/// This port and all messages sent across it are specific to this transform.
final SendPort _port;
- final Asset primaryInput;
-
TransformLogger get logger => _logger;
TransformLogger _logger;
- /// Creates a transform from a serializable map sent from the host isolate.
- ForeignTransform(Map transform)
- : _port = transform['port'],
- primaryInput = deserializeAsset(transform['primaryInput']) {
+ _ForeignBaseTransform(Map transform)
+ : _port = transform['port'] {
_logger = new TransformLogger((assetId, level, message, span) {
call(_port, {
'type': 'log',
@@ -91,6 +110,22 @@ class ForeignTransform implements Transform {
});
}
+ void consumePrimary() {
+ call(_port, {'type': 'consumePrimary'});
+ }
+}
+
+/// A wrapper for a [Transform] that's in the host isolate.
+///
+/// This retrieves inputs from and sends outputs and logs to the host isolate.
+class ForeignTransform extends _ForeignBaseTransform implements Transform {
+ final Asset primaryInput;
+
+ /// Creates a transform from a serializable map sent from the host isolate.
Bob Nystrom 2014/04/16 16:21:00 "serializable" -> "serialized".
nweiz 2014/04/16 20:28:21 Done.
+ ForeignTransform(Map transform)
+ : primaryInput = deserializeAsset(transform['primaryInput']),
+ super(transform);
+
Future<Asset> getInput(AssetId id) {
return call(_port, {
'type': 'getInput',
@@ -119,8 +154,22 @@ class ForeignTransform implements Transform {
'output': serializeAsset(output)
});
}
+}
- void consumePrimary() {
- call(_port, {'type': 'consumePrimary'});
+/// A wrapper for a [DeclaringTransform] that's in the host isolate.
+class ForeignDeclaringTransform extends _ForeignBaseTransform
+ implements DeclaringTransform {
+ final AssetId primaryId;
+
+ /// Creates a transform from a serializable map sent from the host isolate.
+ ForeignDeclaringTransform(Map transform)
+ : primaryId = deserializeId(transform['primaryId']),
+ super(transform);
+
+ void declareOutput(AssetId id) {
+ call(_port, {
+ 'type': 'declareOutput',
+ 'output': serializeId(id)
+ });
}
}

Powered by Google App Engine
This is Rietveld 408576698