Chromium Code Reviews| 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 barback.transformer.declaring_aggregate_transform; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import '../asset/asset_id.dart'; | |
| 10 import '../graph/transform_node.dart'; | |
| 11 import 'base_transform.dart'; | |
| 12 | |
| 13 /// A transform for [DeclaringAggregateTransformer]s that allows them to declare | |
| 14 /// the ids of the outputs they'll generate without generating the concrete | |
| 15 /// bodies of those outputs. | |
| 16 class DeclaringAggregateTransform extends BaseTransform { | |
|
Bob Nystrom
2014/05/05 23:41:56
A bunch of my comments on AggregateTransform apply
nweiz
2014/05/06 22:46:40
Done.
| |
| 17 /// The set of output ids declared by the transformer. | |
| 18 final _outputIds = new Set<AssetId>(); | |
| 19 | |
| 20 /// The aggregate key for this transform. | |
| 21 final key; | |
| 22 | |
| 23 /// The stream of primary input ids that have been aggregated for this | |
| 24 /// transform. | |
| 25 /// | |
| 26 /// This is exposed as a stream so that the transformer can start working | |
| 27 /// before all its input ids are available. The stream will be closed once | |
| 28 /// barback determines that no more inputs exist or will be generated for this | |
| 29 /// transform without external modification. | |
| 30 /// | |
| 31 /// A transformer may complete its `declareOutputs` method before this stream | |
| 32 /// is closed. For example, it may know that each key will only have two | |
| 33 /// inputs associated with it, and so use `transform.primaryIds.take(2)` to | |
| 34 /// access only those inputs' ids. | |
|
Bob Nystrom
2014/05/05 23:41:56
This seems like it would be more helpful as a doc
nweiz
2014/05/06 22:46:40
I'll put it both places.
| |
| 35 Stream<AssetId> get primaryIds => _primaryIds; | |
| 36 Stream<AssetId> _primaryIds; | |
| 37 | |
| 38 /// The controller for [primaryIds]. | |
| 39 /// | |
| 40 /// This is a broadcast controller so that the transform can keep | |
| 41 /// [_allPrimaryIds] up to date. | |
| 42 final _idController = new StreamController<AssetId>(); | |
| 43 | |
| 44 /// The set of all primary input ids that have been emitted by [primaryIds]. | |
| 45 final _allPrimaryIds = new Set<AssetId>(); | |
| 46 | |
| 47 DeclaringAggregateTransform._(TransformNode node) | |
| 48 : key = node.key, | |
| 49 super(node) { | |
| 50 _idController.stream.listen(_allPrimaryId.add); | |
|
Bob Nystrom
2014/05/05 23:41:56
_allPrimaryIds
nweiz
2014/05/06 22:46:40
Done.
| |
| 51 // [primaryIds] should be a non-broadcast stream. | |
| 52 _primaryIds = broadcastToSingleSubscription(_idController.stream); | |
| 53 } | |
| 54 | |
| 55 /// Stores [id] as the id of an output that will be created by this | |
| 56 /// transformation when it's run. | |
| 57 /// | |
| 58 /// A transformation can declare as many assets as it wants. If | |
| 59 /// [DeclaringTransformer.declareOutputs] declareds a given asset id for a | |
|
Bob Nystrom
2014/05/05 23:41:56
"declareds" -> "declares".
nweiz
2014/05/06 22:46:40
Done.
| |
| 60 /// given input, [Transformer.apply] should emit the corresponding asset as | |
| 61 /// well. | |
| 62 void declareOutput(AssetId id) { | |
| 63 // TODO(nweiz): This should immediately throw if an output with that ID | |
| 64 // has already been declared by this transformer. | |
|
Bob Nystrom
2014/05/05 23:41:56
Is there a reason we want to throw for this instea
nweiz
2014/05/06 22:46:40
It's effectively a collision—the user has provided
Bob Nystrom
2014/05/06 23:55:09
I suppose so, but part of the reason we care about
nweiz
2014/05/07 01:28:50
In the non-declaring transformer, though, it's not
| |
| 65 _outputIds.add(id); | |
| 66 } | |
| 67 | |
| 68 void consumePrimary(AssetId id) { | |
| 69 if (!_allPrimaryIds.contains(id)) { | |
| 70 throw new StateError( | |
| 71 "$id can't be consumed because it's not a primary input."); | |
| 72 } | |
| 73 | |
| 74 super(); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 /// The controller for [DeclaringAggregateTransform]. | |
| 79 class DeclaringAggregateTransformController extends BaseTransformController { | |
| 80 DeclaringAggregateTransform get transform => super.transform; | |
| 81 | |
| 82 /// The set of ids that the transformer declares it will emit. | |
| 83 Set<AssetId> get outputIds => transform._outputIds; | |
| 84 | |
| 85 /// The controller for the [DeclaringAggregateTransform.primaryIds] stream. | |
| 86 StreamController<AssetId> get idController => transform._idController; | |
| 87 | |
| 88 DeclaringAggregateTransformController(TransformNode node) | |
| 89 : super(new DeclaringAggregateTransform._(node)); | |
| 90 | |
| 91 void close() { | |
| 92 super.close(); | |
| 93 idController.close(); | |
| 94 } | |
| 95 } | |
| OLD | NEW |