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

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

Issue 203913003: Share code between the transformer isolate and pub proper. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
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 import 'dart:convert';
10
11 import 'package:barback/barback.dart';
12 // TODO(nweiz): don't import from "src" once issue 14966 is fixed.
13 import 'package:barback/src/internal_asset.dart';
14
15 import '../serialize.dart';
16 import '../utils.dart';
17
18 /// Converts [transform] into a serializable map.
19 Map serializeTransform(Transform transform) {
20 var receivePort = new ReceivePort();
21 receivePort.listen((wrappedMessage) {
22 respond(wrappedMessage, (message) {
23 if (message['type'] == 'getInput') {
24 return transform.getInput(deserializeId(message['id']))
25 .then((asset) => serializeAsset(asset));
26 }
27
28 if (message['type'] == 'addOutput') {
29 transform.addOutput(deserializeAsset(message['output']));
30 return null;
31 }
32
33 if (message['type'] == 'consumePrimary') {
34 transform.consumePrimary();
35 return null;
36 }
37
38 assert(message['type'] == 'log');
39 var method;
40 if (message['level'] == 'Info') {
41 method = transform.logger.info;
42 } else if (message['level'] == 'Fine') {
43 method = transform.logger.fine;
44 } else if (message['level'] == 'Warning') {
45 method = transform.logger.warning;
46 } else {
47 assert(message['level'] == 'Error');
48 method = transform.logger.error;
49 }
50
51 var assetId = message['assetId'] == null ? null :
52 deserializeId(message['assetId']);
53 var span = message['span'] == null ? null :
54 deserializeSpan(message['span']);
55 method(message['message'], asset: assetId, span: span);
56 });
57 });
58
59 return {
60 'port': receivePort.sendPort,
61 'primaryInput': serializeAsset(transform.primaryInput)
62 };
63 }
64
65 /// A wrapper for a [Transform] that's in the host isolate.
66 ///
67 /// This retrieves inputs from and sends outputs and logs to the host isolate.
68 class ForeignTransform implements Transform {
69 /// The port with which we communicate with the host isolate.
70 ///
71 /// This port and all messages sent across it are specific to this transform.
72 final SendPort _port;
73
74 final Asset primaryInput;
75
76 TransformLogger get logger => _logger;
77 TransformLogger _logger;
78
79 /// Creates a transform from a serializable map sent from the host isolate.
80 ForeignTransform(Map transform)
81 : _port = transform['port'],
82 primaryInput = deserializeAsset(transform['primaryInput']) {
83 _logger = new TransformLogger((assetId, level, message, span) {
84 call(_port, {
85 'type': 'log',
86 'level': level.name,
87 'message': message,
88 'assetId': assetId == null ? null : serializeId(assetId),
89 'span': span == null ? null : serializeSpan(span)
90 });
91 });
92 }
93
94 Future<Asset> getInput(AssetId id) {
95 return call(_port, {
96 'type': 'getInput',
97 'id': serializeId(id)
98 }).then(deserializeAsset);
99 }
100
101 Future<String> readInputAsString(AssetId id, {Encoding encoding}) {
102 if (encoding == null) encoding = UTF8;
103 return getInput(id).then((input) => input.readAsString(encoding: encoding));
104 }
105
106 Stream<List<int>> readInput(AssetId id) =>
107 futureStream(getInput(id).then((input) => input.read()));
108
109 Future<bool> hasInput(AssetId id) {
110 return getInput(id).then((_) => true).catchError((error) {
111 if (error is AssetNotFoundException && error.id == id) return false;
112 throw error;
113 });
114 }
115
116 void addOutput(Asset output) {
117 call(_port, {
118 'type': 'addOutput',
119 'output': serializeAsset(output)
120 });
121 }
122
123 void consumePrimary() {
124 call(_port, {'type': 'consumePrimary'});
125 }
126 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/pub/asset/dart/serialize/exception.dart ('k') | sdk/lib/_internal/pub/asset/dart/serialize/transformer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698