| 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.transform_node; | 5 library barback.transform_node; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'asset.dart'; | 9 import 'asset.dart'; |
| 10 import 'asset_id.dart'; | 10 import 'asset_id.dart'; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 TransformNode(this.phase, this.transformer, this.primary) { | 57 TransformNode(this.phase, this.transformer, this.primary) { |
| 58 _primarySubscription = primary.onStateChange.listen((state) { | 58 _primarySubscription = primary.onStateChange.listen((state) { |
| 59 if (state.isRemoved) { | 59 if (state.isRemoved) { |
| 60 remove(); | 60 remove(); |
| 61 } else { | 61 } else { |
| 62 _dirty(); | 62 _dirty(); |
| 63 } | 63 } |
| 64 }); | 64 }); |
| 65 } | 65 } |
| 66 | 66 |
| 67 /// The [TransformInfo] describing this node. |
| 68 /// |
| 69 /// [TransformInfo] is the publicly-visible representation of a transform |
| 70 /// node. |
| 71 TransformInfo get info => new TransformInfo(transformer, primary.id); |
| 72 |
| 67 /// Marks this transform as removed. | 73 /// Marks this transform as removed. |
| 68 /// | 74 /// |
| 69 /// This causes all of the transform's outputs to be marked as removed as | 75 /// This causes all of the transform's outputs to be marked as removed as |
| 70 /// well. Normally this will be automatically done internally based on events | 76 /// well. Normally this will be automatically done internally based on events |
| 71 /// from the primary input, but it's possible for a transform to no longer be | 77 /// from the primary input, but it's possible for a transform to no longer be |
| 72 /// valid even if its primary input still exists. | 78 /// valid even if its primary input still exists. |
| 73 void remove() { | 79 void remove() { |
| 74 _isDirty = true; | 80 _isDirty = true; |
| 75 _onDirtyController.close(); | 81 _onDirtyController.close(); |
| 76 _primarySubscription.cancel(); | 82 _primarySubscription.cancel(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 subscription.cancel(); | 115 subscription.cancel(); |
| 110 } | 116 } |
| 111 _inputSubscriptions.clear(); | 117 _inputSubscriptions.clear(); |
| 112 | 118 |
| 113 _isDirty = false; | 119 _isDirty = false; |
| 114 return transformer.apply(transform).catchError((error) { | 120 return transformer.apply(transform).catchError((error) { |
| 115 // If the transform became dirty while processing, ignore any errors from | 121 // If the transform became dirty while processing, ignore any errors from |
| 116 // it. | 122 // it. |
| 117 if (_isDirty) return; | 123 if (_isDirty) return; |
| 118 | 124 |
| 125 if (error is! MissingInputException) { |
| 126 error = new TransformerException(info, error); |
| 127 } |
| 128 |
| 119 // Catch all transformer errors and pipe them to the results stream. | 129 // Catch all transformer errors and pipe them to the results stream. |
| 120 // This is so a broken transformer doesn't take down the whole graph. | 130 // This is so a broken transformer doesn't take down the whole graph. |
| 121 phase.cascade.reportError(error); | 131 phase.cascade.reportError(error); |
| 122 | 132 |
| 123 // Don't allow partial results from a failed transform. | 133 // Don't allow partial results from a failed transform. |
| 124 newOutputs.clear(); | 134 newOutputs.clear(); |
| 125 }).then((_) { | 135 }).then((_) { |
| 126 if (_isDirty) return []; | 136 if (_isDirty) return []; |
| 127 | 137 |
| 128 return _adjustOutputs(newOutputs); | 138 return _adjustOutputs(newOutputs); |
| 129 }); | 139 }); |
| 130 } | 140 } |
| 131 | 141 |
| 132 /// Gets the asset for an input [id]. | 142 /// Gets the asset for an input [id]. |
| 133 /// | 143 /// |
| 134 /// If an input with that ID cannot be found, throws an | 144 /// If an input with that ID cannot be found, throws an |
| 135 /// [AssetNotFoundException]. | 145 /// [AssetNotFoundException]. |
| 136 Future<Asset> getInput(AssetId id) { | 146 Future<Asset> getInput(AssetId id) { |
| 137 return phase.getInput(id).then((node) { | 147 return phase.getInput(id).then((node) { |
| 138 // Throw if the input isn't found. This ensures the transformer's apply | 148 // Throw if the input isn't found. This ensures the transformer's apply |
| 139 // is exited. We'll then catch this and report it through the proper | 149 // is exited. We'll then catch this and report it through the proper |
| 140 // results stream. | 150 // results stream. |
| 141 if (node == null) throw new MissingInputException(id); | 151 if (node == null) throw new MissingInputException(info, id); |
| 142 | 152 |
| 143 // If the asset node is found, wait until its contents are actually | 153 // If the asset node is found, wait until its contents are actually |
| 144 // available before we return them. | 154 // available before we return them. |
| 145 return node.whenAvailable.then((asset) { | 155 return node.whenAvailable.then((asset) { |
| 146 _inputSubscriptions.putIfAbsent(node.id, | 156 _inputSubscriptions.putIfAbsent(node.id, |
| 147 () => node.onStateChange.listen((_) => _dirty())); | 157 () => node.onStateChange.listen((_) => _dirty())); |
| 148 | 158 |
| 149 return asset; | 159 return asset; |
| 150 }).catchError((error) { | 160 }).catchError((error) { |
| 151 if (error is! AssetNotFoundException || error.id != id) throw error; | 161 if (error is! AssetNotFoundException || error.id != id) throw error; |
| 152 // If the node was removed before it could be loaded, treat it as though | 162 // If the node was removed before it could be loaded, treat it as though |
| 153 // it never existed and throw a MissingInputException. | 163 // it never existed and throw a MissingInputException. |
| 154 throw new MissingInputException(id); | 164 throw new MissingInputException(info, id); |
| 155 }); | 165 }); |
| 156 }); | 166 }); |
| 157 } | 167 } |
| 158 | 168 |
| 159 /// Adjusts the outputs of the transform to reflect the outputs emitted on its | 169 /// Adjusts the outputs of the transform to reflect the outputs emitted on its |
| 160 /// most recent run. | 170 /// most recent run. |
| 161 Set<AssetNode> _adjustOutputs(AssetSet newOutputs) { | 171 Set<AssetNode> _adjustOutputs(AssetSet newOutputs) { |
| 162 // Any ids that are for a different package are invalid. | 172 // Any ids that are for a different package are invalid. |
| 163 var invalidIds = newOutputs | 173 var invalidIds = newOutputs |
| 164 .map((asset) => asset.id) | 174 .map((asset) => asset.id) |
| 165 .where((id) => id.package != phase.cascade.package) | 175 .where((id) => id.package != phase.cascade.package) |
| 166 .toSet(); | 176 .toSet(); |
| 167 for (var id in invalidIds) { | 177 for (var id in invalidIds) { |
| 168 newOutputs.removeId(id); | 178 newOutputs.removeId(id); |
| 169 // TODO(nweiz): report this as a warning rather than a failing error. | 179 // TODO(nweiz): report this as a warning rather than a failing error. |
| 170 phase.cascade.reportError( | 180 phase.cascade.reportError(new InvalidOutputException(info, id)); |
| 171 new InvalidOutputException(phase.cascade.package, id)); | |
| 172 } | 181 } |
| 173 | 182 |
| 174 // Remove outputs that used to exist but don't anymore. | 183 // Remove outputs that used to exist but don't anymore. |
| 175 for (var id in _outputControllers.keys.toList()) { | 184 for (var id in _outputControllers.keys.toList()) { |
| 176 if (newOutputs.containsId(id)) continue; | 185 if (newOutputs.containsId(id)) continue; |
| 177 _outputControllers.remove(id).setRemoved(); | 186 _outputControllers.remove(id).setRemoved(); |
| 178 } | 187 } |
| 179 | 188 |
| 180 var brandNewOutputs = new Set<AssetNode>(); | 189 var brandNewOutputs = new Set<AssetNode>(); |
| 181 // Store any new outputs or new contents for existing outputs. | 190 // Store any new outputs or new contents for existing outputs. |
| 182 for (var asset in newOutputs) { | 191 for (var asset in newOutputs) { |
| 183 var controller = _outputControllers[asset.id]; | 192 var controller = _outputControllers[asset.id]; |
| 184 if (controller != null) { | 193 if (controller != null) { |
| 185 controller.setAvailable(asset); | 194 controller.setAvailable(asset); |
| 186 } else { | 195 } else { |
| 187 var controller = new AssetNodeController.available(asset); | 196 var controller = new AssetNodeController.available(asset); |
| 188 _outputControllers[asset.id] = controller; | 197 _outputControllers[asset.id] = controller; |
| 189 brandNewOutputs.add(controller.node); | 198 brandNewOutputs.add(controller.node); |
| 190 } | 199 } |
| 191 } | 200 } |
| 192 | 201 |
| 193 return brandNewOutputs; | 202 return brandNewOutputs; |
| 194 } | 203 } |
| 195 } | 204 } |
| OLD | NEW |