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