Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnConnectionStateChange event listener not cleared on reset/re-init #167

Open
Agent57 opened this issue Nov 11, 2024 · 0 comments
Open

OnConnectionStateChange event listener not cleared on reset/re-init #167

Agent57 opened this issue Nov 11, 2024 · 0 comments

Comments

@Agent57
Copy link

Agent57 commented Nov 11, 2024

Hi,

When the pusher singleton is reset or re-initialised with an onConnectionStateChange() listener, these listeners are not being removed.

Pusher::init() calls removeAllListeners() prior to adding the new options being passed in, but this doesn't include a call to remove the PusherEventName.ON_CONNECTION_STATE_CHANGE

  private removeAllListeners() {
// Missing?:     this.pusherEventEmitter.removeAllListeners(PusherEventName.ON_CONNECTION_STATE_CHANGE);
    this.pusherEventEmitter.removeAllListeners(PusherEventName.ON_AUTHORIZER);
    this.pusherEventEmitter.removeAllListeners(PusherEventName.ON_ERROR);
    this.pusherEventEmitter.removeAllListeners(PusherEventName.ON_EVENT);
    this.pusherEventEmitter.removeAllListeners(PusherEventName.ON_MEMBER_ADDED);
    this.pusherEventEmitter.removeAllListeners(
      PusherEventName.ON_MEMBER_REMOVED
    );
  }

This results in multiple OnConnectionStateChange handlers being triggered after the re-init...

  const initOptions = {
    apiKey: pusherKey,
    cluster: 'eu',
    onConnectionStateChange: (currentState: string, previousState: string) => {
      console.log(`Pusher connection: ${previousState} => ${currentState}`);
    },
    onSubscriptionSucceeded: (channelName: string) => {
      console.log(`Subscribed: ${channelName}`);
    },
    onSubscriptionError: (channelName: string) => {
      console.log(`Error: ${channelName}`);
    },
    onEvent: (event: PusherEvent) => {
      console.log('Global Event...');
      console.log({event});
    },
    onError(message: string, code: Number, e: any) {
      console.log(`Error ${code}: ${message}`);
      console.log({e});
    },
  };

  await pusher.init(initOptions);
  await pusher.connect();

Resulting in the following output after a number of resets...

 LOG  Pusher connection: DISCONNECTED => CONNECTING
 LOG  Pusher connection: DISCONNECTED => CONNECTING
 LOG  Pusher connection: DISCONNECTED => CONNECTING
 LOG  Pusher connection: CONNECTING => CONNECTED
 LOG  Pusher connection: CONNECTING => CONNECTED
 LOG  Pusher connection: CONNECTING => CONNECTED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant