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

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: Expose the Operator class. 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
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 /// An asset should be forwarded if and only if it's not consumed by any
16 /// transformers or clusters in a phase. Even if an asset is forwarded, it
17 /// should be marked as [AssetState.DIRTY] if any of the intermediate forwarded
18 /// assets are themselves marked dirty. It should only be available if all
Bob Nystrom 2013/10/04 21:47:50 Can you clarify what "intermediate forwarded asset
nweiz 2013/10/07 23:21:31 Rewritten this whole thing to hopefully make it cl
19 /// intermediate assets are available.
20 ///
21 /// In order to determine whether to forward an asset, this compares the number
22 /// of inputs to [channels]. Only if each channel has forwarded the asset does
23 /// [this] emit the asset.
24 class PhaseForwarder {
Bob Nystrom 2013/10/04 21:47:50 "AssetForwarder"? I know it's for a Phase, but the
nweiz 2013/10/07 23:21:31 That's already a class that exists, and it's gener
25 /// Sets the number of channels through which the asset may have been
26 /// forwarded.
Bob Nystrom 2013/10/04 21:47:50 I think this should just be documented like a fiel
nweiz 2013/10/07 23:21:31 Done.
27 ///
28 /// Each cluster is a channel, along with one channel for the [PhaseInput]
Bob Nystrom 2013/10/04 21:47:50 This makes me wonder if we could stratify things w
nweiz 2013/10/07 23:21:31 I considered that, but it produces an infinite rec
Bob Nystrom 2013/10/07 23:58:39 Oh, right. :)
29 /// that handles all the transformers.
30 set channels(int value) {
Bob Nystrom 2013/10/04 21:47:50 "channels" -> "numChannels"
nweiz 2013/10/07 23:21:31 Done.
31 _channels = value;
32 _adjustOutput();
33 }
34 int _channels;
Bob Nystrom 2013/10/04 21:47:50 _numChannels
nweiz 2013/10/07 23:21:31 Done.
35
36 /// The input assets.
37 final _assets = new Set<AssetNode>();
38
39 /// The forwarded output asset.
40 ///
41 /// This will be null if the asset is not being forwarded.
42 AssetNode get output => _outputController.node;
43 AssetNodeController _outputController;
44
45 /// A stream that emits an event whenever [this] starts forwarding an asset.
46 ///
47 /// Whenever this stream emits an event, the value will be identical to
48 /// [output].
49 Stream<AssetNode> get onForwarding => _onForwardingController.stream;
50 final _onForwardingController =
51 new StreamController<AssetNode>.broadcast(sync: true);
52
53 PhaseForwarder(this._channels);
54
55 /// Adds a forwarded asset to [this].
56 ///
57 /// [asset] must have the same origin as all other forwarded assets.
58 void add(AssetNode asset) {
59 if (_assets.isNotEmpty) assert(asset.origin == _assets.first.origin);
60
61 _assets.add(asset);
62
63 asset.onStateChange.listen((state) {
64 if (state.isRemoved) _assets.remove(asset);
65 _adjustOutput();
66 });
67
68 _adjustOutput();
69 }
70
71 /// Mark this forwarder as removed.
72 ///
73 /// This will remove [output] if it exists.
74 void remove() {
75 if (_outputController != null) {
76 _outputController.setRemoved();
77 _outputController = null;
78 }
79 _onForwardingController.close();
80 }
81
82 /// Adjusts [output] to ensure that it accurately reflects the current state
83 /// of the forwarded nodes.
84 void _adjustOutput() {
85 assert(_assets.length <= _channels);
86 assert(!_assets.any((asset) => asset.state.isRemoved));
87
88 // If there are any channels that haven't forwarded an asset, we shouldn't
89 // emit an asset. If we are currently, remove it.
90 if (_assets.length < _channels) {
91 if (_outputController == null) return;
92 _outputController.setRemoved();
93 _outputController = null;
94 return;
95 }
96
97 // If there isn't an asset being emitted yet, we should emit one. It should
98 // be dirty iff any of the source assets are dirty.
99 if (_outputController == null) {
100 var asset = _assets.firstWhere((asset) => asset.state.isDirty,
101 orElse: () => _assets.first);
102 _outputController = new AssetNodeController.from(asset);
103 _onForwardingController.add(output);
104 return;
105 }
106
107 // If we're already emitting an asset, set it dirty iff any of the source
108 // assets are dirty.
109 if (_assets.any((asset) => asset.state.isDirty)) {
110 if (!_outputController.node.state.isDirty) _outputController.setDirty();
111 } else {
112 if (!_outputController.node.state.isAvailable) {
113 _outputController.setAvailable(_assets.first.asset);
114 }
115 }
116 }
117 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698