Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(477)

Side by Side Diff: pkg/barback/lib/src/phase_forwarder.dart

Issue 25376003: Add support for transformer clusters to barback. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: More code reivew changes Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/barback/lib/src/phase.dart ('k') | pkg/barback/lib/src/phase_input.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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.phase_forwarder;
6
7 import 'dart:async';
8
9 import 'asset_node.dart';
10 import 'asset_forwarder.dart';
11 import 'phase_output.dart';
12
13 /// A class that takes care of forwarding assets within a phase.
14 ///
15 /// Each phase contains one or more channels that process its input assets. The
16 /// non-grouped transformers for that phase are one such channel, and each
17 /// [TransformerGroup] in that phase is another. For each input asset, each
18 /// channel individually decides whether to forward that asset based on whether
19 /// that channel uses it. If a channel does decide to forward an asset, we call
20 /// that forwarded asset an "intermediate forwarded asset" to distinguish it
21 /// from the output of a [PhaseForwarder].
22 ///
23 /// All intermediate assets with a given origin are provided to a single
24 /// [PhaseForwarder] via [addIntermediateAsset]. This forwarder then determines
25 /// whether all channels in the phase produced intermediate assets. If so, that
26 /// means the input asset wasn't consumed by any channel, so the
27 /// [PhaseForwarder] forwards it again, producing an output which we'll call the
28 /// "final forwarded asset".
29 ///
30 /// A final forwarded asset will be available only if all of the intermediate
31 /// forwarded assets are themselves available. If any of the intermediate assets
32 /// are dirty, the final asset will also be marked dirty.
33 class PhaseForwarder {
34 /// The number of channels through which the asset may have been forwarded.
35 ///
36 /// Each group is a channel, along with one channel for the [PhaseInput] that
37 /// handles all the transformers.
38 set numChannels(int value) {
39 _numChannels = value;
40 _adjustOutput();
41 }
42 int _numChannels;
43
44 /// The intermediate forwarded assets.
45 final _intermediateAssets = new Set<AssetNode>();
46
47 /// The final forwarded asset.
48 ///
49 /// This will be null if the asset is not being forwarded.
50 AssetNode get output => _outputController.node;
51 AssetNodeController _outputController;
52
53 /// A stream that emits an event whenever [this] starts producing a final
54 /// forwarded asset.
55 ///
56 /// Whenever this stream emits an event, the value will be identical to
57 /// [output].
58 Stream<AssetNode> get onForwarding => _onForwardingController.stream;
59 final _onForwardingController =
60 new StreamController<AssetNode>.broadcast(sync: true);
61
62 PhaseForwarder(this._numChannels);
63
64 /// Adds an intermediate forwarded asset to [this].
65 ///
66 /// [asset] must have the same origin as all other intermediate forwarded
67 /// assets.
68 void addIntermediateAsset(AssetNode asset) {
69 if (_intermediateAssets.isNotEmpty) {
70 assert(asset.origin == _intermediateAssets.first.origin);
71 }
72
73 _intermediateAssets.add(asset);
74
75 asset.onStateChange.listen((state) {
76 if (state.isRemoved) _intermediateAssets.remove(asset);
77 _adjustOutput();
78 });
79
80 _adjustOutput();
81 }
82
83 /// Mark this forwarder as removed.
84 ///
85 /// This will remove [output] if it exists.
86 void remove() {
87 if (_outputController != null) {
88 _outputController.setRemoved();
89 _outputController = null;
90 }
91 _onForwardingController.close();
92 }
93
94 /// Adjusts [output] to ensure that it accurately reflects the current state
95 /// of the intermediate forwarded assets.
96 void _adjustOutput() {
97 assert(_intermediateAssets.length <= _numChannels);
98 assert(!_intermediateAssets.any((asset) => asset.state.isRemoved));
99
100 // If there are any channels that haven't forwarded an intermediate asset,
101 // we shouldn't forward a final asset. If we are currently, remove
102 // it.
103 if (_intermediateAssets.length < _numChannels) {
104 if (_outputController == null) return;
105 _outputController.setRemoved();
106 _outputController = null;
107 return;
108 }
109
110 // If there isn't a final asset being forwarded yet, we should forward one.
111 // It should be dirty iff any of the intermediate assets are dirty.
112 if (_outputController == null) {
113 var asset = _intermediateAssets.firstWhere((asset) => asset.state.isDirty,
114 orElse: () => _intermediateAssets.first);
115 _outputController = new AssetNodeController.from(asset);
116 _onForwardingController.add(output);
117 return;
118 }
119
120 // If we're already forwarding a final asset, set it dirty iff any of the
121 // intermediate assets are dirty.
122 if (_intermediateAssets.any((asset) => asset.state.isDirty)) {
123 if (!_outputController.node.state.isDirty) _outputController.setDirty();
124 } else {
125 if (!_outputController.node.state.isAvailable) {
126 _outputController.setAvailable(_intermediateAssets.first.asset);
127 }
128 }
129 }
130 }
OLDNEW
« no previous file with comments | « pkg/barback/lib/src/phase.dart ('k') | pkg/barback/lib/src/phase_input.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698