| Index: pkg/barback/lib/src/asset_node.dart
|
| diff --git a/pkg/barback/lib/src/asset_node.dart b/pkg/barback/lib/src/asset_node.dart
|
| index a1af547ddddc53cedd25b3f6044bf28cdbdad51d..94b9d2fe4c51041ee56d82f9929569bfe9b5c69f 100644
|
| --- a/pkg/barback/lib/src/asset_node.dart
|
| +++ b/pkg/barback/lib/src/asset_node.dart
|
| @@ -50,6 +50,14 @@ class AssetNode {
|
| Asset get asset => _asset;
|
| Asset _asset;
|
|
|
| + /// The callback to be called to notify this asset node's creator that the
|
| + /// concrete asset should be generated.
|
| + ///
|
| + /// This is null for non-lazy asset nodes (see [AssetNodeController.lazy]).
|
| + /// Once this is called, it's set to null and [this] is no longer considered
|
| + /// lazy.
|
| + Function _lazyCallback;
|
| +
|
| /// A broadcast stream that emits an event whenever the node changes state.
|
| ///
|
| /// This stream is synchronous to ensure that when a source asset is modified
|
| @@ -137,6 +145,22 @@ class AssetNode {
|
| : id = asset.id,
|
| _asset = asset,
|
| _state = AssetState.AVAILABLE;
|
| +
|
| + AssetNode._lazy(this.id, this._transform, this._origin, this._lazyCallback)
|
| + : _state = AssetState.DIRTY;
|
| +
|
| + /// If [this] is lazy, force it to generate a concrete asset; otherwise, do
|
| + /// nothing.
|
| + ///
|
| + /// See [AssetNodeController.lazy].
|
| + void force() {
|
| + if (_origin != null) {
|
| + _origin.force();
|
| + } else if (_lazyCallback != null) {
|
| + _lazyCallback();
|
| + _lazyCallback = null;
|
| + }
|
| + }
|
| }
|
|
|
| /// The controller for an [AssetNode].
|
| @@ -154,11 +178,26 @@ class AssetNodeController {
|
| AssetNodeController.available(Asset asset, [TransformNode transform])
|
| : node = new AssetNode._available(asset, transform, null);
|
|
|
| + /// Creates a controller for a lazy node.
|
| + ///
|
| + /// For the most part, this node works like any other dirty node. However, the
|
| + /// owner of its controller isn't expected to do the work to make it available
|
| + /// as soon as possible like they would for a non-lazy node. Instead, when its
|
| + /// value is needed, [callback] will fire to indicate that it should be made
|
| + /// available as soon as possible.
|
| + ///
|
| + /// [callback] is guaranteed to only fire once.
|
| + AssetNodeController.lazy(AssetId id, void callback(),
|
| + [TransformNode transform])
|
| + : node = new AssetNode._lazy(id, transform, null, callback);
|
| +
|
| /// Creates a controller for a node whose initial state matches the current
|
| /// state of [node].
|
| ///
|
| /// [AssetNode.origin] of the returned node will automatically be set to
|
| /// `node.origin`.
|
| + ///
|
| + /// If [node] is lazy, the returned node will also be lazy.
|
| AssetNodeController.from(AssetNode node)
|
| : node = new AssetNode._(node.id, node.transform, node.origin) {
|
| if (node.state.isAvailable) {
|
| @@ -173,6 +212,7 @@ class AssetNodeController {
|
| assert(node._state != AssetState.REMOVED);
|
| node._state = AssetState.DIRTY;
|
| node._asset = null;
|
| + node._lazyCallback = null;
|
| node._stateChangeController.add(AssetState.DIRTY);
|
| }
|
|
|
| @@ -184,6 +224,7 @@ class AssetNodeController {
|
| assert(node._state != AssetState.REMOVED);
|
| node._state = AssetState.REMOVED;
|
| node._asset = null;
|
| + node._lazyCallback = null;
|
| node._stateChangeController.add(AssetState.REMOVED);
|
| }
|
|
|
| @@ -197,8 +238,24 @@ class AssetNodeController {
|
| assert(node._state != AssetState.AVAILABLE);
|
| node._state = AssetState.AVAILABLE;
|
| node._asset = asset;
|
| + node._lazyCallback = null;
|
| node._stateChangeController.add(AssetState.AVAILABLE);
|
| }
|
| +
|
| + /// Marks the node as [AssetState.DIRTY] and lazy.
|
| + ///
|
| + /// Lazy nodes aren't expected to have their values generated until needed.
|
| + /// Once it's necessary, [callback] will be called. [callback] is guaranteed
|
| + /// to be called only once.
|
| + ///
|
| + /// See also [AssetNodeController.lazy].
|
| + void setLazy(void callback()) {
|
| + assert(node._state != AssetState.REMOVED);
|
| + node._state = AssetState.DIRTY;
|
| + node._asset = null;
|
| + node._lazyCallback = callback;
|
| + node._stateChangeController.add(AssetState.DIRTY);
|
| + }
|
| }
|
|
|
| // TODO(nweiz): add an error state.
|
|
|