Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Unified Diff: observatory_pub_packages/barback/src/asset/asset_forwarder.dart

Issue 816693004: Add observatory_pub_packages snapshot to third_party (Closed) Base URL: http://dart.googlecode.com/svn/third_party/
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: observatory_pub_packages/barback/src/asset/asset_forwarder.dart
===================================================================
--- observatory_pub_packages/barback/src/asset/asset_forwarder.dart (revision 0)
+++ observatory_pub_packages/barback/src/asset/asset_forwarder.dart (working copy)
@@ -0,0 +1,49 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library barback.asset.asset_forwarder;
+
+import 'dart:async';
+
+import 'asset_node.dart';
+
+/// A wrapper for an [AssetNode] that forwards events to a new node.
+///
+/// A forwarder is used when a class wants to forward an [AssetNode] that it
+/// gets as an input, but also wants to have control over when that node is
+/// marked as removed. The forwarder can be closed, thus removing its output
+/// node, without the original node having been removed.
+class AssetForwarder {
+ /// The subscription on the input node.
+ StreamSubscription _subscription;
+
+ /// The controller for the output node.
+ final AssetNodeController _controller;
+
+ /// The node to which events are forwarded.
+ AssetNode get node => _controller.node;
+
+ AssetForwarder(AssetNode node)
+ : _controller = new AssetNodeController.from(node) {
+ if (node.state.isRemoved) return;
+
+ _subscription = node.onStateChange.listen((state) {
+ if (state.isAvailable) {
+ _controller.setAvailable(node.asset);
+ } else if (state.isDirty) {
+ _controller.setDirty();
+ } else {
+ assert(state.isRemoved);
+ close();
+ }
+ });
+ }
+
+ /// Closes the forwarder and marks [node] as removed.
+ void close() {
+ if (_controller.node.state.isRemoved) return;
+ _subscription.cancel();
+ _controller.setRemoved();
+ }
+}
« no previous file with comments | « observatory_pub_packages/barback/src/asset/asset.dart ('k') | observatory_pub_packages/barback/src/asset/asset_id.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698