OLD | NEW |
| (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.transformer; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:isolate'; | |
9 | |
10 import 'package:barback/barback.dart'; | |
11 | |
12 import '../serialize.dart'; | |
13 import 'transform.dart'; | |
14 | |
15 /// Converts [transformer] into a serializable map. | |
16 Map _serializeTransformer(Transformer transformer) { | |
17 var port = new ReceivePort(); | |
18 port.listen((wrappedMessage) { | |
19 respond(wrappedMessage, (message) { | |
20 if (message['type'] == 'isPrimary') { | |
21 return transformer.isPrimary(deserializeId(message['id'])); | |
22 } else if (message['type'] == 'declareOutputs') { | |
23 return new Future.sync(() { | |
24 return (transformer as DeclaringTransformer).declareOutputs( | |
25 new ForeignDeclaringTransform(message['transform'])); | |
26 }).then((_) => null); | |
27 } else { | |
28 assert(message['type'] == 'apply'); | |
29 | |
30 // Make sure we return null so that if the transformer's [apply] returns | |
31 // a non-serializable value it doesn't cause problems. | |
32 return new Future.sync(() { | |
33 return transformer.apply(new ForeignTransform(message['transform'])); | |
34 }).then((_) => null); | |
35 } | |
36 }); | |
37 }); | |
38 | |
39 var type; | |
40 if (transformer is LazyTransformer) { | |
41 type = 'LazyTransformer'; | |
42 } else if (transformer is DeclaringTransformer) { | |
43 type = 'DeclaringTransformer'; | |
44 } else { | |
45 type = 'Transformer'; | |
46 } | |
47 | |
48 return { | |
49 'type': type, | |
50 'toString': transformer.toString(), | |
51 'port': port.sendPort | |
52 }; | |
53 } | |
54 | |
55 /// Converts [transformer] into a serializable map. | |
56 Map _serializeAggregateTransformer(AggregateTransformer transformer) { | |
57 var port = new ReceivePort(); | |
58 port.listen((wrappedMessage) { | |
59 respond(wrappedMessage, (message) { | |
60 if (message['type'] == 'classifyPrimary') { | |
61 return transformer.classifyPrimary(deserializeId(message['id'])); | |
62 } else if (message['type'] == 'declareOutputs') { | |
63 return new Future.sync(() { | |
64 return (transformer as DeclaringAggregateTransformer).declareOutputs( | |
65 new ForeignDeclaringAggregateTransform(message['transform'])); | |
66 }).then((_) => null); | |
67 } else { | |
68 assert(message['type'] == 'apply'); | |
69 | |
70 // Make sure we return null so that if the transformer's [apply] returns | |
71 // a non-serializable value it doesn't cause problems. | |
72 return new Future.sync(() { | |
73 return transformer.apply( | |
74 new ForeignAggregateTransform(message['transform'])); | |
75 }).then((_) => null); | |
76 } | |
77 }); | |
78 }); | |
79 | |
80 var type; | |
81 if (transformer is LazyAggregateTransformer) { | |
82 type = 'LazyAggregateTransformer'; | |
83 } else if (transformer is DeclaringAggregateTransformer) { | |
84 type = 'DeclaringAggregateTransformer'; | |
85 } else { | |
86 type = 'AggregateTransformer'; | |
87 } | |
88 | |
89 return { | |
90 'type': type, | |
91 'toString': transformer.toString(), | |
92 'port': port.sendPort | |
93 }; | |
94 } | |
95 | |
96 // Converts [group] into a serializable map. | |
97 Map _serializeTransformerGroup(TransformerGroup group) { | |
98 if (group.phases == null) { | |
99 throw "TransformerGroup $group phases cannot be null."; | |
100 } | |
101 | |
102 return { | |
103 'type': 'TransformerGroup', | |
104 'toString': group.toString(), | |
105 'phases': group.phases.map((phase) { | |
106 return phase.map(serializeTransformerLike).toList(); | |
107 }).toList() | |
108 }; | |
109 } | |
110 | |
111 /// Converts [transformerLike] into a serializable map. | |
112 /// | |
113 /// [transformerLike] can be a [Transformer], an [AggregateTransformer], or a | |
114 /// [TransformerGroup]. | |
115 Map serializeTransformerLike(transformerLike) { | |
116 if (transformerLike is Transformer) { | |
117 return _serializeTransformer(transformerLike); | |
118 } else if (transformerLike is TransformerGroup) { | |
119 return _serializeTransformerGroup(transformerLike); | |
120 } else { | |
121 // This has to be last, since "transformerLike is AggregateTransformer" will | |
122 // throw on older versions of barback. | |
123 assert(transformerLike is AggregateTransformer); | |
124 return _serializeAggregateTransformer(transformerLike); | |
125 } | |
126 } | |
OLD | NEW |