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

Side by Side Diff: pkg/barback/lib/src/graph/transformer_classifier.dart

Issue 299833003: Expose aggregate transformers in barback. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 6 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.graph.transformer_classifier; 5 library barback.graph.transformer_classifier;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import '../asset/asset_forwarder.dart'; 9 import '../asset/asset_forwarder.dart';
10 import '../asset/asset_node.dart'; 10 import '../asset/asset_node.dart';
11 import '../errors.dart'; 11 import '../errors.dart';
12 import '../log.dart'; 12 import '../log.dart';
13 import '../transformer/aggregate_transformer.dart'; 13 import '../transformer/aggregate_transformer.dart';
14 import '../transformer/wrapping_aggregate_transformer.dart'; 14 import '../transformer/wrapping_aggregate_transformer.dart';
15 import '../utils.dart'; 15 import '../utils.dart';
16 import 'node_status.dart'; 16 import 'node_status.dart';
17 import 'node_streams.dart'; 17 import 'node_streams.dart';
18 import 'phase.dart'; 18 import 'phase.dart';
19 import 'transform_node.dart'; 19 import 'transform_node.dart';
20 20
21 /// A class for classifying the primary inputs for a transformer according to 21 /// A class for classifying the primary inputs for a transformer according to
22 /// its [AggregateTransformer.classifyPrimary] method. 22 /// its [AggregateTransformer.classifyPrimary] method.
23 /// 23 ///
24 /// This is also used for non-aggregate transformers; they're modeled as 24 /// This is also used for non-aggregate transformers; they're modeled as
25 /// aggregate transformers that return the primary path if `isPrimary` is true 25 /// aggregate transformers that return the primary path if `isPrimary` is true
26 /// and `null` if `isPrimary` is `null`. 26 /// and `null` if `isPrimary` is `null`.
27 class TransformerClassifier { 27 class TransformerClassifier {
28 /// The containing [Phase]. 28 /// The containing [Phase].
29 final Phase _phase; 29 final Phase phase;
30 30
31 /// The [AggregateTransformer] used to classify the inputs. 31 /// The [AggregateTransformer] used to classify the inputs.
32 final AggregateTransformer transformer; 32 final AggregateTransformer transformer;
33 33
34 /// A string describing the location of [this] in the transformer graph. 34 /// A string describing the location of [this] in the transformer graph.
35 final String _location; 35 final String _location;
36 36
37 /// The individual transforms for each classiciation key. 37 /// The individual transforms for each classiciation key.
38 final _transforms = new Map<Object, TransformNode>(); 38 final _transforms = new Map<Object, TransformNode>();
39 39
40 /// Forwarders used to pass through assets that aren't used by [transformer]. 40 /// Forwarders used to pass through assets that aren't used by [transformer].
41 final _passThroughForwarders = new Set<AssetForwarder>(); 41 final _passThroughForwarders = new Set<AssetForwarder>();
42 42
43 /// The streams exposed by this classifier. 43 /// The streams exposed by this classifier.
44 final _streams = new NodeStreams(); 44 final _streams = new NodeStreams();
45 Stream get onStatusChange => _streams.onStatusChange; 45 Stream get onStatusChange => _streams.onStatusChange;
46 Stream<AssetNode> get onAsset => _streams.onAsset; 46 Stream<AssetNode> get onAsset => _streams.onAsset;
47 Stream<LogEntry> get onLog => _streams.onLog; 47 Stream<LogEntry> get onLog => _streams.onLog;
48 48
49 /// A broadcast stream that emits an event whenever [this] has finished
50 /// classifying all available inputs.
51 Stream get onDoneClassifying => _onDoneClassifyingController.stream;
52 final _onDoneClassifyingController =
53 new StreamController.broadcast(sync: true);
54
49 /// The number of currently-active calls to [transformer.classifyPrimary]. 55 /// The number of currently-active calls to [transformer.classifyPrimary].
50 /// 56 ///
51 /// This is used to determine whether [this] is dirty. 57 /// This is used to determine whether [this] is dirty.
52 var _activeClassifications = 0; 58 var _activeClassifications = 0;
53 59
60 /// Whether this is currently classifying any inputs.
61 bool get isClassifying => _activeClassifications > 0;
62
54 /// How far along [this] is in processing its assets. 63 /// How far along [this] is in processing its assets.
55 NodeStatus get status { 64 NodeStatus get status {
56 if (_activeClassifications > 0) return NodeStatus.RUNNING; 65 if (isClassifying) return NodeStatus.RUNNING;
57 return NodeStatus.dirtiest( 66 return NodeStatus.dirtiest(
58 _transforms.values.map((transform) => transform.status)); 67 _transforms.values.map((transform) => transform.status));
59 } 68 }
60 69
61 TransformerClassifier(this._phase, transformer, this._location) 70 TransformerClassifier(this.phase, transformer, this._location)
62 : transformer = transformer is AggregateTransformer ? 71 : transformer = transformer is AggregateTransformer ?
63 transformer : new WrappingAggregateTransformer(transformer); 72 transformer : new WrappingAggregateTransformer(transformer);
64 73
65 /// Adds a new asset as an input for this transformer. 74 /// Adds a new asset as an input for this transformer.
66 void addInput(AssetNode input) { 75 void addInput(AssetNode input) {
67 _activeClassifications++; 76 _activeClassifications++;
68 syncFuture(() => transformer.classifyPrimary(input.id)).catchError( 77 syncFuture(() => transformer.classifyPrimary(input.id)).catchError(
69 (error, stackTrace) { 78 (error, stackTrace) {
70 if (input.state.isRemoved) return false; 79 if (input.state.isRemoved) return null;
71 80
72 // Catch all transformer errors and pipe them to the results stream. This 81 // Catch all transformer errors and pipe them to the results stream. This
73 // is so a broken transformer doesn't take down the whole graph. 82 // is so a broken transformer doesn't take down the whole graph.
74 var info = new TransformInfo(transformer, input.id); 83 var info = new TransformInfo(transformer, input.id);
75 if (error is! AssetNotFoundException) { 84 if (error is! AssetNotFoundException) {
76 error = new TransformerException(info, error, stackTrace); 85 error = new TransformerException(info, error, stackTrace);
77 } else { 86 } else {
78 error = new MissingInputException(info, error.id); 87 error = new MissingInputException(info, error.id);
79 } 88 }
80 _phase.cascade.reportError(error); 89 phase.cascade.reportError(error);
81 90
82 return false; 91 return null;
83 }).then((key) { 92 }).then((key) {
84 if (input.state.isRemoved) return; 93 if (input.state.isRemoved) return;
85 if (key == null) { 94 if (key == null) {
86 var forwarder = new AssetForwarder(input); 95 var forwarder = new AssetForwarder(input);
87 _passThroughForwarders.add(forwarder); 96 _passThroughForwarders.add(forwarder);
88 forwarder.node.whenRemoved( 97 forwarder.node.whenRemoved(
89 () => _passThroughForwarders.remove(forwarder)); 98 () => _passThroughForwarders.remove(forwarder));
90 _streams.onAssetController.add(forwarder.node); 99 _streams.onAssetController.add(forwarder.node);
91 } else if (_transforms.containsKey(key)) { 100 } else if (_transforms.containsKey(key)) {
92 _transforms[key].addPrimary(input); 101 _transforms[key].addPrimary(input);
93 } else { 102 } else {
94 var transform = new TransformNode(_phase, transformer, key, _location); 103 var transform = new TransformNode(this, transformer, key, _location);
95 _transforms[key] = transform; 104 _transforms[key] = transform;
96 105
97 transform.onStatusChange.listen( 106 transform.onStatusChange.listen(
98 (_) => _streams.changeStatus(status), 107 (_) => _streams.changeStatus(status),
99 onDone: () => _transforms.remove(input.id.path)); 108 onDone: () {
109 _transforms.remove(transform.key);
110 if (!_streams.isClosed) _streams.changeStatus(status);
111 });
100 112
101 _streams.onAssetPool.add(transform.onAsset); 113 _streams.onAssetPool.add(transform.onAsset);
102 _streams.onLogPool.add(transform.onLog); 114 _streams.onLogPool.add(transform.onLog);
103 transform.addPrimary(input); 115 transform.addPrimary(input);
104 } 116 }
105 }).whenComplete(() { 117 }).whenComplete(() {
106 _activeClassifications--; 118 _activeClassifications--;
107 if (!_streams.isClosed) _streams.changeStatus(status); 119 if (_streams.isClosed) return;
120 if (!isClassifying) _onDoneClassifyingController.add(null);
121 _streams.changeStatus(status);
108 }); 122 });
109 } 123 }
110 124
111 /// Removes this transformer. 125 /// Removes this transformer.
112 /// 126 ///
113 /// This marks all outputs of the transformer as removed. 127 /// This marks all outputs of the transformer as removed.
114 void remove() { 128 void remove() {
115 _streams.close(); 129 _streams.close();
130 _onDoneClassifyingController.close();
116 for (var transform in _transforms.values.toList()) { 131 for (var transform in _transforms.values.toList()) {
117 transform.remove(); 132 transform.remove();
118 } 133 }
119 for (var forwarder in _passThroughForwarders.toList()) { 134 for (var forwarder in _passThroughForwarders.toList()) {
120 forwarder.close(); 135 forwarder.close();
121 } 136 }
122 } 137 }
123 138
124 /// Force all deferred transforms to begin producing concrete assets. 139 /// Force all deferred transforms to begin producing concrete assets.
125 void forceAllTransforms() { 140 void forceAllTransforms() {
126 for (var transform in _transforms.values) { 141 for (var transform in _transforms.values) {
127 transform.force(); 142 transform.force();
128 } 143 }
129 } 144 }
130 145
131 String toString() => "classifier in $_location for $transformer"; 146 String toString() => "classifier in $_location for $transformer";
132 } 147 }
OLDNEW
« no previous file with comments | « pkg/barback/lib/src/graph/transform_node.dart ('k') | pkg/barback/lib/src/transformer/aggregate_transform.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698