OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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.asset_cascade; | 5 library barback.asset_cascade; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'asset.dart'; | 10 import 'asset.dart'; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 /// | 77 /// |
78 /// If no build it in progress, is `null`. | 78 /// If no build it in progress, is `null`. |
79 Future _processDone; | 79 Future _processDone; |
80 | 80 |
81 /// Whether any source assets have been updated or removed since processing | 81 /// Whether any source assets have been updated or removed since processing |
82 /// last began. | 82 /// last began. |
83 var _newChanges = false; | 83 var _newChanges = false; |
84 | 84 |
85 /// Creates a new [AssetCascade]. | 85 /// Creates a new [AssetCascade]. |
86 /// | 86 /// |
87 /// It loads source assets within [package] using [provider] and then uses | 87 /// It loads source assets within [package] using [provider]. |
88 /// [transformerPhases] to generate output files from them. | |
89 //TODO(rnystrom): Better way of specifying transformers and their ordering. | 88 //TODO(rnystrom): Better way of specifying transformers and their ordering. |
Bob Nystrom
2013/08/20 19:08:54
This is TODONE!
nweiz
2013/08/20 21:39:17
Done.
| |
90 AssetCascade(this.graph, this.package, | 89 AssetCascade(this.graph, this.package) { |
91 Iterable<Iterable<Transformer>> transformerPhases) { | 90 _addPhase(new Phase(this, [])); |
92 // Flatten the phases to a list so we can traverse backwards to wire up | |
93 // each phase to its next. | |
94 var phases = transformerPhases.toList(); | |
95 if (phases.isEmpty) phases = [[]]; | |
96 | |
97 Phase nextPhase = null; | |
98 for (var transformers in phases.reversed) { | |
99 nextPhase = new Phase(this, transformers.toList(), nextPhase); | |
100 nextPhase.onDirty.listen((_) { | |
101 _newChanges = true; | |
102 _waitForProcess(); | |
103 }); | |
104 _phases.insert(0, nextPhase); | |
105 } | |
106 } | 91 } |
107 | 92 |
108 /// Gets the asset identified by [id]. | 93 /// Gets the asset identified by [id]. |
109 /// | 94 /// |
110 /// If [id] is for a generated or transformed asset, this will wait until it | 95 /// If [id] is for a generated or transformed asset, this will wait until it |
111 /// has been created and return it. This means that the returned asset will | 96 /// has been created and return it. This means that the returned asset will |
112 /// always be [AssetState.AVAILABLE]. | 97 /// always be [AssetState.AVAILABLE]. |
113 /// | 98 /// |
114 /// If the asset cannot be found, returns null. | 99 /// If the asset cannot be found, returns null. |
115 Future<AssetNode> getAssetNode(AssetId id) { | 100 Future<AssetNode> getAssetNode(AssetId id) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 removed.forEach((id) { | 163 removed.forEach((id) { |
179 // If the source was being loaded, cancel that load. | 164 // If the source was being loaded, cancel that load. |
180 if (_loadingSources.containsKey(id)) _loadingSources.remove(id).cancel(); | 165 if (_loadingSources.containsKey(id)) _loadingSources.remove(id).cancel(); |
181 | 166 |
182 var controller = _sourceControllerMap.remove(id); | 167 var controller = _sourceControllerMap.remove(id); |
183 // Don't choke if an id is double-removed for some reason. | 168 // Don't choke if an id is double-removed for some reason. |
184 if (controller != null) controller.setRemoved(); | 169 if (controller != null) controller.setRemoved(); |
185 }); | 170 }); |
186 } | 171 } |
187 | 172 |
173 /// Sets this cascade's transformer phases to [transformers]. | |
174 void updateTransformers(Iterable<Iterable<Transformer>> transformers) { | |
175 transformers = transformers.toList(); | |
176 | |
177 for (var i = 0; i < transformers.length; i++) { | |
178 if (_phases.length > i) { | |
179 _phases[i].updateTransformers(transformers[i]); | |
180 continue; | |
181 } | |
182 | |
183 _addPhase(_phases.last.addPhase(transformers[i])); | |
184 } | |
185 | |
186 if (transformers.length < _phases.length) { | |
187 for (var i = transformers.length; i < _phases.length; i++) { | |
188 // TODO(nweiz): actually remove phases rather than emptying them of | |
189 // transformers. | |
190 _phases[i].updateTransformers([]); | |
191 } | |
192 } | |
193 } | |
194 | |
188 void reportError(BarbackException error) { | 195 void reportError(BarbackException error) { |
189 _accumulatedErrors.add(error); | 196 _accumulatedErrors.add(error); |
190 _errorsController.add(error); | 197 _errorsController.add(error); |
191 } | 198 } |
192 | 199 |
200 /// Add [phase] to the end of [_phases] and watch its [onDirty] stream. | |
201 void _addPhase(Phase phase) { | |
202 phase.onDirty.listen((_) { | |
203 _newChanges = true; | |
204 _waitForProcess(); | |
205 }); | |
206 _phases.add(phase); | |
207 } | |
208 | |
193 /// Starts the build process asynchronously if there is work to be done. | 209 /// Starts the build process asynchronously if there is work to be done. |
194 /// | 210 /// |
195 /// Returns a future that completes with the background processing is done. | 211 /// Returns a future that completes with the background processing is done. |
196 /// If there is no work to do, returns a future that completes immediately. | 212 /// If there is no work to do, returns a future that completes immediately. |
197 /// All errors that occur during processing will be caught (and routed to the | 213 /// All errors that occur during processing will be caught (and routed to the |
198 /// [results] stream) before they get to the returned future, so it is safe | 214 /// [results] stream) before they get to the returned future, so it is safe |
199 /// to discard it. | 215 /// to discard it. |
200 Future _waitForProcess() { | 216 Future _waitForProcess() { |
201 if (_processDone != null) return _processDone; | 217 if (_processDone != null) return _processDone; |
202 | 218 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 | 257 |
242 // Otherwise, everything is done. | 258 // Otherwise, everything is done. |
243 return; | 259 return; |
244 } | 260 } |
245 | 261 |
246 // Process that phase and then loop onto the next. | 262 // Process that phase and then loop onto the next. |
247 return future.then((_) => _process()); | 263 return future.then((_) => _process()); |
248 }); | 264 }); |
249 } | 265 } |
250 } | 266 } |
OLD | NEW |