| OLD | NEW |
| 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/transformer.dart'; | 13 import '../transformer/aggregate_transformer.dart'; |
| 14 import '../transformer/wrapping_aggregate_transformer.dart'; |
| 14 import '../utils.dart'; | 15 import '../utils.dart'; |
| 15 import 'node_status.dart'; | 16 import 'node_status.dart'; |
| 16 import 'node_streams.dart'; | 17 import 'node_streams.dart'; |
| 17 import 'phase.dart'; | 18 import 'phase.dart'; |
| 18 import 'transform_node.dart'; | 19 import 'transform_node.dart'; |
| 19 | 20 |
| 20 /// 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 |
| 21 /// its `classifyPrimary` method. | 22 /// its [AggregateTransformer.classifyPrimary] method. |
| 22 /// | 23 /// |
| 23 /// This is used for non-aggregate transformers; they're modeled as aggregate | 24 /// This is also used for non-aggregate transformers; they're modeled as |
| 24 /// transformers that return the primary path if `isPrimary` is true and `null` | 25 /// aggregate transformers that return the primary path if `isPrimary` is true |
| 25 /// if `isPrimary` is `null`. | 26 /// and `null` if `isPrimary` is `null`. |
| 26 class TransformerClassifier { | 27 class TransformerClassifier { |
| 27 /// The containing [Phase]. | 28 /// The containing [Phase]. |
| 28 final Phase _phase; | 29 final Phase _phase; |
| 29 | 30 |
| 30 /// The [Transformer] to use to classify the inputs. | 31 /// The [AggregateTransformer] used to classify the inputs. |
| 31 final Transformer transformer; | 32 final AggregateTransformer transformer; |
| 32 | 33 |
| 33 /// A string describing the location of [this] in the transformer graph. | 34 /// A string describing the location of [this] in the transformer graph. |
| 34 final String _location; | 35 final String _location; |
| 35 | 36 |
| 36 /// The individual transforms for each classiciation key. | 37 /// The individual transforms for each classiciation key. |
| 37 final _transforms = new Map<Object, TransformNode>(); | 38 final _transforms = new Map<Object, TransformNode>(); |
| 38 | 39 |
| 39 /// 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]. |
| 40 final _passThroughForwarders = new Set<AssetForwarder>(); | 41 final _passThroughForwarders = new Set<AssetForwarder>(); |
| 41 | 42 |
| 42 /// The streams exposed by this classifier. | 43 /// The streams exposed by this classifier. |
| 43 final _streams = new NodeStreams(); | 44 final _streams = new NodeStreams(); |
| 44 Stream get onStatusChange => _streams.onStatusChange; | 45 Stream get onStatusChange => _streams.onStatusChange; |
| 45 Stream<AssetNode> get onAsset => _streams.onAsset; | 46 Stream<AssetNode> get onAsset => _streams.onAsset; |
| 46 Stream<LogEntry> get onLog => _streams.onLog; | 47 Stream<LogEntry> get onLog => _streams.onLog; |
| 47 | 48 |
| 48 /// The number of currently-active calls to [transformer.isPrimary]. | 49 /// The number of currently-active calls to [transformer.classifyPrimary]. |
| 49 /// | 50 /// |
| 50 /// This is used to determine whether [this] is dirty. | 51 /// This is used to determine whether [this] is dirty. |
| 51 var _activeIsPrimaries = 0; | 52 var _activeClassifications = 0; |
| 52 | 53 |
| 53 /// How far along [this] is in processing its assets. | 54 /// How far along [this] is in processing its assets. |
| 54 NodeStatus get status { | 55 NodeStatus get status { |
| 55 if (_activeIsPrimaries > 0) return NodeStatus.RUNNING; | 56 if (_activeClassifications > 0) return NodeStatus.RUNNING; |
| 56 return NodeStatus.dirtiest( | 57 return NodeStatus.dirtiest( |
| 57 _transforms.values.map((transform) => transform.status)); | 58 _transforms.values.map((transform) => transform.status)); |
| 58 } | 59 } |
| 59 | 60 |
| 60 TransformerClassifier(this._phase, this.transformer, this._location); | 61 TransformerClassifier(this._phase, transformer, this._location) |
| 62 : transformer = transformer is AggregateTransformer ? |
| 63 transformer : new WrappingAggregateTransformer(transformer); |
| 61 | 64 |
| 62 /// Adds a new asset as an input for this transformer. | 65 /// Adds a new asset as an input for this transformer. |
| 63 void addInput(AssetNode input) { | 66 void addInput(AssetNode input) { |
| 64 _activeIsPrimaries++; | 67 _activeClassifications++; |
| 65 syncFuture(() => transformer.isPrimary(input.id)).catchError( | 68 syncFuture(() => transformer.classifyPrimary(input.id)).catchError( |
| 66 (error, stackTrace) { | 69 (error, stackTrace) { |
| 67 if (input.state.isRemoved) return false; | 70 if (input.state.isRemoved) return false; |
| 68 | 71 |
| 69 // Catch all transformer errors and pipe them to the results stream. This | 72 // Catch all transformer errors and pipe them to the results stream. This |
| 70 // is so a broken transformer doesn't take down the whole graph. | 73 // is so a broken transformer doesn't take down the whole graph. |
| 71 var info = new TransformInfo(transformer, input.id); | 74 var info = new TransformInfo(transformer, input.id); |
| 72 if (error is! AssetNotFoundException) { | 75 if (error is! AssetNotFoundException) { |
| 73 error = new TransformerException(info, error, stackTrace); | 76 error = new TransformerException(info, error, stackTrace); |
| 74 } else { | 77 } else { |
| 75 error = new MissingInputException(info, error.id); | 78 error = new MissingInputException(info, error.id); |
| 76 } | 79 } |
| 77 _phase.cascade.reportError(error); | 80 _phase.cascade.reportError(error); |
| 78 | 81 |
| 79 return false; | 82 return false; |
| 80 }).then((isPrimary) { | 83 }).then((key) { |
| 81 if (input.state.isRemoved) return; | 84 if (input.state.isRemoved) return; |
| 82 if (!isPrimary) { | 85 if (key == null) { |
| 83 var forwarder = new AssetForwarder(input); | 86 var forwarder = new AssetForwarder(input); |
| 84 _passThroughForwarders.add(forwarder); | 87 _passThroughForwarders.add(forwarder); |
| 85 forwarder.node.whenRemoved( | 88 forwarder.node.whenRemoved( |
| 86 () => _passThroughForwarders.remove(forwarder)); | 89 () => _passThroughForwarders.remove(forwarder)); |
| 87 _streams.onAssetController.add(forwarder.node); | 90 _streams.onAssetController.add(forwarder.node); |
| 91 } else if (_transforms.containsKey(key)) { |
| 92 _transforms[key].addPrimary(input); |
| 88 } else { | 93 } else { |
| 89 var transform = new TransformNode( | 94 var transform = new TransformNode(_phase, transformer, key, _location); |
| 90 _phase, transformer, input, _location); | 95 _transforms[key] = transform; |
| 91 _transforms[input.id.path] = transform; | |
| 92 | 96 |
| 93 transform.onStatusChange.listen( | 97 transform.onStatusChange.listen( |
| 94 (_) => _streams.changeStatus(status), | 98 (_) => _streams.changeStatus(status), |
| 95 onDone: () => _transforms.remove(input.id.path)); | 99 onDone: () => _transforms.remove(input.id.path)); |
| 96 | 100 |
| 97 _streams.onAssetPool.add(transform.onAsset); | 101 _streams.onAssetPool.add(transform.onAsset); |
| 98 _streams.onLogPool.add(transform.onLog); | 102 _streams.onLogPool.add(transform.onLog); |
| 103 transform.addPrimary(input); |
| 99 } | 104 } |
| 100 }).whenComplete(() { | 105 }).whenComplete(() { |
| 101 _activeIsPrimaries--; | 106 _activeClassifications--; |
| 102 if (!_streams.isClosed) _streams.changeStatus(status); | 107 if (!_streams.isClosed) _streams.changeStatus(status); |
| 103 }); | 108 }); |
| 104 } | 109 } |
| 105 | 110 |
| 106 /// Removes this transformer. | 111 /// Removes this transformer. |
| 107 /// | 112 /// |
| 108 /// This marks all outputs of the transformer as removed. | 113 /// This marks all outputs of the transformer as removed. |
| 109 void remove() { | 114 void remove() { |
| 110 _streams.close(); | 115 _streams.close(); |
| 111 for (var transform in _transforms.values.toList()) { | 116 for (var transform in _transforms.values.toList()) { |
| 112 transform.remove(); | 117 transform.remove(); |
| 113 } | 118 } |
| 114 for (var forwarder in _passThroughForwarders.toList()) { | 119 for (var forwarder in _passThroughForwarders.toList()) { |
| 115 forwarder.close(); | 120 forwarder.close(); |
| 116 } | 121 } |
| 117 } | 122 } |
| 118 | 123 |
| 119 /// Force all deferred transforms to begin producing concrete assets. | 124 /// Force all deferred transforms to begin producing concrete assets. |
| 120 void forceAllTransforms() { | 125 void forceAllTransforms() { |
| 121 for (var transform in _transforms.values) { | 126 for (var transform in _transforms.values) { |
| 122 transform.force(); | 127 transform.force(); |
| 123 } | 128 } |
| 124 } | 129 } |
| 125 | 130 |
| 126 String toString() => "classifier in $_location for $transformer"; | 131 String toString() => "classifier in $_location for $transformer"; |
| 127 } | 132 } |
| OLD | NEW |