Commit b5b67f3f authored by Yusuke Iwaki's avatar Yusuke Iwaki

FIX #84 call unsubscribe on subscription done if needed.

parent 5b35e0f4
...@@ -57,13 +57,16 @@ public class DDPClientWraper { ...@@ -57,13 +57,16 @@ public class DDPClientWraper {
* Subscribe with DDP client. * Subscribe with DDP client.
*/ */
public Task<DDPSubscription.Ready> subscribe(final String name, JSONArray param) { public Task<DDPSubscription.Ready> subscribe(final String name, JSONArray param) {
return ddpClient.sub(UUID.randomUUID().toString(), name, param); final String subscriptionId = UUID.randomUUID().toString();
RCLog.d("sub:[%s]> %s(%s)", subscriptionId, name, param);
return ddpClient.sub(subscriptionId, name, param);
} }
/** /**
* Unsubscribe with DDP client. * Unsubscribe with DDP client.
*/ */
public Task<DDPSubscription.NoSub> unsubscribe(final String subscriptionId) { public Task<DDPSubscription.NoSub> unsubscribe(final String subscriptionId) {
RCLog.d("unsub:[%s]>", subscriptionId);
return ddpClient.unsub(subscriptionId); return ddpClient.unsub(subscriptionId);
} }
......
...@@ -21,6 +21,7 @@ public abstract class AbstractDDPDocEventSubscriber implements Registerable { ...@@ -21,6 +21,7 @@ public abstract class AbstractDDPDocEventSubscriber implements Registerable {
protected final String hostname; protected final String hostname;
protected final RealmHelper realmHelper; protected final RealmHelper realmHelper;
protected final DDPClientWraper ddpClient; protected final DDPClientWraper ddpClient;
private boolean isUnsubscribed;
private String subscriptionId; private String subscriptionId;
private Subscription rxSubscription; private Subscription rxSubscription;
...@@ -53,6 +54,7 @@ public abstract class AbstractDDPDocEventSubscriber implements Registerable { ...@@ -53,6 +54,7 @@ public abstract class AbstractDDPDocEventSubscriber implements Registerable {
protected void onUnregister() {} protected void onUnregister() {}
@Override public final void register() { @Override public final void register() {
isUnsubscribed = false;
JSONArray params = null; JSONArray params = null;
try { try {
params = getSubscriptionParams(); params = getSubscriptionParams();
...@@ -61,7 +63,11 @@ public abstract class AbstractDDPDocEventSubscriber implements Registerable { ...@@ -61,7 +63,11 @@ public abstract class AbstractDDPDocEventSubscriber implements Registerable {
} }
ddpClient.subscribe(getSubscriptionName(), params).onSuccess(task -> { ddpClient.subscribe(getSubscriptionName(), params).onSuccess(task -> {
subscriptionId = task.getResult().id; if (isUnsubscribed) {
ddpClient.unsubscribe(task.getResult().id).continueWith(new LogcatIfError());
} else {
subscriptionId = task.getResult().id;
}
return null; return null;
}).continueWith(task -> { }).continueWith(task -> {
if (task.isFaulted()) { if (task.isFaulted()) {
...@@ -165,6 +171,7 @@ public abstract class AbstractDDPDocEventSubscriber implements Registerable { ...@@ -165,6 +171,7 @@ public abstract class AbstractDDPDocEventSubscriber implements Registerable {
} }
@Override public final void unregister() { @Override public final void unregister() {
isUnsubscribed = true;
onUnregister(); onUnregister();
if (rxSubscription != null) { if (rxSubscription != null) {
rxSubscription.unsubscribe(); rxSubscription.unsubscribe();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment