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

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

Issue 1165473002: Start pulling pub from its own repo. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Code review changes Created 5 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library pub.asset.serialize.transform;
6
7 import 'dart:async';
8 import 'dart:isolate';
9
10 import 'package:barback/barback.dart';
11 // TODO(nweiz): don't import from "src" once issue 14966 is fixed.
12 import 'package:barback/src/internal_asset.dart';
13
14 import '../serialize.dart';
15 import 'get_input_transform.dart';
16
17 /// Serialize the methods shared between [Transform] and [DeclaringTransform].
18 ///
19 /// [additionalFields] contains additional serialized fields to add to the
20 /// serialized transform. [methodHandlers] is a set of additional methods. Each
21 /// value should take a JSON message and return the response (which may be a
22 /// Future).
23 Map _serializeBaseTransform(transform, Map additionalFields,
24 Map<String, Function> methodHandlers) {
25 var receivePort = new ReceivePort();
26 receivePort.listen((wrappedMessage) {
27 respond(wrappedMessage, (message) {
28 var handler = methodHandlers[message['type']];
29 if (handler != null) return handler(message);
30
31 if (message['type'] == 'consumePrimary') {
32 transform.consumePrimary();
33 return null;
34 }
35
36 assert(message['type'] == 'log');
37 var method = {
38 'Info': transform.logger.info,
39 'Fine': transform.logger.fine,
40 'Warning': transform.logger.warning,
41 'Error': transform.logger.error
42 }[message['level']];
43 assert(method != null);
44
45 var assetId = message['assetId'] == null ? null :
46 deserializeId(message['assetId']);
47 var span = message['span'] == null ? null :
48 deserializeSpan(message['span']);
49 method(message['message'], asset: assetId, span: span);
50 });
51 });
52
53 return {'port': receivePort.sendPort}..addAll(additionalFields);
54 }
55
56 /// Converts [transform] into a serializable map.
57 Map serializeTransform(Transform transform) {
58 return _serializeBaseTransform(transform, {
59 'primaryInput': serializeAsset(transform.primaryInput)
60 }, {
61 'getInput': (message) => transform.getInput(deserializeId(message['id']))
62 .then((asset) => serializeAsset(asset)),
63 'addOutput': (message) =>
64 transform.addOutput(deserializeAsset(message['output']))
65 });
66 }
67
68 /// Converts [transform] into a serializable map.
69 Map serializeDeclaringTransform(DeclaringTransform transform) {
70 return _serializeBaseTransform(transform, {
71 'primaryId': serializeId(transform.primaryId)
72 }, {
73 'declareOutput': (message) =>
74 transform.declareOutput(deserializeId(message['output']))
75 });
76 }
77
78 /// The base class for wrappers for [Transform]s that are in the host isolate.
79 class _ForeignBaseTransform {
80 /// The port with which we communicate with the host isolate.
81 ///
82 /// This port and all messages sent across it are specific to this transform.
83 final SendPort _port;
84
85 TransformLogger get logger => _logger;
86 TransformLogger _logger;
87
88 _ForeignBaseTransform(Map transform)
89 : _port = transform['port'] {
90 _logger = new TransformLogger((assetId, level, message, span) {
91 call(_port, {
92 'type': 'log',
93 'level': level.name,
94 'message': message,
95 'assetId': assetId == null ? null : serializeId(assetId),
96 'span': span == null ? null : serializeSpan(span)
97 });
98 });
99 }
100
101 void consumePrimary() {
102 call(_port, {'type': 'consumePrimary'});
103 }
104 }
105
106 /// A wrapper for a [Transform] that's in the host isolate.
107 ///
108 /// This retrieves inputs from and sends outputs and logs to the host isolate.
109 class ForeignTransform extends _ForeignBaseTransform
110 with GetInputTransform implements Transform {
111 final Asset primaryInput;
112
113 /// Creates a transform from a serialized map sent from the host isolate.
114 ForeignTransform(Map transform)
115 : primaryInput = deserializeAsset(transform['primaryInput']),
116 super(transform);
117
118 Future<Asset> getInput(AssetId id) {
119 return call(_port, {
120 'type': 'getInput',
121 'id': serializeId(id)
122 }).then(deserializeAsset);
123 }
124
125 void addOutput(Asset output) {
126 call(_port, {
127 'type': 'addOutput',
128 'output': serializeAsset(output)
129 });
130 }
131 }
132
133 /// A wrapper for a [DeclaringTransform] that's in the host isolate.
134 class ForeignDeclaringTransform extends _ForeignBaseTransform
135 implements DeclaringTransform {
136 final AssetId primaryId;
137
138 /// Creates a transform from a serializable map sent from the host isolate.
139 ForeignDeclaringTransform(Map transform)
140 : primaryId = deserializeId(transform['primaryId']),
141 super(transform);
142
143 void declareOutput(AssetId id) {
144 call(_port, {
145 'type': 'declareOutput',
146 'output': serializeId(id)
147 });
148 }
149 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698