| 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.phase_forwarder; | 5 library barback.phase_forwarder; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'asset_node.dart'; | 9 import 'asset_node.dart'; |
| 10 import 'asset_node_set.dart'; | 10 import 'asset_node_set.dart'; |
| 11 | 11 |
| 12 /// A class that takes care of forwarding assets within a phase. | 12 /// A class that takes care of forwarding assets within a phase. |
| 13 /// | 13 /// |
| 14 /// Each phase contains one or more channels that process its input assets. The | 14 /// Each phase contains one or more channels that process its input assets. Each |
| 15 /// non-grouped transformers for that phase are one such channel, and each | 15 /// non-grouped transformer for that phase is a channel, each [TransformerGroup] |
| 16 /// [TransformerGroup] in that phase is another. For each input asset, each | 16 /// in that phase is another, and the source node is the final channel. For each |
| 17 /// channel individually decides whether to forward that asset based on whether | 17 /// input asset, each channel individually decides whether to forward that asset |
| 18 /// that channel uses it. If a channel does decide to forward an asset, we call | 18 /// based on whether that channel uses it. If a channel does decide to forward |
| 19 /// that forwarded asset an "intermediate forwarded asset" to distinguish it | 19 /// an asset, we call that forwarded asset an "intermediate forwarded asset" to |
| 20 /// from the output of a [PhaseForwarder]. | 20 /// distinguish it from the output of a [PhaseForwarder]. |
| 21 /// | 21 /// |
| 22 /// All intermediate assets with a given origin are provided to a single | 22 /// All intermediate assets with a given origin are provided to a single |
| 23 /// [PhaseForwarder] via [addIntermediateAsset]. This forwarder then determines | 23 /// [PhaseForwarder] via [addIntermediateAsset]. This forwarder then determines |
| 24 /// whether all channels in the phase produced intermediate assets. If so, that | 24 /// whether all channels in the phase produced intermediate assets. If so, that |
| 25 /// means the input asset wasn't consumed by any channel, so the | 25 /// means the input asset wasn't consumed by any channel, so the |
| 26 /// [PhaseForwarder] forwards it again, producing an output which we'll call the | 26 /// [PhaseForwarder] forwards it again, producing an output which we'll call the |
| 27 /// "final forwarded asset". | 27 /// "final forwarded asset". |
| 28 /// | 28 /// |
| 29 /// A final forwarded asset will be available only if all of the intermediate | 29 /// A final forwarded asset will be available only if all of the intermediate |
| 30 /// forwarded assets are themselves available. If any of the intermediate assets | 30 /// forwarded assets are themselves available. If any of the intermediate assets |
| 31 /// are dirty, the final asset will also be marked dirty. | 31 /// are dirty, the final asset will also be marked dirty. |
| 32 class PhaseForwarder { | 32 class PhaseForwarder { |
| 33 /// The number of channels through which the asset may have been forwarded. | 33 /// The number of channels to forward, counting the source node. |
| 34 /// | |
| 35 /// Each group is a channel, along with one channel for the [PhaseInput] that | |
| 36 /// handles all the transformers. | |
| 37 set numChannels(int value) { | |
| 38 _numChannels = value; | |
| 39 _adjustOutput(); | |
| 40 } | |
| 41 int _numChannels; | 34 int _numChannels; |
| 42 | 35 |
| 43 /// The intermediate forwarded assets. | 36 /// The intermediate forwarded assets. |
| 44 final _intermediateAssets = new AssetNodeSet(); | 37 final _intermediateAssets = new AssetNodeSet(); |
| 45 | 38 |
| 46 /// The final forwarded asset. | 39 /// The final forwarded asset. |
| 47 /// | 40 /// |
| 48 /// This will be null if the asset is not being forwarded. | 41 /// This will be null if the asset is not being forwarded. |
| 49 AssetNode get output => _outputController.node; | 42 AssetNode get output => |
| 43 _outputController == null ? null : _outputController.node; |
| 50 AssetNodeController _outputController; | 44 AssetNodeController _outputController; |
| 51 | 45 |
| 52 /// A stream that emits an event whenever [this] starts producing a final | 46 /// A stream that emits an event whenever [this] starts producing a final |
| 53 /// forwarded asset. | 47 /// forwarded asset. |
| 54 /// | 48 /// |
| 55 /// Whenever this stream emits an event, the value will be identical to | 49 /// Whenever this stream emits an event, the value will be identical to |
| 56 /// [output]. | 50 /// [output]. |
| 57 Stream<AssetNode> get onAsset => _onAssetController.stream; | 51 Stream<AssetNode> get onAsset => _onAssetController.stream; |
| 58 final _onAssetController = new StreamController<AssetNode>(sync: true); | 52 final _onAssetController = |
| 53 new StreamController<AssetNode>.broadcast(sync: true); |
| 59 | 54 |
| 60 PhaseForwarder(this._numChannels); | 55 /// Creates a phase forwarder forwarding nodes that come from [node] across |
| 56 /// [numTransformers] transformers and [numGroups] groups. |
| 57 /// |
| 58 /// [node] is passed in explicitly so that it can be forwarded if there are no |
| 59 /// other channels. |
| 60 PhaseForwarder(AssetNode node, int numTransformers, int numGroups) |
| 61 : _numChannels = numTransformers + numGroups + 1 { |
| 62 addIntermediateAsset(node); |
| 63 } |
| 64 |
| 65 /// Notify the forwarder that the number of transformer and group channels has |
| 66 /// changed. |
| 67 void updateTransformers(int numTransformers, int numGroups) { |
| 68 // Add one channel for the source node. |
| 69 _numChannels = numTransformers + numGroups + 1; |
| 70 _adjustOutput(); |
| 71 } |
| 61 | 72 |
| 62 /// Adds an intermediate forwarded asset to [this]. | 73 /// Adds an intermediate forwarded asset to [this]. |
| 63 /// | 74 /// |
| 64 /// [asset] must have the same origin as all other intermediate forwarded | 75 /// [asset] must have the same origin as all other intermediate forwarded |
| 65 /// assets. | 76 /// assets. |
| 66 void addIntermediateAsset(AssetNode asset) { | 77 void addIntermediateAsset(AssetNode asset) { |
| 67 if (_intermediateAssets.isNotEmpty) { | 78 if (_intermediateAssets.isNotEmpty) { |
| 68 assert(asset.origin == _intermediateAssets.first.origin); | 79 assert(asset.origin == _intermediateAssets.first.origin); |
| 69 } | 80 } |
| 70 | 81 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 orElse: () => _intermediateAssets.first); | 120 orElse: () => _intermediateAssets.first); |
| 110 _outputController = new AssetNodeController.from(finalAsset); | 121 _outputController = new AssetNodeController.from(finalAsset); |
| 111 _onAssetController.add(output); | 122 _onAssetController.add(output); |
| 112 return; | 123 return; |
| 113 } | 124 } |
| 114 | 125 |
| 115 // If we're already forwarding a final asset, set it dirty iff any of the | 126 // If we're already forwarding a final asset, set it dirty iff any of the |
| 116 // intermediate assets are dirty. | 127 // intermediate assets are dirty. |
| 117 if (_intermediateAssets.any((asset) => asset.state.isDirty)) { | 128 if (_intermediateAssets.any((asset) => asset.state.isDirty)) { |
| 118 if (!_outputController.node.state.isDirty) _outputController.setDirty(); | 129 if (!_outputController.node.state.isDirty) _outputController.setDirty(); |
| 119 } else { | 130 } else if (!_outputController.node.state.isAvailable) { |
| 120 if (!_outputController.node.state.isAvailable) { | 131 _outputController.setAvailable(_intermediateAssets.first.asset); |
| 121 _outputController.setAvailable(_intermediateAssets.first.asset); | |
| 122 } | |
| 123 } | 132 } |
| 124 } | 133 } |
| 125 } | 134 } |
| OLD | NEW |