Skip to content

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

@Agent57

Description

@Agent57

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions