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

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: Code review 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 [add]. This forwarder then determines whether all
25 /// channels in the phase produced intermediate assets. If so, that means the
26 /// input asset wasn't consumed by any channel, so the [PhaseForwarder] forwards
27 /// it again, producing an output which we'll call the "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.
Bob Nystrom 2013/10/07 23:58:39 This is really helpful.
nweiz 2013/10/08 00:30:39 :)
32 class PhaseForwarder {
33 /// The number of channels through which the asset may have been forwarded.
34 ///
35 /// Each group is a channel, along with one channel for the [PhaseInput] that
36 /// handles all the transformers.
37 set numChannels(int value) {
38 _numChannels = value;
39 _adjustOutput();
40 }
41 int _numChannels;
42
43 /// The intermediate forwarded assets.
44 final _intermediateAssets = new Set<AssetNode>();
45
46 /// The final forwarded asset.
47 ///
48 /// This will be null if the asset is not being forwarded.
49 AssetNode get output => _outputController.node;
50 AssetNodeController _outputController;
51
52 /// A stream that emits an event whenever [this] starts producing a final
53 /// forwarded asset.
54 ///
55 /// Whenever this stream emits an event, the value will be identical to
56 /// [output].
57 Stream<AssetNode> get onForwarding => _onForwardingController.stream;
58 final _onForwardingController =
59 new StreamController<AssetNode>.broadcast(sync: true);
60
61 PhaseForwarder(this._numChannels);
62
63 /// Adds an intermediate forwarded asset to [this].
64 ///
65 /// [asset] must have the same origin as all other intermediate forwarded
66 /// assets.
67 void addIntermediateAsset(AssetNode asset) {
68 if (_intermediateAssets.isNotEmpty) {
69 assert(asset.origin == _intermediateAssets.first.origin);
70 }
71
72 _intermediateAssets.add(asset);
73
74 asset.onStateChange.listen((state) {
75 if (state.isRemoved) _intermediateAssets.remove(asset);
76 _adjustOutput();
77 });
78
79 _adjustOutput();
80 }
81
82 /// Mark this forwarder as removed.
83 ///
84 /// This will remove [output] if it exists.
85 void remove() {
86 if (_outputController != null) {
87 _outputController.setRemoved();
88 _outputController = null;
89 }
90 _onForwardingController.close();
91 }
92
93 /// Adjusts [output] to ensure that it accurately reflects the current state
94 /// of the intermediate forwarded assets.
95 void _adjustOutput() {
96 assert(_intermediateAssets.length <= _numChannels);
97 assert(!_intermediateAssets.any((asset) => asset.state.isRemoved));
98
99 // If there are any channels that haven't forwarded an intermediate asset,
100 // we shouldn't forward a final asset. If we are currently, remove
101 // it.
102 if (_intermediateAssets.length < _numChannels) {
103 if (_outputController == null) return;
104 _outputController.setRemoved();
105 _outputController = null;
106 return;
107 }
108
109 // If there isn't a final asset being forwarded yet, we should forward one.
110 // It should be dirty iff any of the intermediate assets are dirty.
111 if (_outputController == null) {
112 var asset = _intermediateAssets.firstWhere((asset) => asset.state.isDirty,
113 orElse: () => _intermediateAssets.first);
114 _outputController = new AssetNodeController.from(asset);
115 _onForwardingController.add(output);
116 return;
117 }
118
119 // If we're already forwarding a final asset, set it dirty iff any of the
120 // intermediate assets are dirty.
121 if (_intermediateAssets.any((asset) => asset.state.isDirty)) {
122 if (!_outputController.node.state.isDirty) _outputController.setDirty();
123 } else {
124 if (!_outputController.node.state.isAvailable) {
125 _outputController.setAvailable(_intermediateAssets.first.asset);
126 }
127 }
128 }
129 }
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