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

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

Issue 149243009: Add support for lazy transformers. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: name changes Created 6 years, 10 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
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';
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 /// The current state of the asset node. 43 /// The current state of the asset node.
44 AssetState get state => _state; 44 AssetState get state => _state;
45 AssetState _state; 45 AssetState _state;
46 46
47 /// The concrete asset that this node represents. 47 /// The concrete asset that this node represents.
48 /// 48 ///
49 /// This is null unless [state] is [AssetState.AVAILABLE]. 49 /// This is null unless [state] is [AssetState.AVAILABLE].
50 Asset get asset => _asset; 50 Asset get asset => _asset;
51 Asset _asset; 51 Asset _asset;
52 52
53 /// The callback to be called to notify this asset node's creator that the
54 /// concrete asset should be generated.
55 ///
56 /// This is null for non-lazy asset nodes (see [AssetNodeController.lazy]).
57 /// Once this is called, it's set to null and [this] is no longer considered
58 /// lazy.
59 Function _lazyCallback;
60
53 /// A broadcast stream that emits an event whenever the node changes state. 61 /// A broadcast stream that emits an event whenever the node changes state.
54 /// 62 ///
55 /// This stream is synchronous to ensure that when a source asset is modified 63 /// This stream is synchronous to ensure that when a source asset is modified
56 /// or removed, the appropriate portion of the asset graph is dirtied before 64 /// or removed, the appropriate portion of the asset graph is dirtied before
57 /// any [Barback.getAssetById] calls emit newly-incorrect values. 65 /// any [Barback.getAssetById] calls emit newly-incorrect values.
58 Stream<AssetState> get onStateChange => _stateChangeController.stream; 66 Stream<AssetState> get onStateChange => _stateChangeController.stream;
59 67
60 /// This is synchronous so that a source being updated will always be 68 /// This is synchronous so that a source being updated will always be
61 /// propagated through the build graph before anything that depends on it is 69 /// propagated through the build graph before anything that depends on it is
62 /// requested. 70 /// requested.
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 return onStateChange.firstWhere(test).then((_) => callback(state)); 138 return onStateChange.firstWhere(test).then((_) => callback(state));
131 } 139 }
132 140
133 AssetNode._(this.id, this._transform, this._origin) 141 AssetNode._(this.id, this._transform, this._origin)
134 : _state = AssetState.DIRTY; 142 : _state = AssetState.DIRTY;
135 143
136 AssetNode._available(Asset asset, this._transform, this._origin) 144 AssetNode._available(Asset asset, this._transform, this._origin)
137 : id = asset.id, 145 : id = asset.id,
138 _asset = asset, 146 _asset = asset,
139 _state = AssetState.AVAILABLE; 147 _state = AssetState.AVAILABLE;
148
149 AssetNode._lazy(this.id, this._transform, this._origin, this._lazyCallback)
150 : _state = AssetState.DIRTY;
151
152 /// If [this] is lazy, force it to generate a concrete asset; otherwise, do
153 /// nothing.
154 ///
155 /// See [AssetNodeController.lazy].
156 void force() {
157 if (_origin != null) {
158 _origin.force();
159 } else if (_lazyCallback != null) {
160 _lazyCallback();
161 _lazyCallback = null;
162 }
163 }
140 } 164 }
141 165
142 /// The controller for an [AssetNode]. 166 /// The controller for an [AssetNode].
143 /// 167 ///
144 /// This controls which state the node is in. 168 /// This controls which state the node is in.
145 class AssetNodeController { 169 class AssetNodeController {
146 final AssetNode node; 170 final AssetNode node;
147 171
148 /// Creates a controller for a dirty node. 172 /// Creates a controller for a dirty node.
149 AssetNodeController(AssetId id, [TransformNode transform]) 173 AssetNodeController(AssetId id, [TransformNode transform])
150 : node = new AssetNode._(id, transform, null); 174 : node = new AssetNode._(id, transform, null);
151 175
152 /// Creates a controller for an available node with the given concrete 176 /// Creates a controller for an available node with the given concrete
153 /// [asset]. 177 /// [asset].
154 AssetNodeController.available(Asset asset, [TransformNode transform]) 178 AssetNodeController.available(Asset asset, [TransformNode transform])
155 : node = new AssetNode._available(asset, transform, null); 179 : node = new AssetNode._available(asset, transform, null);
156 180
181 /// Creates a controller for a lazy node.
182 ///
183 /// For the most part, this node works like any other dirty node. However, the
184 /// owner of its controller isn't expected to do the work to make it available
185 /// as soon as possible like they would for a non-lazy node. Instead, when its
186 /// value is needed, [callback] will fire to indicate that it should be made
187 /// available as soon as possible.
188 ///
189 /// [callback] is guaranteed to only fire once.
190 AssetNodeController.lazy(AssetId id, void callback(),
191 [TransformNode transform])
192 : node = new AssetNode._lazy(id, transform, null, callback);
193
157 /// Creates a controller for a node whose initial state matches the current 194 /// Creates a controller for a node whose initial state matches the current
158 /// state of [node]. 195 /// state of [node].
159 /// 196 ///
160 /// [AssetNode.origin] of the returned node will automatically be set to 197 /// [AssetNode.origin] of the returned node will automatically be set to
161 /// `node.origin`. 198 /// `node.origin`.
199 ///
200 /// If [node] is lazy, the returned node will also be lazy.
162 AssetNodeController.from(AssetNode node) 201 AssetNodeController.from(AssetNode node)
163 : node = new AssetNode._(node.id, node.transform, node.origin) { 202 : node = new AssetNode._(node.id, node.transform, node.origin) {
164 if (node.state.isAvailable) { 203 if (node.state.isAvailable) {
165 setAvailable(node.asset); 204 setAvailable(node.asset);
166 } else if (node.state.isRemoved) { 205 } else if (node.state.isRemoved) {
167 setRemoved(); 206 setRemoved();
168 } 207 }
169 } 208 }
170 209
171 /// Marks the node as [AssetState.DIRTY]. 210 /// Marks the node as [AssetState.DIRTY].
172 void setDirty() { 211 void setDirty() {
173 assert(node._state != AssetState.REMOVED); 212 assert(node._state != AssetState.REMOVED);
174 node._state = AssetState.DIRTY; 213 node._state = AssetState.DIRTY;
175 node._asset = null; 214 node._asset = null;
215 node._lazyCallback = null;
176 node._stateChangeController.add(AssetState.DIRTY); 216 node._stateChangeController.add(AssetState.DIRTY);
177 } 217 }
178 218
179 /// Marks the node as [AssetState.REMOVED]. 219 /// Marks the node as [AssetState.REMOVED].
180 /// 220 ///
181 /// Once a node is marked as removed, it can't be marked as any other state. 221 /// Once a node is marked as removed, it can't be marked as any other state.
182 /// If a new asset is created with the same id, it will get a new node. 222 /// If a new asset is created with the same id, it will get a new node.
183 void setRemoved() { 223 void setRemoved() {
184 assert(node._state != AssetState.REMOVED); 224 assert(node._state != AssetState.REMOVED);
185 node._state = AssetState.REMOVED; 225 node._state = AssetState.REMOVED;
186 node._asset = null; 226 node._asset = null;
227 node._lazyCallback = null;
187 node._stateChangeController.add(AssetState.REMOVED); 228 node._stateChangeController.add(AssetState.REMOVED);
188 } 229 }
189 230
190 /// Marks the node as [AssetState.AVAILABLE] with the given concrete [asset]. 231 /// Marks the node as [AssetState.AVAILABLE] with the given concrete [asset].
191 /// 232 ///
192 /// It's an error to mark an already-available node as available. It should be 233 /// It's an error to mark an already-available node as available. It should be
193 /// marked as dirty first. 234 /// marked as dirty first.
194 void setAvailable(Asset asset) { 235 void setAvailable(Asset asset) {
195 assert(asset.id == node.id); 236 assert(asset.id == node.id);
196 assert(node._state != AssetState.REMOVED); 237 assert(node._state != AssetState.REMOVED);
197 assert(node._state != AssetState.AVAILABLE); 238 assert(node._state != AssetState.AVAILABLE);
198 node._state = AssetState.AVAILABLE; 239 node._state = AssetState.AVAILABLE;
199 node._asset = asset; 240 node._asset = asset;
241 node._lazyCallback = null;
200 node._stateChangeController.add(AssetState.AVAILABLE); 242 node._stateChangeController.add(AssetState.AVAILABLE);
201 } 243 }
244
245 /// Marks the node as [AssetState.DIRTY] and lazy.
246 ///
247 /// Lazy nodes aren't expected to have their values generated until needed.
248 /// Once it's necessary, [callback] will be called. [callback] is guaranteed
249 /// to be called only once.
250 ///
251 /// See also [AssetNodeController.lazy].
252 void setLazy(void callback()) {
253 assert(node._state != AssetState.REMOVED);
254 node._state = AssetState.DIRTY;
255 node._asset = null;
256 node._lazyCallback = callback;
257 node._stateChangeController.add(AssetState.DIRTY);
258 }
202 } 259 }
203 260
204 // TODO(nweiz): add an error state. 261 // TODO(nweiz): add an error state.
205 /// An enum of states that an [AssetNode] can be in. 262 /// An enum of states that an [AssetNode] can be in.
206 class AssetState { 263 class AssetState {
207 /// The node has a concrete asset loaded, available, and up-to-date. The asset 264 /// The node has a concrete asset loaded, available, and up-to-date. The asset
208 /// is accessible via [AssetNode.asset]. An asset can only be marked available 265 /// is accessible via [AssetNode.asset]. An asset can only be marked available
209 /// again from the [AssetState.DIRTY] state. 266 /// again from the [AssetState.DIRTY] state.
210 static final AVAILABLE = const AssetState._("available"); 267 static final AVAILABLE = const AssetState._("available");
211 268
(...skipping 13 matching lines...) Expand all
225 282
226 /// Whether this state is [AssetState.DIRTY]. 283 /// Whether this state is [AssetState.DIRTY].
227 bool get isDirty => this == AssetState.DIRTY; 284 bool get isDirty => this == AssetState.DIRTY;
228 285
229 final String name; 286 final String name;
230 287
231 const AssetState._(this.name); 288 const AssetState._(this.name);
232 289
233 String toString() => name; 290 String toString() => name;
234 } 291 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698