Skip to content

Rename EVENT_TYPE to PusherEventName enum #118

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 2 commits into from
Oct 11, 2023
Merged
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
47 changes: 26 additions & 21 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const PusherWebsocketReactNative = NativeModules.PusherWebsocketReactNative
}
);

enum EVENT_TYPE {
enum PusherEventName {
ON_AUTHORIZER = 'PusherReactNative:onAuthorizer',
ON_CONNECTION_STATE_CHANGE = 'PusherReactNative:onConnectionStateChange',
ON_SUBSCRIPTION_ERROR = 'PusherReactNative:onSubscriptionError',
Expand Down Expand Up @@ -133,7 +133,7 @@ export class Pusher {
}

private addListener(
pusherEventName: EVENT_TYPE,
pusherEventName: PusherEventName,
callback: (event: any) => void
) {
return this.pusherEventEmitter.addListener(pusherEventName, callback);
Expand Down Expand Up @@ -174,19 +174,22 @@ export class Pusher {
onMemberAdded?: (channelName: string, member: PusherMember) => void;
onMemberRemoved?: (channelName: string, member: PusherMember) => void;
}) {
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(
PusherEventName.ON_CONNECTION_STATE_CHANGE,
(event: any) => {
this.connectionState = event.currentState.toUpperCase();
args.onConnectionStateChange?.(
event.currentState.toUpperCase(),
event.previousState.toUpperCase()
);
}
);

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

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

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

this.addListener(EVENT_TYPE.ON_MEMBER_REMOVED, (event) => {
this.addListener(PusherEventName.ON_MEMBER_REMOVED, (event) => {
const user = event.user;
const channelName = event.channelName;
var member = new PusherMember(user.userId, user.userInfo);
Expand All @@ -249,7 +252,7 @@ export class Pusher {
});

this.addListener(
EVENT_TYPE.ON_AUTHORIZER,
PusherEventName.ON_AUTHORIZER,
async ({ channelName, socketId }) => {
const data = await args.onAuthorizer?.(channelName, socketId);
if (data) {
Expand All @@ -263,7 +266,7 @@ export class Pusher {
);

this.addListener(
EVENT_TYPE.ON_SUBSCRIPTION_ERROR,
PusherEventName.ON_SUBSCRIPTION_ERROR,
async ({ channelName, message, type }) => {
args.onSubscriptionError?.(channelName, message, type);
}
Expand Down Expand Up @@ -300,14 +303,16 @@ export class Pusher {
}

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);
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
);
}

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