| 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_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 Loading... |
| 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 Loading... |
| 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. |
| 184 /// |
| 185 /// This is used when resolving collisions, where a node will stick around but |
| 186 /// a different transform will have created it. |
| 187 void setTransform(TransformNode transform) { |
| 188 node._transform = transform; |
| 189 } |
| 168 } | 190 } |
| 169 | 191 |
| 170 // TODO(nweiz): add an error state. | 192 // TODO(nweiz): add an error state. |
| 171 /// An enum of states that an [AssetNode] can be in. | 193 /// An enum of states that an [AssetNode] can be in. |
| 172 class AssetState { | 194 class AssetState { |
| 173 /// The node has a concrete asset loaded, available, and up-to-date. The asset | 195 /// 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 | 196 /// is accessible via [AssetNode.asset]. An asset can only be marked available |
| 175 /// again from the [AssetState.DIRTY] state. | 197 /// again from the [AssetState.DIRTY] state. |
| 176 static final AVAILABLE = const AssetState._("available"); | 198 static final AVAILABLE = const AssetState._("available"); |
| 177 | 199 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 191 | 213 |
| 192 /// Whether this state is [AssetState.DIRTY]. | 214 /// Whether this state is [AssetState.DIRTY]. |
| 193 bool get isDirty => this == AssetState.DIRTY; | 215 bool get isDirty => this == AssetState.DIRTY; |
| 194 | 216 |
| 195 final String name; | 217 final String name; |
| 196 | 218 |
| 197 const AssetState._(this.name); | 219 const AssetState._(this.name); |
| 198 | 220 |
| 199 String toString() => name; | 221 String toString() => name; |
| 200 } | 222 } |
| OLD | NEW |