Chromium Code Reviews| 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, and each |
| 16 /// [TransformerGroup] in that phase is another. For each input asset, each | 16 /// [TransformerGroup] in that phase is another. For each input asset, each |
| 17 /// channel individually decides whether to forward that asset based on whether | 17 /// channel individually decides whether to forward that asset based on whether |
| 18 /// that channel uses it. If a channel does decide to forward an asset, we call | 18 /// that channel uses it. If a channel does decide to forward an asset, we call |
| 19 /// that forwarded asset an "intermediate forwarded asset" to distinguish it | 19 /// that forwarded asset an "intermediate forwarded asset" to distinguish it |
| 20 /// from the output of a [PhaseForwarder]. | 20 /// 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 through which the asset may have been forwarded. |
| 34 /// | 34 /// |
| 35 /// Each group is a channel, along with one channel for the [PhaseInput] that | 35 /// Each transformer in the phase is a channel, as is each group. [value] may |
| 36 /// handles all the transformers. | 36 /// be zero, indicating that this phase has no transformers or groups; if so, |
| 37 /// the asset is always forwarded. | |
| 37 set numChannels(int value) { | 38 set numChannels(int value) { |
| 38 _numChannels = value; | 39 _numChannels = value; |
| 39 _adjustOutput(); | 40 _adjustOutput(); |
| 40 } | 41 } |
| 41 int _numChannels; | 42 int _numChannels; |
| 42 | 43 |
| 44 /// The real number of channels to forward, counting the source node. | |
| 45 /// | |
| 46 /// This is one higher than [numChannels] because the source node should get | |
| 47 /// forwarded if there are no other channels. | |
| 48 int get _realNumChannels => _numChannels + 1; | |
|
Bob Nystrom
2014/03/12 17:42:58
This is pretty gross. How about we encapsulate the
nweiz
2014/03/12 21:44:47
SGTM, done.
| |
| 49 | |
| 43 /// The intermediate forwarded assets. | 50 /// The intermediate forwarded assets. |
| 44 final _intermediateAssets = new AssetNodeSet(); | 51 final _intermediateAssets = new AssetNodeSet(); |
| 45 | 52 |
| 46 /// The final forwarded asset. | 53 /// The final forwarded asset. |
| 47 /// | 54 /// |
| 48 /// This will be null if the asset is not being forwarded. | 55 /// This will be null if the asset is not being forwarded. |
| 49 AssetNode get output => _outputController.node; | 56 AssetNode get output => |
| 57 _outputController == null ? null : _outputController.node; | |
| 50 AssetNodeController _outputController; | 58 AssetNodeController _outputController; |
| 51 | 59 |
| 52 /// A stream that emits an event whenever [this] starts producing a final | 60 /// A stream that emits an event whenever [this] starts producing a final |
| 53 /// forwarded asset. | 61 /// forwarded asset. |
| 54 /// | 62 /// |
| 55 /// Whenever this stream emits an event, the value will be identical to | 63 /// Whenever this stream emits an event, the value will be identical to |
| 56 /// [output]. | 64 /// [output]. |
| 57 Stream<AssetNode> get onAsset => _onAssetController.stream; | 65 Stream<AssetNode> get onAsset => _onAssetController.stream; |
| 58 final _onAssetController = new StreamController<AssetNode>(sync: true); | 66 final _onAssetController = new StreamController<AssetNode>.broadcast(sync: tru e); |
|
Bob Nystrom
2014/03/12 17:42:58
Long line.
nweiz
2014/03/12 21:44:47
Done.
| |
| 59 | 67 |
| 60 PhaseForwarder(this._numChannels); | 68 /// Creates a phase forwarder forwarding nodes that come from [node] across |
| 69 /// [numChannels] channels. | |
| 70 /// | |
| 71 /// [node] is passed in explicitly so that it can be forwarded if | |
| 72 /// [numChannels] becomes zero. | |
| 73 PhaseForwarder(AssetNode node, this._numChannels) { | |
| 74 addIntermediateAsset(node); | |
| 75 } | |
| 61 | 76 |
| 62 /// Adds an intermediate forwarded asset to [this]. | 77 /// Adds an intermediate forwarded asset to [this]. |
| 63 /// | 78 /// |
| 64 /// [asset] must have the same origin as all other intermediate forwarded | 79 /// [asset] must have the same origin as all other intermediate forwarded |
| 65 /// assets. | 80 /// assets. |
| 66 void addIntermediateAsset(AssetNode asset) { | 81 void addIntermediateAsset(AssetNode asset) { |
| 67 if (_intermediateAssets.isNotEmpty) { | 82 if (_intermediateAssets.isNotEmpty) { |
| 68 assert(asset.origin == _intermediateAssets.first.origin); | 83 assert(asset.origin == _intermediateAssets.first.origin); |
| 69 } | 84 } |
| 70 | 85 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 81 if (_outputController != null) { | 96 if (_outputController != null) { |
| 82 _outputController.setRemoved(); | 97 _outputController.setRemoved(); |
| 83 _outputController = null; | 98 _outputController = null; |
| 84 } | 99 } |
| 85 _onAssetController.close(); | 100 _onAssetController.close(); |
| 86 } | 101 } |
| 87 | 102 |
| 88 /// Adjusts [output] to ensure that it accurately reflects the current state | 103 /// Adjusts [output] to ensure that it accurately reflects the current state |
| 89 /// of the intermediate forwarded assets. | 104 /// of the intermediate forwarded assets. |
| 90 void _adjustOutput() { | 105 void _adjustOutput() { |
| 91 assert(_intermediateAssets.length <= _numChannels); | 106 assert(_intermediateAssets.length <= _realNumChannels); |
| 92 assert(!_intermediateAssets.any((asset) => asset.state.isRemoved)); | 107 assert(!_intermediateAssets.any((asset) => asset.state.isRemoved)); |
| 93 | 108 |
| 94 // If there are any channels that haven't forwarded an intermediate asset, | 109 // If there are any channels that haven't forwarded an intermediate asset, |
| 95 // we shouldn't forward a final asset. If we are currently, remove | 110 // we shouldn't forward a final asset. If we are currently, remove |
| 96 // it. | 111 // it. |
| 97 if (_intermediateAssets.length < _numChannels) { | 112 if (_intermediateAssets.length < _realNumChannels) { |
| 98 if (_outputController == null) return; | 113 if (_outputController == null) return; |
| 99 _outputController.setRemoved(); | 114 _outputController.setRemoved(); |
| 100 _outputController = null; | 115 _outputController = null; |
| 101 return; | 116 return; |
| 102 } | 117 } |
| 103 | 118 |
| 104 // If there isn't a final asset being forwarded yet, we should forward one. | 119 // If there isn't a final asset being forwarded yet, we should forward one. |
| 105 // It should be dirty iff any of the intermediate assets are dirty. | 120 // It should be dirty iff any of the intermediate assets are dirty. |
| 106 if (_outputController == null) { | 121 if (_outputController == null) { |
| 107 var finalAsset = _intermediateAssets.firstWhere( | 122 var finalAsset = _intermediateAssets.firstWhere( |
| 108 (asset) => asset.state.isDirty, | 123 (asset) => asset.state.isDirty, |
| 109 orElse: () => _intermediateAssets.first); | 124 orElse: () => _intermediateAssets.first); |
| 110 _outputController = new AssetNodeController.from(finalAsset); | 125 _outputController = new AssetNodeController.from(finalAsset); |
| 111 _onAssetController.add(output); | 126 _onAssetController.add(output); |
| 112 return; | 127 return; |
| 113 } | 128 } |
| 114 | 129 |
| 115 // If we're already forwarding a final asset, set it dirty iff any of the | 130 // If we're already forwarding a final asset, set it dirty iff any of the |
| 116 // intermediate assets are dirty. | 131 // intermediate assets are dirty. |
| 117 if (_intermediateAssets.any((asset) => asset.state.isDirty)) { | 132 if (_intermediateAssets.any((asset) => asset.state.isDirty)) { |
| 118 if (!_outputController.node.state.isDirty) _outputController.setDirty(); | 133 if (!_outputController.node.state.isDirty) _outputController.setDirty(); |
| 119 } else { | 134 } else if (!_outputController.node.state.isAvailable) { |
| 120 if (!_outputController.node.state.isAvailable) { | 135 _outputController.setAvailable(_intermediateAssets.first.asset); |
| 121 _outputController.setAvailable(_intermediateAssets.first.asset); | |
| 122 } | |
| 123 } | 136 } |
| 124 } | 137 } |
| 125 } | 138 } |
| OLD | NEW |