| 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 27 matching lines...) Expand all Loading... |
| 38 /// began running. | 38 /// began running. |
| 39 bool get isDirty => _isDirty; | 39 bool get isDirty => _isDirty; |
| 40 var _isDirty = true; | 40 var _isDirty = true; |
| 41 | 41 |
| 42 /// The subscriptions to each input's [AssetNode.onStateChange] stream. | 42 /// The subscriptions to each input's [AssetNode.onStateChange] stream. |
| 43 var _inputSubscriptions = new Map<AssetId, StreamSubscription>(); | 43 var _inputSubscriptions = new Map<AssetId, StreamSubscription>(); |
| 44 | 44 |
| 45 /// The controllers for the asset nodes emitted by this node. | 45 /// The controllers for the asset nodes emitted by this node. |
| 46 var _outputControllers = new Map<AssetId, AssetNodeController>(); | 46 var _outputControllers = new Map<AssetId, AssetNodeController>(); |
| 47 | 47 |
| 48 /// A stream that emits an event whenever this transform becomes dirty and |
| 49 /// needs to be re-run. |
| 50 /// |
| 51 /// This may emit events when the transform was already dirty or while |
| 52 /// processing transforms. Events are emitted synchronously to ensure that the |
| 53 /// dirty state is thoroughly propagated as soon as any assets are changed. |
| 54 Stream get onDirty => _onDirtyController.stream; |
| 55 final _onDirtyController = new StreamController.broadcast(sync: true); |
| 56 |
| 48 TransformNode(this.phase, this.transformer, this.primary) { | 57 TransformNode(this.phase, this.transformer, this.primary) { |
| 49 _primarySubscription = primary.onStateChange.listen((state) { | 58 _primarySubscription = primary.onStateChange.listen((state) { |
| 50 if (state.isRemoved) { | 59 if (state.isRemoved) { |
| 51 remove(); | 60 remove(); |
| 52 } else { | 61 } else { |
| 53 _dirty(); | 62 _dirty(); |
| 54 } | 63 } |
| 55 }); | 64 }); |
| 56 } | 65 } |
| 57 | 66 |
| 58 /// Marks this transform as removed. | 67 /// Marks this transform as removed. |
| 59 /// | 68 /// |
| 60 /// This causes all of the transform's outputs to be marked as removed as | 69 /// This causes all of the transform's outputs to be marked as removed as |
| 61 /// well. Normally this will be automatically done internally based on events | 70 /// well. Normally this will be automatically done internally based on events |
| 62 /// from the primary input, but it's possible for a transform to no longer be | 71 /// from the primary input, but it's possible for a transform to no longer be |
| 63 /// valid even if its primary input still exists. | 72 /// valid even if its primary input still exists. |
| 64 void remove() { | 73 void remove() { |
| 65 _isDirty = true; | 74 _isDirty = true; |
| 75 _onDirtyController.close(); |
| 66 _primarySubscription.cancel(); | 76 _primarySubscription.cancel(); |
| 67 for (var subscription in _inputSubscriptions.values) { | 77 for (var subscription in _inputSubscriptions.values) { |
| 68 subscription.cancel(); | 78 subscription.cancel(); |
| 69 } | 79 } |
| 70 for (var controller in _outputControllers.values) { | 80 for (var controller in _outputControllers.values) { |
| 71 controller.setRemoved(); | 81 controller.setRemoved(); |
| 72 } | 82 } |
| 73 } | 83 } |
| 74 | 84 |
| 75 /// Marks this transform as dirty. | 85 /// Marks this transform as dirty. |
| 76 /// | 86 /// |
| 77 /// This causes all of the transform's outputs to be marked as dirty as well. | 87 /// This causes all of the transform's outputs to be marked as dirty as well. |
| 78 void _dirty() { | 88 void _dirty() { |
| 79 _isDirty = true; | 89 _isDirty = true; |
| 80 for (var controller in _outputControllers.values) { | 90 for (var controller in _outputControllers.values) { |
| 81 controller.setDirty(); | 91 controller.setDirty(); |
| 82 } | 92 } |
| 93 _onDirtyController.add(null); |
| 83 } | 94 } |
| 84 | 95 |
| 85 /// Applies this transform. | 96 /// Applies this transform. |
| 86 /// | 97 /// |
| 87 /// Returns a set of asset nodes representing the outputs from this transform | 98 /// Returns a set of asset nodes representing the outputs from this transform |
| 88 /// that weren't emitted last time it was run. | 99 /// that weren't emitted last time it was run. |
| 89 Future<Set<AssetNode>> apply() { | 100 Future<Set<AssetNode>> apply() { |
| 90 var newOutputs = new AssetSet(); | 101 var newOutputs = new AssetSet(); |
| 91 var transform = createTransform(this, newOutputs); | 102 var transform = createTransform(this, newOutputs); |
| 92 | 103 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 } else { | 184 } else { |
| 174 var controller = new AssetNodeController.available(asset); | 185 var controller = new AssetNodeController.available(asset); |
| 175 _outputControllers[asset.id] = controller; | 186 _outputControllers[asset.id] = controller; |
| 176 brandNewOutputs.add(controller.node); | 187 brandNewOutputs.add(controller.node); |
| 177 } | 188 } |
| 178 } | 189 } |
| 179 | 190 |
| 180 return brandNewOutputs; | 191 return brandNewOutputs; |
| 181 } | 192 } |
| 182 } | 193 } |
| OLD | NEW |