Skip to content

Commit

Permalink
Fix for messagesubscriber not being initialized when doing 2 factor l…
Browse files Browse the repository at this point in the history
…ogin
  • Loading branch information
seime committed Feb 6, 2023
1 parent 2274ad7 commit 98ee842
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public class PubNubMessageSubscriber {

private final Set<String> channels = new HashSet<>();

private PubNub pub;
private PubNub pub = null;

private boolean initialized = false;

public void init(String userIdString, PubNubListener messageListener) throws PubNubMessageException {

Expand Down Expand Up @@ -207,13 +209,20 @@ public void file(@NotNull PubNub pubnub, @NotNull PNFileEventResult pnFileEventR
}
});

initialized = true;

} catch (PubNubException e) {
throw new PubNubMessageException("Error creating PubNub subscription", e);
}
}

public synchronized void addListener(String channelName) {

if (!initialized) {
logger.debug("Cannot add listener before component is initialized");
return;
}

boolean added = channels.add(channelName);
if (added) {
logger.debug("Adding listener for channel {}", channelName);
Expand All @@ -224,20 +233,27 @@ public synchronized void addListener(String channelName) {
}

public synchronized void removeListener(String channelName) {
logger.debug("Removing listener for channel {}", channelName);

if (!initialized) {
logger.debug("Cannot remove listener before component is initialized");
return;
}

boolean removed = channels.remove(channelName);
if (removed) {
logger.debug("Removing listener for channel {}", channelName);
pub.unsubscribe().channels(List.of(channelName)).execute();
} else {
logger.warn("Listener for channel {} not found, cannot remove", channelName);
}
}

public void dispose() {
pub.unsubscribeAll();
pub.disconnect();
pub.destroy();
channels.clear();
if (initialized) {
pub.unsubscribeAll();
pub.disconnect();
pub.destroy();
channels.clear();
}
}
}

0 comments on commit 98ee842

Please sign in to comment.