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

Side by Side Diff: pkg/barback/lib/src/transformer/aggregate_transform.dart

Issue 262173009: Add aggregate transformer types to barback. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 7 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
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library barback.transformer.transform; 5 library barback.transformer.aggregate_transform;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 9
10 import '../asset/asset.dart'; 10 import '../asset/asset.dart';
11 import '../asset/asset_id.dart'; 11 import '../asset/asset_id.dart';
12 import '../asset/asset_set.dart'; 12 import '../asset/asset_set.dart';
13 import '../errors.dart'; 13 import '../errors.dart';
14 import '../graph/transform_node.dart'; 14 import '../graph/transform_node.dart';
15 import '../utils.dart'; 15 import '../utils.dart';
16 import 'base_transform.dart'; 16 import 'base_transform.dart';
17 17
18 /// While a [Transformer] represents a *kind* of transformation, this defines 18 /// A transform for [AggregateTransformer]s that provides access to all of their
19 /// one specific usage of it on a set of files. 19 /// primary inputs.
20 /// 20 class AggregateTransform extends BaseTransform {
21 /// This ephemeral object exists only during an actual transform application to
22 /// facilitate communication between the [Transformer] and the code hosting
23 /// the transformation. It lets the [Transformer] access inputs and generate
24 /// outputs.
25 class Transform extends BaseTransform {
26 final TransformNode _node; 21 final TransformNode _node;
27 22
23 /// The set of outputs emitted by the transformer.
28 final _outputs = new AssetSet(); 24 final _outputs = new AssetSet();
29 25
30 /// Gets the primary input asset. 26 /// The transform key.
31 /// 27 ///
32 /// While a transformation can use multiple input assets, one must be a 28 /// This is the key returned by [AggregateTransformer.classifyPrimary] for all
33 /// special "primary" asset. This will be the "entrypoint" or "main" input 29 /// the assets in this transform.
34 /// file for a transformation. 30 String get key => _node.key;
31
32 /// The stream of primary inputs that will be processed by this transform.
35 /// 33 ///
36 /// For example, with a dart2js transform, the primary input would be the 34 /// This is exposed as a stream so that the transformer can start working
37 /// entrypoint Dart file. All of the other Dart files that that imports 35 /// before all its inputs are available. The stream is closed not just when
38 /// would be secondary inputs. 36 /// all inputs are provided, but when barback is confident no more inputs will
37 /// be forthcoming.
39 /// 38 ///
40 /// This method may fail at runtime with an [AssetNotFoundException] if called 39 /// A transformer may complete its `apply` method before this stream is
41 /// asynchronously after the transform begins running. The primary input may 40 /// closed. For example, it may know that each key will only have two inputs
42 /// become unavailable while this transformer is running due to asset changes 41 /// associated with it, and so use `transform.primaryInputs.take(2)` to access
43 /// earlier in the graph. You can ignore the error if this happens: the 42 /// only those inputs.
44 /// transformer will be re-run automatically for you. 43 Stream<Asset> get primaryInputs => _primaryInputs;
45 Asset get primaryInput { 44 Stream<Asset> _primaryInputs;
46 if (!_node.primary.state.isAvailable) {
47 throw new AssetNotFoundException(_node.primary.id);
48 }
49 45
50 return _node.primary.asset; 46 /// The controller for [primaryInputs].
47 ///
48 /// This is a broadcast controller so that the transform can keep
49 /// [_emittedPrimaryInputs] up to date.
50 final _inputController = new StreamController<Asset>.broadcast();
51
52 /// The set of all primary inputs that have been emitted by [primaryInputs].
53 final _emittedPrimaryInputs = new AssetSet();
54
55 AggregateTransform._(TransformNode node)
56 : _node = node,
57 super(node) {
58 _inputController.stream.listen(_emittedPrimaryInputs.add);
59 // [primaryInputs] should be a non-broadcast stream.
60 _primaryInputs = broadcastToSingleSubscription(_inputController.stream);
51 } 61 }
52 62
53 Transform._(TransformNode node)
54 : _node = node,
55 super(node);
56
57 /// Gets the asset for an input [id]. 63 /// Gets the asset for an input [id].
58 /// 64 ///
59 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. 65 /// If an input with [id] cannot be found, throws an [AssetNotFoundException].
60 Future<Asset> getInput(AssetId id) { 66 Future<Asset> getInput(AssetId id) {
61 if (id == _node.primary.id) return syncFuture(() => primaryInput); 67 if (_emittedPrimaryInputs.containsId(id)) {
62 return _node.getInput(id); 68 return syncFuture(() => _emittedPrimaryInputs[id]);
69 } else {
70 return _node.getInput(id);
71 }
63 } 72 }
64 73
65 /// A convenience method to the contents of the input with [id] as a string. 74 /// A convenience method to the contents of the input with [id] as a string.
66 /// 75 ///
67 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. 76 /// This is equivalent to calling [getInput] followed by [Asset.readAsString].
68 /// 77 ///
69 /// If the asset was created from a [String] the original string is always 78 /// If the asset was created from a [String] the original string is always
70 /// returned and [encoding] is ignored. Otherwise, the binary data of the 79 /// returned and [encoding] is ignored. Otherwise, the binary data of the
71 /// asset is decoded using [encoding], which defaults to [UTF8]. 80 /// asset is decoded using [encoding], which defaults to [UTF8].
72 /// 81 ///
(...skipping 25 matching lines...) Expand all
98 } 107 }
99 108
100 /// Stores [output] as the output created by this transformation. 109 /// Stores [output] as the output created by this transformation.
101 /// 110 ///
102 /// A transformation can output as many assets as it wants. 111 /// A transformation can output as many assets as it wants.
103 void addOutput(Asset output) { 112 void addOutput(Asset output) {
104 // TODO(rnystrom): This should immediately throw if an output with that ID 113 // TODO(rnystrom): This should immediately throw if an output with that ID
105 // has already been created by this transformer. 114 // has already been created by this transformer.
106 _outputs.add(output); 115 _outputs.add(output);
107 } 116 }
117
118 void consumePrimary(AssetId id) {
119 if (!_emittedPrimaryInputs.containsId(id)) {
120 throw new StateError(
121 "$id can't be consumed because it's not a primary input.");
122 }
123
124 super.consumePrimary(id);
125 }
108 } 126 }
109 127
110 /// The controller for [Transform]. 128 /// The controller for [AggregateTransform].
111 class TransformController extends BaseTransformController { 129 class AggregateTransformController extends BaseTransformController {
112 Transform get transform => super.transform; 130 AggregateTransform get transform => super.transform;
113 131
114 /// The set of assets that the transformer has emitted. 132 /// The set of assets that the transformer has emitted.
115 AssetSet get outputs => transform._outputs; 133 AssetSet get outputs => transform._outputs;
116 134
117 TransformController(TransformNode node) 135 /// The controller for the [AggregateTransform.primaryInputs] stream.
118 : super(new Transform._(node)); 136 StreamController<Asset> get inputController => transform._inputController;
137
138 AggregateTransformController(TransformNode node)
139 : super(new AggregateTransform._(node));
140
141 void close() {
142 super.close();
143 transform._inputController.close();
144 }
119 } 145 }
OLDNEW
« no previous file with comments | « pkg/barback/lib/src/graph/transform_node.dart ('k') | pkg/barback/lib/src/transformer/aggregate_transformer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698