Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library barback.asset_forwarder; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'asset_node.dart'; | |
| 10 | |
| 11 /// A wrapper for an [AssetNode] that forwards events to a new node. | |
| 12 /// | |
| 13 /// This allows the output node to be marked as removed without the input node | |
| 14 /// having been removed. | |
|
Bob Nystrom
2013/08/21 19:47:38
Without knowing any larger context, this doesn't e
nweiz
2013/08/21 20:33:51
Done.
| |
| 15 class AssetForwarder { | |
| 16 /// The subscription on the input node. | |
| 17 StreamSubscription _subscription; | |
| 18 | |
| 19 /// The controller for the output node. | |
| 20 final AssetNodeController _controller; | |
| 21 | |
| 22 /// The node to which events are forwarded. | |
| 23 AssetNode get node => _controller.node; | |
| 24 | |
| 25 AssetForwarder(AssetNode node) | |
| 26 : _controller = new AssetNodeController(node.id, node.transform) { | |
| 27 _subscription = node.onStateChange.listen((state) { | |
| 28 if (state.isAvailable) { | |
| 29 _controller.setAvailable(node.asset); | |
| 30 } else if (state.isDirty) { | |
| 31 _controller.setDirty(); | |
| 32 } else { | |
| 33 assert(state.isRemoved); | |
| 34 close(); | |
| 35 } | |
| 36 }); | |
| 37 | |
| 38 if (node.state.isAvailable) { | |
| 39 _controller.setAvailable(node.asset); | |
| 40 } else if (node.state.isRemoved) { | |
| 41 close(); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 /// Closes the forwarder and marks [node] as removed. | |
| 46 void close() { | |
| 47 if (_controller.node.state.isRemoved) return; | |
| 48 _subscription.cancel(); | |
| 49 _controller.setRemoved(); | |
| 50 } | |
| 51 } | |
| OLD | NEW |