From 15c66260eaa45338db48c4af989ff28fa39a588f Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Fri, 31 Oct 2008 18:33:59 +0000 Subject: [PATCH] Take ChangeListeners in SafeSubscriber's constructor that will start listening to the subscribed object when it becomes available and will stop listening when we unsubscribe. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5486 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../presents/util/SafeSubscriber.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/java/com/threerings/presents/util/SafeSubscriber.java b/src/java/com/threerings/presents/util/SafeSubscriber.java index 9872ffc3b..1dc9d2c0e 100644 --- a/src/java/com/threerings/presents/util/SafeSubscriber.java +++ b/src/java/com/threerings/presents/util/SafeSubscriber.java @@ -23,6 +23,7 @@ package com.threerings.presents.util; import com.samskivert.util.StringUtil; +import com.threerings.presents.dobj.ChangeListener; import com.threerings.presents.dobj.DObject; import com.threerings.presents.dobj.DObjectManager; import com.threerings.presents.dobj.ObjectAccessException; @@ -31,10 +32,9 @@ import com.threerings.presents.dobj.Subscriber; import static com.threerings.presents.Log.log; /** - * A class that safely handles the asynchronous subscription to a - * distributed object when it is not know if the subscription will - * complete before the subscriber decides they no longer wish to be - * subscribed. + * A class that safely handles the asynchronous subscription to a distributed object when it is + * not know if the subscription will complete before the subscriber decides they no longer wish to + * be subscribed. * * @param the type of object to which we are subscribing. */ @@ -42,10 +42,12 @@ public class SafeSubscriber implements Subscriber { /** - * Creates a safe subscriber for the specified distributed object - * which will interact with the specified subscriber. + * Creates a safe subscriber for the specified distributed object which will interact with the + * specified subscriber. If any listeners are given, they'll be added as listeners to the + * distributed object after the subscriber is told it's available, and will be removed when + * unsubscribing from the object. */ - public SafeSubscriber (int oid, Subscriber subscriber) + public SafeSubscriber (int oid, Subscriber subscriber, ChangeListener...listeners) { // make sure they're not fucking us around if (oid <= 0) { @@ -149,6 +151,11 @@ public class SafeSubscriber return; } + // clear out any listeners we added + for (ChangeListener listener : _listeners) { + _object.removeListener(listener); + } + // finally effect our unsubscription _object = null; omgr.unsubscribeFromObject(_oid, this); @@ -188,6 +195,9 @@ public class SafeSubscriber // otherwise the air is fresh and clean and we can do our job _object = object; _subscriber.objectAvailable(object); + for (ChangeListener listener : _listeners) { + _object.addListener(listener); + } } // documentation inherited from interface @@ -219,6 +229,7 @@ public class SafeSubscriber ", dobj=" + StringUtil.shortClassName(_object) + "]"; } + protected ChangeListener[] _listeners; protected int _oid; protected Subscriber _subscriber; protected T _object;