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