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

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

Issue 23038018: Factor a PhaseOutput class out of Phase. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 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/asset_forwarder.dart ('k') | pkg/barback/lib/src/phase.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.asset_node; 5 library barback.asset_node;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'asset.dart'; 9 import 'asset.dart';
10 import 'asset_id.dart'; 10 import 'asset_id.dart';
11 import 'errors.dart'; 11 import 'errors.dart';
12 import 'transform_node.dart'; 12 import 'transform_node.dart';
13 13
14 /// Describes the current state of an asset as part of a transformation graph. 14 /// Describes the current state of an asset as part of a transformation graph.
15 /// 15 ///
16 /// An asset node can be in one of three states (see [AssetState]). It provides 16 /// An asset node can be in one of three states (see [AssetState]). It provides
17 /// an [onStateChange] stream that emits an event whenever it changes state. 17 /// an [onStateChange] stream that emits an event whenever it changes state.
18 /// 18 ///
19 /// Asset nodes are controlled using [AssetNodeController]s. 19 /// Asset nodes are controlled using [AssetNodeController]s.
20 class AssetNode { 20 class AssetNode {
21 /// The id of the asset that this node represents. 21 /// The id of the asset that this node represents.
22 final AssetId id; 22 final AssetId id;
23 23
24 /// The transform that created this asset node. 24 /// The transform that created this asset node.
25 /// 25 ///
26 /// This is `null` for source assets. 26 /// This is `null` for source assets. It can change if the upstream transform
27 final TransformNode transform; 27 /// that created this asset changes; this change will *not* cause an
28 /// [onStateChange] event.
29 TransformNode get transform => _transform;
30 TransformNode _transform;
28 31
29 /// The current state of the asset node. 32 /// The current state of the asset node.
30 AssetState get state => _state; 33 AssetState get state => _state;
31 AssetState _state; 34 AssetState _state;
32 35
33 /// The concrete asset that this node represents. 36 /// The concrete asset that this node represents.
34 /// 37 ///
35 /// This is null unless [state] is [AssetState.AVAILABLE]. 38 /// This is null unless [state] is [AssetState.AVAILABLE].
36 Asset get asset => _asset; 39 Asset get asset => _asset;
37 Asset _asset; 40 Asset _asset;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 106
104 /// Returns a Future that completes as soon as the node is in a state that 107 /// Returns a Future that completes as soon as the node is in a state that
105 /// matches [test]. 108 /// matches [test].
106 /// 109 ///
107 /// The Future completes synchronously if this is already in such a state. 110 /// The Future completes synchronously if this is already in such a state.
108 Future<AssetState> _waitForState(bool test(AssetState state)) { 111 Future<AssetState> _waitForState(bool test(AssetState state)) {
109 if (test(state)) return new Future.sync(() => state); 112 if (test(state)) return new Future.sync(() => state);
110 return onStateChange.firstWhere(test); 113 return onStateChange.firstWhere(test);
111 } 114 }
112 115
113 AssetNode._(this.id, this.transform) 116 AssetNode._(this.id, this._transform)
114 : _state = AssetState.DIRTY; 117 : _state = AssetState.DIRTY;
115 118
116 AssetNode._available(Asset asset, this.transform) 119 AssetNode._available(Asset asset, this._transform)
117 : id = asset.id, 120 : id = asset.id,
118 _asset = asset, 121 _asset = asset,
119 _state = AssetState.AVAILABLE; 122 _state = AssetState.AVAILABLE;
120 } 123 }
121 124
122 /// The controller for an [AssetNode]. 125 /// The controller for an [AssetNode].
123 /// 126 ///
124 /// This controls which state the node is in. 127 /// This controls which state the node is in.
125 class AssetNodeController { 128 class AssetNodeController {
126 final AssetNode node; 129 final AssetNode node;
127 130
128 /// Creates a controller for a dirty node. 131 /// Creates a controller for a dirty node.
129 AssetNodeController(AssetId id, [TransformNode transform]) 132 AssetNodeController(AssetId id, [TransformNode transform])
130 : node = new AssetNode._(id, transform); 133 : node = new AssetNode._(id, transform);
131 134
132 /// Creates a controller for an available node with the given concrete 135 /// Creates a controller for an available node with the given concrete
133 /// [asset]. 136 /// [asset].
134 AssetNodeController.available(Asset asset, [TransformNode transform]) 137 AssetNodeController.available(Asset asset, [TransformNode transform])
135 : node = new AssetNode._available(asset, transform); 138 : node = new AssetNode._available(asset, transform);
136 139
140 /// Creates a controller for a node whose initial state matches the current
141 /// state of [node].
142 AssetNodeController.from(AssetNode node)
143 : node = new AssetNode._(node.id, node.transform) {
144 if (node.state.isAvailable) {
145 setAvailable(node.asset);
146 } else if (node.state.isRemoved) {
147 setRemoved();
148 }
149 }
150
137 /// Marks the node as [AssetState.DIRTY]. 151 /// Marks the node as [AssetState.DIRTY].
138 void setDirty() { 152 void setDirty() {
139 assert(node._state != AssetState.REMOVED); 153 assert(node._state != AssetState.REMOVED);
140 node._state = AssetState.DIRTY; 154 node._state = AssetState.DIRTY;
141 node._asset = null; 155 node._asset = null;
142 node._stateChangeController.add(AssetState.DIRTY); 156 node._stateChangeController.add(AssetState.DIRTY);
143 } 157 }
144 158
145 /// Marks the node as [AssetState.REMOVED]. 159 /// Marks the node as [AssetState.REMOVED].
146 /// 160 ///
(...skipping 11 matching lines...) Expand all
158 /// It's an error to mark an already-available node as available. It should be 172 /// It's an error to mark an already-available node as available. It should be
159 /// marked as dirty first. 173 /// marked as dirty first.
160 void setAvailable(Asset asset) { 174 void setAvailable(Asset asset) {
161 assert(asset.id == node.id); 175 assert(asset.id == node.id);
162 assert(node._state != AssetState.REMOVED); 176 assert(node._state != AssetState.REMOVED);
163 assert(node._state != AssetState.AVAILABLE); 177 assert(node._state != AssetState.AVAILABLE);
164 node._state = AssetState.AVAILABLE; 178 node._state = AssetState.AVAILABLE;
165 node._asset = asset; 179 node._asset = asset;
166 node._stateChangeController.add(AssetState.AVAILABLE); 180 node._stateChangeController.add(AssetState.AVAILABLE);
167 } 181 }
182
183 /// Sets the node's [AssetNode.transform] property.
Bob Nystrom 2013/08/21 21:23:40 Add a bit explaining when/why this will occur.
nweiz 2013/08/22 18:30:49 Done.
184 void setTransform(TransformNode transform) {
185 node._transform = transform;
186 }
168 } 187 }
169 188
170 // TODO(nweiz): add an error state. 189 // TODO(nweiz): add an error state.
171 /// An enum of states that an [AssetNode] can be in. 190 /// An enum of states that an [AssetNode] can be in.
172 class AssetState { 191 class AssetState {
173 /// The node has a concrete asset loaded, available, and up-to-date. The asset 192 /// The node has a concrete asset loaded, available, and up-to-date. The asset
174 /// is accessible via [AssetNode.asset]. An asset can only be marked available 193 /// is accessible via [AssetNode.asset]. An asset can only be marked available
175 /// again from the [AssetState.DIRTY] state. 194 /// again from the [AssetState.DIRTY] state.
176 static final AVAILABLE = const AssetState._("available"); 195 static final AVAILABLE = const AssetState._("available");
177 196
(...skipping 13 matching lines...) Expand all
191 210
192 /// Whether this state is [AssetState.DIRTY]. 211 /// Whether this state is [AssetState.DIRTY].
193 bool get isDirty => this == AssetState.DIRTY; 212 bool get isDirty => this == AssetState.DIRTY;
194 213
195 final String name; 214 final String name;
196 215
197 const AssetState._(this.name); 216 const AssetState._(this.name);
198 217
199 String toString() => name; 218 String toString() => name;
200 } 219 }
OLDNEW
« no previous file with comments | « pkg/barback/lib/src/asset_forwarder.dart ('k') | pkg/barback/lib/src/phase.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698