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

Side by Side Diff: pkg/barback/lib/src/transformer/declaring_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
(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 stream of primary input ids that have been aggregated for this
28 /// transform.
29 ///
30 /// This is exposed as a stream so that the transformer can start working
31 /// before all its input ids are available. The stream is closed not just when
32 /// all inputs are provided, but when barback is confident no more inputs will
33 /// be forthcoming.
34 ///
35 /// A transformer may complete its `declareOutputs` method before this stream
36 /// is closed. For example, it may know that each key will only have two
37 /// inputs associated with it, and so use `transform.primaryIds.take(2)` to
38 /// access only those inputs' ids.
39 Stream<AssetId> get primaryIds => _primaryIds;
40 Stream<AssetId> _primaryIds;
41
42 /// The controller for [primaryIds].
43 ///
44 /// This is a broadcast controller so that the transform can keep
45 /// [_emittedPrimaryIds] up to date.
46 final _idController = new StreamController<AssetId>();
47
48 /// The set of all primary input ids that have been emitted by [primaryIds].
49 final _emittedPrimaryIds = new Set<AssetId>();
50
51 DeclaringAggregateTransform._(TransformNode node)
52 : key = node.key,
53 super(node) {
54 _idController.stream.listen(_emittedPrimaryIds.add);
55 // [primaryIds] should be a non-broadcast stream.
56 _primaryIds = broadcastToSingleSubscription(_idController.stream);
57 }
58
59 /// Stores [id] as the id of an output that will be created by this
60 /// transformation when it's run.
61 ///
62 /// A transformation can declare as many assets as it wants. If
63 /// [DeclaringTransformer.declareOutputs] declares a given asset id for a
64 /// given input, [Transformer.apply] should emit the corresponding asset as
65 /// well.
66 void declareOutput(AssetId id) {
67 // TODO(nweiz): This should immediately throw if an output with that ID
68 // has already been declared by this transformer.
69 _outputIds.add(id);
70 }
71
72 void consumePrimary(AssetId id) {
73 if (!_emittedPrimaryIds.contains(id)) {
74 throw new StateError(
75 "$id can't be consumed because it's not a primary input.");
76 }
77
78 super.consumePrimary(id);
79 }
80 }
81
82 /// The controller for [DeclaringAggregateTransform].
83 class DeclaringAggregateTransformController extends BaseTransformController {
84 DeclaringAggregateTransform get transform => super.transform;
85
86 /// The set of ids that the transformer declares it will emit.
87 Set<AssetId> get outputIds => transform._outputIds;
88
89 /// The controller for the [DeclaringAggregateTransform.primaryIds] stream.
90 StreamController<AssetId> get idController => transform._idController;
91
92 DeclaringAggregateTransformController(TransformNode node)
93 : super(new DeclaringAggregateTransform._(node));
94
95 void close() {
96 super.close();
97 idController.close();
98 }
99 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698