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'; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 new StreamController<AssetState>.broadcast(sync: true); | 76 new StreamController<AssetState>.broadcast(sync: true); |
77 | 77 |
78 /// Calls [callback] when the node's asset is available. | 78 /// Calls [callback] when the node's asset is available. |
79 /// | 79 /// |
80 /// If the asset is currently available, this calls [callback] synchronously | 80 /// If the asset is currently available, this calls [callback] synchronously |
81 /// to ensure that the asset is still available. | 81 /// to ensure that the asset is still available. |
82 /// | 82 /// |
83 /// 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 |
84 /// asset is removed before becoming available, the returned future will throw | 84 /// asset is removed before becoming available, the returned future will throw |
85 /// an [AssetNotFoundException]. | 85 /// an [AssetNotFoundException]. |
86 Future/*<T>*/ whenAvailable/*<T>*/(/*=T*/ callback(Asset asset)) { | 86 Future<T> whenAvailable<T>(T callback(Asset asset)) { |
87 return _waitForState((state) => state.isAvailable || state.isRemoved, | 87 return _waitForState((state) => state.isAvailable || state.isRemoved, |
88 (state) { | 88 (state) { |
89 if (state.isRemoved) throw new AssetNotFoundException(id); | 89 if (state.isRemoved) throw new AssetNotFoundException(id); |
90 return callback(asset); | 90 return callback(asset); |
91 }); | 91 }); |
92 } | 92 } |
93 | 93 |
94 /// Calls [callback] when the node's asset is removed. | 94 /// Calls [callback] when the node's asset is removed. |
95 /// | 95 /// |
96 /// 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... |
107 Future<AssetState> whenStateChanges() { | 107 Future<AssetState> whenStateChanges() { |
108 var startState = state; | 108 var startState = state; |
109 return _waitForState((state) => state != startState, (state) => state); | 109 return _waitForState((state) => state != startState, (state) => state); |
110 } | 110 } |
111 | 111 |
112 /// 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]. |
113 /// | 113 /// |
114 /// [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. |
115 /// | 115 /// |
116 /// The return value of [callback] is piped to the returned Future. | 116 /// The return value of [callback] is piped to the returned Future. |
117 Future/*<T>*/ _waitForState/*<T>*/( | 117 Future<T> _waitForState<T>( |
118 bool test(AssetState state), | 118 bool test(AssetState state), T callback(AssetState state)) { |
119 /*=T*/ callback(AssetState state)) { | |
120 if (test(state)) return new Future.sync(() => 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, |
(...skipping 144 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 |