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'; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 /// The streams exposed by this classifier. | 42 /// The streams exposed by this classifier. |
43 final _streams = new NodeStreams(); | 43 final _streams = new NodeStreams(); |
44 Stream get onStatusChange => _streams.onStatusChange; | 44 Stream get onStatusChange => _streams.onStatusChange; |
45 Stream<AssetNode> get onAsset => _streams.onAsset; | 45 Stream<AssetNode> get onAsset => _streams.onAsset; |
46 Stream<LogEntry> get onLog => _streams.onLog; | 46 Stream<LogEntry> get onLog => _streams.onLog; |
47 | 47 |
48 /// A broadcast stream that emits an event whenever [this] has finished | 48 /// A broadcast stream that emits an event whenever [this] has finished |
49 /// classifying all available inputs. | 49 /// classifying all available inputs. |
50 Stream get onDoneClassifying => _onDoneClassifyingController.stream; | 50 Stream get onDoneClassifying => _onDoneClassifyingController.stream; |
51 final _onDoneClassifyingController = | 51 final _onDoneClassifyingController = |
52 new StreamController.broadcast(sync: true); | 52 new StreamController<Null>.broadcast(sync: true); |
53 | 53 |
54 /// The number of currently-active calls to [transformer.classifyPrimary]. | 54 /// The number of currently-active calls to [transformer.classifyPrimary]. |
55 /// | 55 /// |
56 /// This is used to determine whether [this] is dirty. | 56 /// This is used to determine whether [this] is dirty. |
57 var _activeClassifications = 0; | 57 var _activeClassifications = 0; |
58 | 58 |
59 /// Whether this is currently classifying any inputs. | 59 /// Whether this is currently classifying any inputs. |
60 bool get isClassifying => _activeClassifications > 0; | 60 bool get isClassifying => _activeClassifications > 0; |
61 | 61 |
62 /// How far along [this] is in processing its assets. | 62 /// How far along [this] is in processing its assets. |
63 NodeStatus get status { | 63 NodeStatus get status { |
64 if (isClassifying) return NodeStatus.RUNNING; | 64 if (isClassifying) return NodeStatus.RUNNING; |
65 return NodeStatus.dirtiest( | 65 return NodeStatus |
66 _transforms.values.map((transform) => transform.status)); | 66 .dirtiest(_transforms.values.map((transform) => transform.status)); |
67 } | 67 } |
68 | 68 |
69 TransformerClassifier(this.phase, transformer, this._location) | 69 TransformerClassifier(this.phase, transformer, this._location) |
70 : transformer = transformer is AggregateTransformer ? | 70 : transformer = transformer is AggregateTransformer |
71 transformer : new WrappingAggregateTransformer(transformer); | 71 ? transformer |
| 72 : new WrappingAggregateTransformer(transformer); |
72 | 73 |
73 /// Adds a new asset as an input for this transformer. | 74 /// Adds a new asset as an input for this transformer. |
74 void addInput(AssetNode input) { | 75 void addInput(AssetNode input) { |
75 _activeClassifications++; | 76 _activeClassifications++; |
76 new Future.sync(() => transformer.classifyPrimary(input.id)) | 77 new Future.sync(() => transformer.classifyPrimary(input.id)) |
77 .catchError((error, stackTrace) { | 78 .catchError((error, stackTrace) { |
78 if (input.state.isRemoved) return null; | 79 if (input.state.isRemoved) return null; |
79 | 80 |
80 // 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 |
81 // 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. |
82 var info = new TransformInfo(transformer, input.id); | 83 var info = new TransformInfo(transformer, input.id); |
83 if (error is! AssetNotFoundException) { | 84 if (error is! AssetNotFoundException) { |
84 error = new TransformerException(info, error, stackTrace); | 85 error = new TransformerException(info, error, stackTrace); |
85 } else { | 86 } else { |
86 error = new MissingInputException(info, error.id); | 87 error = new MissingInputException(info, error.id); |
87 } | 88 } |
88 phase.cascade.reportError(error); | 89 phase.cascade.reportError(error); |
89 | 90 |
90 return null; | 91 return null; |
91 }).then((key) { | 92 }).then((key) { |
92 if (input.state.isRemoved) return; | 93 if (input.state.isRemoved) return; |
93 if (key == null) { | 94 if (key == null) { |
94 var forwarder = new AssetForwarder(input); | 95 var forwarder = new AssetForwarder(input); |
95 _passThroughForwarders.add(forwarder); | 96 _passThroughForwarders.add(forwarder); |
96 forwarder.node.whenRemoved( | 97 forwarder.node |
97 () => _passThroughForwarders.remove(forwarder)); | 98 .whenRemoved(() => _passThroughForwarders.remove(forwarder)); |
98 _streams.onAssetController.add(forwarder.node); | 99 _streams.onAssetController.add(forwarder.node); |
99 } else if (_transforms.containsKey(key)) { | 100 } else if (_transforms.containsKey(key)) { |
100 _transforms[key].addPrimary(input); | 101 _transforms[key].addPrimary(input); |
101 } else { | 102 } else { |
102 var transform = new TransformNode(this, transformer, key, _location); | 103 var transform = new TransformNode(this, transformer, key, _location); |
103 _transforms[key] = transform; | 104 _transforms[key] = transform; |
104 | 105 |
105 transform.onStatusChange.listen( | 106 transform.onStatusChange.listen((_) => _streams.changeStatus(status), |
106 (_) => _streams.changeStatus(status), | |
107 onDone: () { | 107 onDone: () { |
108 _transforms.remove(transform.key); | 108 _transforms.remove(transform.key); |
109 if (!_streams.isClosed) _streams.changeStatus(status); | 109 if (!_streams.isClosed) _streams.changeStatus(status); |
110 }); | 110 }); |
111 | 111 |
112 _streams.onAssetPool.add(transform.onAsset); | 112 _streams.onAssetPool.add(transform.onAsset); |
113 _streams.onLogPool.add(transform.onLog); | 113 _streams.onLogPool.add(transform.onLog); |
114 transform.addPrimary(input); | 114 transform.addPrimary(input); |
115 } | 115 } |
116 }).whenComplete(() { | 116 }).whenComplete(() { |
(...skipping 20 matching lines...) Expand all Loading... |
137 | 137 |
138 /// Force all deferred transforms to begin producing concrete assets. | 138 /// Force all deferred transforms to begin producing concrete assets. |
139 void forceAllTransforms() { | 139 void forceAllTransforms() { |
140 for (var transform in _transforms.values) { | 140 for (var transform in _transforms.values) { |
141 transform.force(); | 141 transform.force(); |
142 } | 142 } |
143 } | 143 } |
144 | 144 |
145 String toString() => "classifier in $_location for $transformer"; | 145 String toString() => "classifier in $_location for $transformer"; |
146 } | 146 } |
OLD | NEW |