Skip to content

Commit 6842c09

Browse files
committed
Refactor code
1 parent 61fae04 commit 6842c09

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/main/java/com/pusher/client/channel/Channel.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ public interface Channel {
1717
*/
1818
String getName();
1919

20-
/**
21-
* Gets the current subscription count of the subscribed channel.
22-
*
23-
* @return Count of subscriptions
24-
*/
25-
Integer getCount();
26-
2720
/**
2821
* Binds a {@link SubscriptionEventListener} to an event. The
2922
* {@link SubscriptionEventListener} will be notified whenever the specified

src/main/java/com/pusher/client/channel/impl/BaseChannel.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.HashMap;
44
import java.util.HashSet;
5-
import java.util.LinkedHashMap;
65
import java.util.Map;
76
import java.util.Set;
87

@@ -11,19 +10,22 @@
1110
import com.google.gson.GsonBuilder;
1211
import com.pusher.client.channel.*;
1312
import com.pusher.client.channel.impl.message.SubscribeMessage;
13+
import com.pusher.client.channel.impl.message.SubscriptionCountData;
1414
import com.pusher.client.channel.impl.message.UnsubscribeMessage;
1515
import com.pusher.client.util.Factory;
1616

1717
public abstract class BaseChannel implements InternalChannel {
1818
protected final Gson GSON;
1919
private static final String INTERNAL_EVENT_PREFIX = "pusher_internal:";
2020
protected static final String SUBSCRIPTION_SUCCESS_EVENT = "pusher_internal:subscription_succeeded";
21+
protected static final String SUBSCRIPTION_COUNT_EVENT = "pusher_internal:subscription_count";
2122
private Set<SubscriptionEventListener> globalListeners = new HashSet<SubscriptionEventListener>();
2223
private final Map<String, Set<SubscriptionEventListener>> eventNameToListenerMap = new HashMap<String, Set<SubscriptionEventListener>>();
2324
protected volatile ChannelState state = ChannelState.INITIAL;
2425
private ChannelEventListener eventListener;
2526
private final Factory factory;
2627
private final Object lock = new Object();
28+
private Integer subscriptionCount;
2729

2830
public BaseChannel(final Factory factory) {
2931
GsonBuilder gsonBuilder = new GsonBuilder();
@@ -37,6 +39,11 @@ public BaseChannel(final Factory factory) {
3739
@Override
3840
abstract public String getName();
3941

42+
@Override
43+
public Integer getCount() {
44+
return subscriptionCount;
45+
}
46+
4047
@Override
4148
public void bind(final String eventName, final SubscriptionEventListener listener) {
4249
validateArguments(eventName, listener);
@@ -112,6 +119,8 @@ public PusherEvent prepareEvent(String event, String message) {
112119
public void onMessage(String event, String message) {
113120
if (event.equals(SUBSCRIPTION_SUCCESS_EVENT)) {
114121
updateState(ChannelState.SUBSCRIBED);
122+
}else if (event.equals(SUBSCRIPTION_COUNT_EVENT)) {
123+
handleSubscriptionCountEvent(message);
115124
} else {
116125
final Set<SubscriptionEventListener> listeners = getInterestedListeners(event);
117126
if (listeners != null) {
@@ -184,6 +193,20 @@ private void validateArguments(final String eventName, final SubscriptionEventLi
184193
}
185194
}
186195

196+
private void handleSubscriptionCountEvent(final String message) {
197+
String channelName = this.getName();
198+
final SubscriptionCountData subscriptionCountMessage = GSON.fromJson(message, SubscriptionCountData.class);
199+
subscriptionCount = subscriptionCountMessage.getCount();
200+
if (eventListener != null ) {
201+
factory.queueOnEventThread(new Runnable() {
202+
@Override
203+
public void run() {
204+
eventListener.onSubscriptionCountChanged(channelName, subscriptionCountMessage.getCount());
205+
}
206+
});
207+
}
208+
}
209+
187210
protected Set<SubscriptionEventListener> getInterestedListeners(String event) {
188211
synchronized (lock) {
189212
Set<SubscriptionEventListener> listeners = new HashSet<SubscriptionEventListener>();

src/main/java/com/pusher/client/channel/impl/InternalChannel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public interface InternalChannel extends Channel, Comparable<InternalChannel> {
1111

1212
String toUnsubscribeMessage();
1313

14+
Integer getCount();
15+
1416
PusherEvent prepareEvent(String event, String message);
1517

1618
void onMessage(String event, String message);

0 commit comments

Comments
 (0)