Skip to content

feat: Add resetPusherInstance function #110

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

* [FIXED] Handle exceptions properly while subscribing to a channel on Android (#104)


## 1.2.2

* [FIXED] Crash when a user subscribes to a channel twice on Android
* [FIXED] Crash when a user subscribes to a channel twice on Android
* [FIXED] Wait for unsubscription before deleting the local channel (#88)

## 1.2.1
Expand Down
69 changes: 52 additions & 17 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ const PusherWebsocketReactNative = NativeModules.PusherWebsocketReactNative
}
);

enum EVENT_TYPE {
ON_AUTHORIZER = 'PusherReactNative:onAuthorizer',
ON_CONNECTION_STATE_CHANGE = 'PusherReactNative:onConnectionStateChange',
ON_SUBSCRIPTION_ERROR = 'PusherReactNative:onSubscriptionError',
ON_EVENT = 'PusherReactNative:onEvent',
ON_ERROR = 'PusherReactNative:onError',
ON_MEMBER_ADDED = 'PusherReactNative:onMemberAdded',
ON_MEMBER_REMOVED = 'PusherReactNative:onMemberRemoved',
}

export interface PusherAuthorizerResult {
/** required for private channels */
auth?: string;
Expand Down Expand Up @@ -122,8 +132,10 @@ export class Pusher {
return Pusher.instance;
}

private addListener(event: string, callback: (event: any) => void) {
const pusherEventName = `PusherReactNative:${event}`;
private addListener(
pusherEventName: EVENT_TYPE,
callback: (event: any) => void
) {
return this.pusherEventEmitter.addListener(pusherEventName, callback);
}

Expand Down Expand Up @@ -162,19 +174,19 @@ export class Pusher {
onMemberAdded?: (channelName: string, member: PusherMember) => void;
onMemberRemoved?: (channelName: string, member: PusherMember) => void;
}) {
this.addListener('onConnectionStateChange', (event: any) => {
this.addListener(EVENT_TYPE.ON_CONNECTION_STATE_CHANGE, (event: any) => {
this.connectionState = event.currentState.toUpperCase();
args.onConnectionStateChange?.(
event.currentState.toUpperCase(),
event.previousState.toUpperCase()
);
});

this.addListener('onError', (event: any) =>
this.addListener(EVENT_TYPE.ON_ERROR, (event: any) =>
args.onError?.(event.message, event.code, event.error)
);

this.addListener('onEvent', (event: any) => {
this.addListener(EVENT_TYPE.ON_EVENT, (event: any) => {
const channelName = event.channelName;
const eventName = event.eventName;
const data = event.data;
Expand Down Expand Up @@ -216,7 +228,7 @@ export class Pusher {
}
});

this.addListener('onMemberAdded', (event) => {
this.addListener(EVENT_TYPE.ON_MEMBER_ADDED, (event) => {
const user = event.user;
const channelName = event.channelName;
var member = new PusherMember(user.userId, user.userInfo);
Expand All @@ -226,7 +238,7 @@ export class Pusher {
channel?.onMemberAdded?.(member);
});

this.addListener('onMemberRemoved', (event) => {
this.addListener(EVENT_TYPE.ON_MEMBER_REMOVED, (event) => {
const user = event.user;
const channelName = event.channelName;
var member = new PusherMember(user.userId, user.userInfo);
Expand All @@ -236,19 +248,22 @@ export class Pusher {
channel?.onMemberRemoved?.(member);
});

this.addListener('onAuthorizer', async ({ channelName, socketId }) => {
const data = await args.onAuthorizer?.(channelName, socketId);
if (data) {
await PusherWebsocketReactNative.onAuthorizer(
channelName,
socketId,
data
);
this.addListener(
EVENT_TYPE.ON_AUTHORIZER,
async ({ channelName, socketId }) => {
const data = await args.onAuthorizer?.(channelName, socketId);
if (data) {
await PusherWebsocketReactNative.onAuthorizer(
channelName,
socketId,
data
);
}
}
});
);

this.addListener(
'onSubscriptionError',
EVENT_TYPE.ON_SUBSCRIPTION_ERROR,
async ({ channelName, message, type }) => {
args.onSubscriptionError?.(channelName, message, type);
}
Expand Down Expand Up @@ -277,6 +292,26 @@ export class Pusher {
return await PusherWebsocketReactNative.disconnect();
}

private unsubscribeAllChannels() {
const channelsCopy = new Map(this.channels);
channelsCopy.forEach((channel) => {
this.unsubscribe({ channelName: channel.channelName });
});
}

private removeAllListeners() {
this.pusherEventEmitter.removeAllListeners(EVENT_TYPE.ON_AUTHORIZER);
this.pusherEventEmitter.removeAllListeners(EVENT_TYPE.ON_ERROR);
this.pusherEventEmitter.removeAllListeners(EVENT_TYPE.ON_EVENT);
this.pusherEventEmitter.removeAllListeners(EVENT_TYPE.ON_MEMBER_ADDED);
this.pusherEventEmitter.removeAllListeners(EVENT_TYPE.ON_MEMBER_REMOVED);
}

public async resetPusherInstance() {
this.removeAllListeners();
this.unsubscribeAllChannels();
}

async subscribe(args: {
channelName: string;
onSubscriptionSucceeded?: (data: any) => void;
Expand Down