OLD | NEW |
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.asset_node; | 5 library barback.asset.asset_node; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import '../errors.dart'; | 9 import '../errors.dart'; |
10 import '../graph/transform_node.dart'; | 10 import '../graph/transform_node.dart'; |
11 import '../utils.dart'; | |
12 import 'asset.dart'; | 11 import 'asset.dart'; |
13 import 'asset_id.dart'; | 12 import 'asset_id.dart'; |
14 | 13 |
15 /// 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. |
16 /// | 15 /// |
17 /// 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 |
18 /// an [onStateChange] stream that emits an event whenever it changes state. | 17 /// an [onStateChange] stream that emits an event whenever it changes state. |
19 /// | 18 /// |
20 /// Asset nodes are controlled using [AssetNodeController]s. | 19 /// Asset nodes are controlled using [AssetNodeController]s. |
21 class AssetNode { | 20 class AssetNode { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 new StreamController<AssetState>.broadcast(sync: true); | 76 new StreamController<AssetState>.broadcast(sync: true); |
78 | 77 |
79 /// Calls [callback] when the node's asset is available. | 78 /// Calls [callback] when the node's asset is available. |
80 /// | 79 /// |
81 /// If the asset is currently available, this calls [callback] synchronously | 80 /// If the asset is currently available, this calls [callback] synchronously |
82 /// to ensure that the asset is still available. | 81 /// to ensure that the asset is still available. |
83 /// | 82 /// |
84 /// The return value of [callback] is piped to the returned Future. If the | 83 /// The return value of [callback] is piped to the returned Future. If the |
85 /// asset is removed before becoming available, the returned future will throw | 84 /// asset is removed before becoming available, the returned future will throw |
86 /// an [AssetNotFoundException]. | 85 /// an [AssetNotFoundException]. |
87 Future whenAvailable(callback(Asset asset)) { | 86 Future/*<T>*/ whenAvailable/*<T>*/(/*=T*/ callback(Asset asset)) { |
88 return _waitForState((state) => state.isAvailable || state.isRemoved, | 87 return _waitForState((state) => state.isAvailable || state.isRemoved, |
89 (state) { | 88 (state) { |
90 if (state.isRemoved) throw new AssetNotFoundException(id); | 89 if (state.isRemoved) throw new AssetNotFoundException(id); |
91 return callback(asset); | 90 return callback(asset); |
92 }); | 91 }); |
93 } | 92 } |
94 | 93 |
95 /// Calls [callback] when the node's asset is removed. | 94 /// Calls [callback] when the node's asset is removed. |
96 /// | 95 /// |
97 /// If the asset is already removed when this is called, it calls [callback] | 96 /// If the asset is already removed when this is called, it calls [callback] |
(...skipping 10 matching lines...) Expand all Loading... |
108 Future<AssetState> whenStateChanges() { | 107 Future<AssetState> whenStateChanges() { |
109 var startState = state; | 108 var startState = state; |
110 return _waitForState((state) => state != startState, (state) => state); | 109 return _waitForState((state) => state != startState, (state) => state); |
111 } | 110 } |
112 | 111 |
113 /// Calls [callback] as soon as the node is in a state that matches [test]. | 112 /// Calls [callback] as soon as the node is in a state that matches [test]. |
114 /// | 113 /// |
115 /// [callback] is called synchronously if this is already in such a state. | 114 /// [callback] is called synchronously if this is already in such a state. |
116 /// | 115 /// |
117 /// The return value of [callback] is piped to the returned Future. | 116 /// The return value of [callback] is piped to the returned Future. |
118 Future _waitForState(bool test(AssetState state), | 117 Future/*<T>*/ _waitForState/*<T>*/(bool test(AssetState state), |
119 callback(AssetState state)) { | 118 /*=T*/ callback(AssetState state)) { |
120 if (test(state)) return syncFuture(() => callback(state)); | 119 if (test(state)) return new Future.sync(() => callback(state)); |
121 return onStateChange.firstWhere(test).then((_) => callback(state)); | 120 return onStateChange.firstWhere(test).then((_) => callback(state)); |
122 } | 121 } |
123 | 122 |
124 AssetNode._(this.id, this._transform, this._origin) | 123 AssetNode._(this.id, this._transform, this._origin) |
125 : _state = AssetState.RUNNING; | 124 : _state = AssetState.RUNNING; |
126 | 125 |
127 AssetNode._available(Asset asset, this._transform, this._origin) | 126 AssetNode._available(Asset asset, this._transform, this._origin) |
128 : id = asset.id, | 127 : id = asset.id, |
129 _asset = asset, | 128 _asset = asset, |
130 _state = AssetState.AVAILABLE; | 129 _state = AssetState.AVAILABLE; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 | 273 |
275 /// Whether this state is [AssetState.RUNNING]. | 274 /// Whether this state is [AssetState.RUNNING]. |
276 bool get isDirty => this == AssetState.RUNNING; | 275 bool get isDirty => this == AssetState.RUNNING; |
277 | 276 |
278 final String name; | 277 final String name; |
279 | 278 |
280 const AssetState._(this.name); | 279 const AssetState._(this.name); |
281 | 280 |
282 String toString() => name; | 281 String toString() => name; |
283 } | 282 } |
OLD | NEW |