| 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 '../utils.dart'; | |
| 12 import 'base_transform.dart'; | |
| 13 | |
| 14 /// A transform for [DeclaringAggregateTransformer]s that allows them to declare | |
| 15 /// the ids of the outputs they'll generate without generating the concrete | |
| 16 /// bodies of those outputs. | |
| 17 class DeclaringAggregateTransform extends BaseTransform { | |
| 18 /// The set of output ids declared by the transformer. | |
| 19 final _outputIds = new Set<AssetId>(); | |
| 20 | |
| 21 /// The transform key. | |
| 22 /// | |
| 23 /// This is the key returned by [AggregateTransformer.classifyPrimary] for all | |
| 24 /// the assets in this transform. | |
| 25 final String key; | |
| 26 | |
| 27 /// The package in which this transform is running. | |
| 28 final String package; | |
| 29 | |
| 30 /// The stream of primary input ids that have been aggregated for this | |
| 31 /// transform. | |
| 32 /// | |
| 33 /// This is exposed as a stream so that the transformer can start working | |
| 34 /// before all its input ids are available. The stream is closed not just when | |
| 35 /// all inputs are provided, but when barback is confident no more inputs will | |
| 36 /// be forthcoming. | |
| 37 /// | |
| 38 /// A transformer may complete its `declareOutputs` method before this stream | |
| 39 /// is closed. For example, it may know that each key will only have two | |
| 40 /// inputs associated with it, and so use `transform.primaryIds.take(2)` to | |
| 41 /// access only those inputs' ids. | |
| 42 Stream<AssetId> get primaryIds => _primaryIds; | |
| 43 Stream<AssetId> _primaryIds; | |
| 44 | |
| 45 /// The controller for [primaryIds]. | |
| 46 /// | |
| 47 /// This is a broadcast controller so that the transform can keep | |
| 48 /// [_emittedPrimaryIds] up to date. | |
| 49 final _idController = new StreamController<AssetId>.broadcast(); | |
| 50 | |
| 51 /// The set of all primary input ids that have been emitted by [primaryIds]. | |
| 52 final _emittedPrimaryIds = new Set<AssetId>(); | |
| 53 | |
| 54 DeclaringAggregateTransform._(TransformNode node) | |
| 55 : key = node.key, | |
| 56 package = node.phase.cascade.package, | |
| 57 super(node) { | |
| 58 _idController.stream.listen(_emittedPrimaryIds.add); | |
| 59 // [primaryIds] should be a non-broadcast stream. | |
| 60 _primaryIds = broadcastToSingleSubscription(_idController.stream); | |
| 61 } | |
| 62 | |
| 63 /// Stores [id] as the id of an output that will be created by this | |
| 64 /// transformation when it's run. | |
| 65 /// | |
| 66 /// A transformation can declare as many assets as it wants. If | |
| 67 /// [DeclaringTransformer.declareOutputs] declares a given asset id for a | |
| 68 /// given input, [Transformer.apply] should emit the corresponding asset as | |
| 69 /// well. | |
| 70 void declareOutput(AssetId id) { | |
| 71 // TODO(nweiz): This should immediately throw if an output with that ID | |
| 72 // has already been declared by this transformer. | |
| 73 _outputIds.add(id); | |
| 74 } | |
| 75 | |
| 76 void consumePrimary(AssetId id) { | |
| 77 if (!_emittedPrimaryIds.contains(id)) { | |
| 78 throw new StateError( | |
| 79 "$id can't be consumed because it's not a primary input."); | |
| 80 } | |
| 81 | |
| 82 super.consumePrimary(id); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 /// The controller for [DeclaringAggregateTransform]. | |
| 87 class DeclaringAggregateTransformController extends BaseTransformController { | |
| 88 DeclaringAggregateTransform get transform => super.transform; | |
| 89 | |
| 90 /// The set of ids that the transformer declares it will emit. | |
| 91 Set<AssetId> get outputIds => transform._outputIds; | |
| 92 | |
| 93 bool get isDone => transform._idController.isClosed; | |
| 94 | |
| 95 DeclaringAggregateTransformController(TransformNode node) | |
| 96 : super(new DeclaringAggregateTransform._(node)); | |
| 97 | |
| 98 /// Adds a primary input id to the [DeclaringAggregateTransform.primaryIds] | |
| 99 /// stream. | |
| 100 void addId(AssetId id) => transform._idController.add(id); | |
| 101 | |
| 102 void done() { | |
| 103 transform._idController.close(); | |
| 104 } | |
| 105 } | |
| OLD | NEW |