Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 aggregate key for this transform. |
|
Bob Nystrom
2014/05/05 23:41:56
"Aggregate key" already means something to a lot o
nweiz
2014/05/06 22:46:40
Done.
| |
| 27 get key => _node.key; | |
| 28 | |
| 29 /// The stream of primary inputs that have been aggregated for this transform. | |
|
Bob Nystrom
2014/05/05 23:41:56
"have been aggregated for" -> "will be processed b
nweiz
2014/05/06 22:46:40
Done.
| |
| 31 /// | 30 /// |
| 32 /// While a transformation can use multiple input assets, one must be a | 31 /// This is exposed as a stream so that the transformer can start working |
| 33 /// special "primary" asset. This will be the "entrypoint" or "main" input | 32 /// before all its inputs are available. The stream will be closed once |
| 34 /// file for a transformation. | 33 /// barback determines that no more inputs exist or will be generated for this |
| 34 /// transform without external modification. | |
|
Bob Nystrom
2014/05/05 23:41:56
This is a bit hard to read in part because of the
nweiz
2014/05/06 22:46:40
I want to be explicit that the stream will stay op
Bob Nystrom
2014/05/06 23:55:09
Maybe just say that then: "The stream is closed no
nweiz
2014/05/07 01:28:50
Done.
| |
| 35 /// | 35 /// |
| 36 /// For example, with a dart2js transform, the primary input would be the | 36 /// A transformer may complete its `apply` method before this stream is |
| 37 /// entrypoint Dart file. All of the other Dart files that that imports | 37 /// closed. For example, it may know that each key will only have two inputs |
| 38 /// would be secondary inputs. | 38 /// associated with it, and so use `transform.primaryInputs.take(2)` to access |
| 39 /// only those inputs. | |
|
Bob Nystrom
2014/05/05 23:41:56
I think this is a bit misleading. If the transform
nweiz
2014/05/06 22:46:40
`toList()` is definitely not what you want to do i
Bob Nystrom
2014/05/06 23:55:09
Oof, we should discuss this, because I think that
nweiz
2014/05/07 01:28:50
toList() will work just fine for 99% of aggregate
| |
| 40 Stream<Asset> get primaryInputs => _primaryInputs; | |
| 41 Stream<Asset> _primaryInputs; | |
| 42 | |
| 43 /// The controller for [primaryInputs]. | |
| 39 /// | 44 /// |
| 40 /// This method may fail at runtime with an [AssetNotFoundException] if called | 45 /// This is a broadcast controller so that the transform can keep |
| 41 /// asynchronously after the transform begins running. The primary input may | 46 /// [_allPrimaryInputs] up to date. |
| 42 /// become unavailable while this transformer is running due to asset changes | 47 final _inputController = new StreamController<Asset>.broadcast(); |
| 43 /// earlier in the graph. You can ignore the error if this happens: the | |
| 44 /// transformer will be re-run automatically for you. | |
| 45 Asset get primaryInput { | |
| 46 if (!_node.primary.state.isAvailable) { | |
| 47 throw new AssetNotFoundException(_node.primary.id); | |
| 48 } | |
| 49 | 48 |
| 50 return _node.primary.asset; | 49 /// The set of all primary inputs that have been emitted by [primaryInputs]. |
| 50 final _allPrimaryInputs = new AssetSet(); | |
|
Bob Nystrom
2014/05/05 23:41:56
"all" isn't very helpful here. How about "_emitted
nweiz
2014/05/06 22:46:40
Done.
| |
| 51 | |
| 52 AggregateTransform._(TransformNode node) | |
| 53 : _node = node, | |
| 54 super(node) { | |
| 55 _inputController.stream.listen(_allPrimaryInputs.add); | |
| 56 // [primaryInputs] should be a non-broadcast stream. | |
| 57 _primaryInputs = broadcastToSingleSubscription(_inputController.stream); | |
| 51 } | 58 } |
| 52 | 59 |
| 53 Transform._(TransformNode node) | |
| 54 : _node = node, | |
| 55 super(node); | |
| 56 | |
| 57 /// Gets the asset for an input [id]. | 60 /// Gets the asset for an input [id]. |
| 58 /// | 61 /// |
| 59 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 62 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
| 60 Future<Asset> getInput(AssetId id) { | 63 Future<Asset> getInput(AssetId id) { |
| 61 if (id == _node.primary.id) return syncFuture(() => primaryInput); | 64 if (_allPrimaryInputs.containsId(id)) { |
| 62 return _node.getInput(id); | 65 return syncFuture(() => _allPrimaryInputs[id]); |
| 66 } else { | |
| 67 return _node.getInput(id); | |
| 68 } | |
| 63 } | 69 } |
| 64 | 70 |
| 65 /// A convenience method to the contents of the input with [id] as a string. | 71 /// A convenience method to the contents of the input with [id] as a string. |
| 66 /// | 72 /// |
| 67 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. | 73 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. |
| 68 /// | 74 /// |
| 69 /// If the asset was created from a [String] the original string is always | 75 /// 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 | 76 /// returned and [encoding] is ignored. Otherwise, the binary data of the |
| 71 /// asset is decoded using [encoding], which defaults to [UTF8]. | 77 /// asset is decoded using [encoding], which defaults to [UTF8]. |
| 72 /// | 78 /// |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 98 } | 104 } |
| 99 | 105 |
| 100 /// Stores [output] as the output created by this transformation. | 106 /// Stores [output] as the output created by this transformation. |
| 101 /// | 107 /// |
| 102 /// A transformation can output as many assets as it wants. | 108 /// A transformation can output as many assets as it wants. |
| 103 void addOutput(Asset output) { | 109 void addOutput(Asset output) { |
| 104 // TODO(rnystrom): This should immediately throw if an output with that ID | 110 // TODO(rnystrom): This should immediately throw if an output with that ID |
| 105 // has already been created by this transformer. | 111 // has already been created by this transformer. |
| 106 _outputs.add(output); | 112 _outputs.add(output); |
| 107 } | 113 } |
| 114 | |
| 115 void consumePrimary(AssetId id) { | |
| 116 if (!_allPrimaryInputs.containsId(id)) { | |
|
Bob Nystrom
2014/05/05 23:41:56
What if you know what the primary input *will* be
nweiz
2014/05/06 22:46:40
You really shouldn't be assuming that an asset exi
Bob Nystrom
2014/05/06 23:55:09
SGTM.
| |
| 117 throw new StateError( | |
| 118 "$id can't be consumed because it's not a primary input."); | |
| 119 } | |
| 120 | |
| 121 super(); | |
| 122 } | |
| 108 } | 123 } |
| 109 | 124 |
| 110 /// The controller for [Transform]. | 125 /// The controller for [AggregateTransform]. |
| 111 class TransformController extends BaseTransformController { | 126 class AggregateTransformController extends BaseTransformController { |
| 112 Transform get transform => super.transform; | 127 AggregateTransform get transform => super.transform; |
| 113 | 128 |
| 114 /// The set of assets that the transformer has emitted. | 129 /// The set of assets that the transformer has emitted. |
| 115 AssetSet get outputs => transform._outputs; | 130 AssetSet get outputs => transform._outputs; |
| 116 | 131 |
| 117 TransformController(TransformNode node) | 132 /// The controller for the [AggregateTransform.primaryInputs] stream. |
| 118 : super(new Transform._(node)); | 133 StreamController<Asset> get inputController => transform._inputController; |
| 134 | |
| 135 AggregateTransformController(TransformNode node) | |
| 136 : super(new AggregateTransform._(node)); | |
| 137 | |
| 138 void close() { | |
| 139 super.close(); | |
| 140 inputController.close(); | |
| 141 } | |
| 119 } | 142 } |
| OLD | NEW |