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 | 11 |
11 /// A class that takes care of forwarding assets within a phase. | 12 /// A class that takes care of forwarding assets within a phase. |
12 /// | 13 /// |
13 /// 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. The |
14 /// non-grouped transformers for that phase are one such channel, and each | 15 /// non-grouped transformers for that phase are one such channel, and each |
15 /// [TransformerGroup] in that phase is another. For each input asset, each | 16 /// [TransformerGroup] in that phase is another. For each input asset, each |
16 /// channel individually decides whether to forward that asset based on whether | 17 /// channel individually decides whether to forward that asset based on whether |
17 /// 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 |
18 /// that forwarded asset an "intermediate forwarded asset" to distinguish it | 19 /// that forwarded asset an "intermediate forwarded asset" to distinguish it |
19 /// from the output of a [PhaseForwarder]. | 20 /// from the output of a [PhaseForwarder]. |
(...skipping 13 matching lines...) Expand all Loading... |
33 /// | 34 /// |
34 /// Each group is a channel, along with one channel for the [PhaseInput] that | 35 /// Each group is a channel, along with one channel for the [PhaseInput] that |
35 /// handles all the transformers. | 36 /// handles all the transformers. |
36 set numChannels(int value) { | 37 set numChannels(int value) { |
37 _numChannels = value; | 38 _numChannels = value; |
38 _adjustOutput(); | 39 _adjustOutput(); |
39 } | 40 } |
40 int _numChannels; | 41 int _numChannels; |
41 | 42 |
42 /// The intermediate forwarded assets. | 43 /// The intermediate forwarded assets. |
43 final _intermediateAssets = new Set<AssetNode>(); | 44 final _intermediateAssets = new AssetNodeSet(); |
44 | 45 |
45 /// The final forwarded asset. | 46 /// The final forwarded asset. |
46 /// | 47 /// |
47 /// This will be null if the asset is not being forwarded. | 48 /// This will be null if the asset is not being forwarded. |
48 AssetNode get output => _outputController.node; | 49 AssetNode get output => _outputController.node; |
49 AssetNodeController _outputController; | 50 AssetNodeController _outputController; |
50 | 51 |
51 /// A stream that emits an event whenever [this] starts producing a final | 52 /// A stream that emits an event whenever [this] starts producing a final |
52 /// forwarded asset. | 53 /// forwarded asset. |
53 /// | 54 /// |
54 /// Whenever this stream emits an event, the value will be identical to | 55 /// Whenever this stream emits an event, the value will be identical to |
55 /// [output]. | 56 /// [output]. |
56 Stream<AssetNode> get onForwarding => _onForwardingController.stream; | 57 Stream<AssetNode> get onForwarding => _onForwardingController.stream; |
57 final _onForwardingController = | 58 final _onForwardingController = |
58 new StreamController<AssetNode>.broadcast(sync: true); | 59 new StreamController<AssetNode>.broadcast(sync: true); |
59 | 60 |
60 PhaseForwarder(this._numChannels); | 61 PhaseForwarder(this._numChannels); |
61 | 62 |
62 /// Adds an intermediate forwarded asset to [this]. | 63 /// Adds an intermediate forwarded asset to [this]. |
63 /// | 64 /// |
64 /// [asset] must have the same origin as all other intermediate forwarded | 65 /// [asset] must have the same origin as all other intermediate forwarded |
65 /// assets. | 66 /// assets. |
66 void addIntermediateAsset(AssetNode asset) { | 67 void addIntermediateAsset(AssetNode asset) { |
67 if (_intermediateAssets.isNotEmpty) { | 68 if (_intermediateAssets.isNotEmpty) { |
68 assert(asset.origin == _intermediateAssets.first.origin); | 69 assert(asset.origin == _intermediateAssets.first.origin); |
69 } | 70 } |
70 | 71 |
71 _intermediateAssets.add(asset); | 72 _intermediateAssets.add(asset); |
72 | 73 asset.onStateChange.listen((_) => _adjustOutput()); |
73 asset.onStateChange.listen((state) { | |
74 if (state.isRemoved) _intermediateAssets.remove(asset); | |
75 _adjustOutput(); | |
76 }); | |
77 | 74 |
78 _adjustOutput(); | 75 _adjustOutput(); |
79 } | 76 } |
80 | 77 |
81 /// Mark this forwarder as removed. | 78 /// Mark this forwarder as removed. |
82 /// | 79 /// |
83 /// This will remove [output] if it exists. | 80 /// This will remove [output] if it exists. |
84 void remove() { | 81 void remove() { |
85 if (_outputController != null) { | 82 if (_outputController != null) { |
86 _outputController.setRemoved(); | 83 _outputController.setRemoved(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 // intermediate assets are dirty. | 117 // intermediate assets are dirty. |
121 if (_intermediateAssets.any((asset) => asset.state.isDirty)) { | 118 if (_intermediateAssets.any((asset) => asset.state.isDirty)) { |
122 if (!_outputController.node.state.isDirty) _outputController.setDirty(); | 119 if (!_outputController.node.state.isDirty) _outputController.setDirty(); |
123 } else { | 120 } else { |
124 if (!_outputController.node.state.isAvailable) { | 121 if (!_outputController.node.state.isAvailable) { |
125 _outputController.setAvailable(_intermediateAssets.first.asset); | 122 _outputController.setAvailable(_intermediateAssets.first.asset); |
126 } | 123 } |
127 } | 124 } |
128 } | 125 } |
129 } | 126 } |
OLD | NEW |