OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library barback.graph.phase_forwarder; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import '../asset/asset_node.dart'; |
| 10 import '../asset/asset_node_set.dart'; |
| 11 |
| 12 /// A class that takes care of forwarding assets within a phase. |
| 13 /// |
| 14 /// Each phase contains one or more channels that process its input assets. Each |
| 15 /// non-grouped transformer for that phase is a channel, each [TransformerGroup] |
| 16 /// in that phase is another, and the source node is the final channel. For each |
| 17 /// input asset, each channel individually decides whether to forward that asset |
| 18 /// based on whether that channel uses it. If a channel does decide to forward |
| 19 /// an asset, we call that forwarded asset an "intermediate forwarded asset" to |
| 20 /// distinguish it from the output of a [PhaseForwarder]. |
| 21 /// |
| 22 /// All intermediate assets with a given origin are provided to a single |
| 23 /// [PhaseForwarder] via [addIntermediateAsset]. This forwarder then determines |
| 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 |
| 26 /// [PhaseForwarder] forwards it again, producing an output which we'll call the |
| 27 /// "final forwarded asset". |
| 28 /// |
| 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 |
| 31 /// are dirty, the final asset will also be marked dirty. |
| 32 class PhaseForwarder { |
| 33 /// The number of channels to forward, counting the source node. |
| 34 int _numChannels; |
| 35 |
| 36 /// The intermediate forwarded assets. |
| 37 final _intermediateAssets = new AssetNodeSet(); |
| 38 |
| 39 /// The final forwarded asset. |
| 40 /// |
| 41 /// This will be null if the asset is not being forwarded. |
| 42 AssetNode get output => |
| 43 _outputController == null ? null : _outputController.node; |
| 44 AssetNodeController _outputController; |
| 45 |
| 46 /// A stream that emits an event whenever [this] starts producing a final |
| 47 /// forwarded asset. |
| 48 /// |
| 49 /// Whenever this stream emits an event, the value will be identical to |
| 50 /// [output]. |
| 51 Stream<AssetNode> get onAsset => _onAssetController.stream; |
| 52 final _onAssetController = |
| 53 new StreamController<AssetNode>.broadcast(sync: true); |
| 54 |
| 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 } |
| 72 |
| 73 /// Adds an intermediate forwarded asset to [this]. |
| 74 /// |
| 75 /// [asset] must have the same origin as all other intermediate forwarded |
| 76 /// assets. |
| 77 void addIntermediateAsset(AssetNode asset) { |
| 78 if (_intermediateAssets.isNotEmpty) { |
| 79 assert(asset.origin == _intermediateAssets.first.origin); |
| 80 } |
| 81 |
| 82 _intermediateAssets.add(asset); |
| 83 asset.onStateChange.listen((_) => _adjustOutput()); |
| 84 |
| 85 _adjustOutput(); |
| 86 } |
| 87 |
| 88 /// Mark this forwarder as removed. |
| 89 /// |
| 90 /// This will remove [output] if it exists. |
| 91 void remove() { |
| 92 if (_outputController != null) { |
| 93 _outputController.setRemoved(); |
| 94 _outputController = null; |
| 95 } |
| 96 _onAssetController.close(); |
| 97 } |
| 98 |
| 99 /// Adjusts [output] to ensure that it accurately reflects the current state |
| 100 /// of the intermediate forwarded assets. |
| 101 void _adjustOutput() { |
| 102 assert(_intermediateAssets.length <= _numChannels); |
| 103 assert(!_intermediateAssets.any((asset) => asset.state.isRemoved)); |
| 104 |
| 105 // If there are any channels that haven't forwarded an intermediate asset, |
| 106 // we shouldn't forward a final asset. If we are currently, remove |
| 107 // it. |
| 108 if (_intermediateAssets.length < _numChannels) { |
| 109 if (_outputController == null) return; |
| 110 _outputController.setRemoved(); |
| 111 _outputController = null; |
| 112 return; |
| 113 } |
| 114 |
| 115 // If there isn't a final asset being forwarded yet, we should forward one. |
| 116 // It should be dirty iff any of the intermediate assets are dirty. |
| 117 if (_outputController == null) { |
| 118 var finalAsset = _intermediateAssets.firstWhere( |
| 119 (asset) => asset.state.isDirty, |
| 120 orElse: () => _intermediateAssets.first); |
| 121 _outputController = new AssetNodeController.from(finalAsset); |
| 122 _onAssetController.add(output); |
| 123 return; |
| 124 } |
| 125 |
| 126 // If we're already forwarding a final asset, set it dirty iff any of the |
| 127 // intermediate assets are dirty. |
| 128 if (_intermediateAssets.any((asset) => asset.state.isDirty)) { |
| 129 if (!_outputController.node.state.isDirty) _outputController.setDirty(); |
| 130 } else if (!_outputController.node.state.isAvailable) { |
| 131 _outputController.setAvailable(_intermediateAssets.first.asset); |
| 132 } |
| 133 } |
| 134 } |
OLD | NEW |