Skip to content

Commit 9254a65

Browse files
essajifbenevides
andauthored
feat: Add resetPusherInstance function to remove all listeners & subscriptions (#110)
Co-authored-by: Felipe Benevides <felipe.benevides@messagebird.com>
1 parent f73c8f8 commit 9254a65

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

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

7+
78
## 1.2.2
89

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

1213
## 1.2.1

src/index.tsx

+52-17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ const PusherWebsocketReactNative = NativeModules.PusherWebsocketReactNative
1717
}
1818
);
1919

20+
enum EVENT_TYPE {
21+
ON_AUTHORIZER = 'PusherReactNative:onAuthorizer',
22+
ON_CONNECTION_STATE_CHANGE = 'PusherReactNative:onConnectionStateChange',
23+
ON_SUBSCRIPTION_ERROR = 'PusherReactNative:onSubscriptionError',
24+
ON_EVENT = 'PusherReactNative:onEvent',
25+
ON_ERROR = 'PusherReactNative:onError',
26+
ON_MEMBER_ADDED = 'PusherReactNative:onMemberAdded',
27+
ON_MEMBER_REMOVED = 'PusherReactNative:onMemberRemoved',
28+
}
29+
2030
export interface PusherAuthorizerResult {
2131
/** required for private channels */
2232
auth?: string;
@@ -122,8 +132,10 @@ export class Pusher {
122132
return Pusher.instance;
123133
}
124134

125-
private addListener(event: string, callback: (event: any) => void) {
126-
const pusherEventName = `PusherReactNative:${event}`;
135+
private addListener(
136+
pusherEventName: EVENT_TYPE,
137+
callback: (event: any) => void
138+
) {
127139
return this.pusherEventEmitter.addListener(pusherEventName, callback);
128140
}
129141

@@ -162,19 +174,19 @@ export class Pusher {
162174
onMemberAdded?: (channelName: string, member: PusherMember) => void;
163175
onMemberRemoved?: (channelName: string, member: PusherMember) => void;
164176
}) {
165-
this.addListener('onConnectionStateChange', (event: any) => {
177+
this.addListener(EVENT_TYPE.ON_CONNECTION_STATE_CHANGE, (event: any) => {
166178
this.connectionState = event.currentState.toUpperCase();
167179
args.onConnectionStateChange?.(
168180
event.currentState.toUpperCase(),
169181
event.previousState.toUpperCase()
170182
);
171183
});
172184

173-
this.addListener('onError', (event: any) =>
185+
this.addListener(EVENT_TYPE.ON_ERROR, (event: any) =>
174186
args.onError?.(event.message, event.code, event.error)
175187
);
176188

177-
this.addListener('onEvent', (event: any) => {
189+
this.addListener(EVENT_TYPE.ON_EVENT, (event: any) => {
178190
const channelName = event.channelName;
179191
const eventName = event.eventName;
180192
const data = event.data;
@@ -216,7 +228,7 @@ export class Pusher {
216228
}
217229
});
218230

219-
this.addListener('onMemberAdded', (event) => {
231+
this.addListener(EVENT_TYPE.ON_MEMBER_ADDED, (event) => {
220232
const user = event.user;
221233
const channelName = event.channelName;
222234
var member = new PusherMember(user.userId, user.userInfo);
@@ -226,7 +238,7 @@ export class Pusher {
226238
channel?.onMemberAdded?.(member);
227239
});
228240

229-
this.addListener('onMemberRemoved', (event) => {
241+
this.addListener(EVENT_TYPE.ON_MEMBER_REMOVED, (event) => {
230242
const user = event.user;
231243
const channelName = event.channelName;
232244
var member = new PusherMember(user.userId, user.userInfo);
@@ -236,19 +248,22 @@ export class Pusher {
236248
channel?.onMemberRemoved?.(member);
237249
});
238250

239-
this.addListener('onAuthorizer', async ({ channelName, socketId }) => {
240-
const data = await args.onAuthorizer?.(channelName, socketId);
241-
if (data) {
242-
await PusherWebsocketReactNative.onAuthorizer(
243-
channelName,
244-
socketId,
245-
data
246-
);
251+
this.addListener(
252+
EVENT_TYPE.ON_AUTHORIZER,
253+
async ({ channelName, socketId }) => {
254+
const data = await args.onAuthorizer?.(channelName, socketId);
255+
if (data) {
256+
await PusherWebsocketReactNative.onAuthorizer(
257+
channelName,
258+
socketId,
259+
data
260+
);
261+
}
247262
}
248-
});
263+
);
249264

250265
this.addListener(
251-
'onSubscriptionError',
266+
EVENT_TYPE.ON_SUBSCRIPTION_ERROR,
252267
async ({ channelName, message, type }) => {
253268
args.onSubscriptionError?.(channelName, message, type);
254269
}
@@ -277,6 +292,26 @@ export class Pusher {
277292
return await PusherWebsocketReactNative.disconnect();
278293
}
279294

295+
private unsubscribeAllChannels() {
296+
const channelsCopy = new Map(this.channels);
297+
channelsCopy.forEach((channel) => {
298+
this.unsubscribe({ channelName: channel.channelName });
299+
});
300+
}
301+
302+
private removeAllListeners() {
303+
this.pusherEventEmitter.removeAllListeners(EVENT_TYPE.ON_AUTHORIZER);
304+
this.pusherEventEmitter.removeAllListeners(EVENT_TYPE.ON_ERROR);
305+
this.pusherEventEmitter.removeAllListeners(EVENT_TYPE.ON_EVENT);
306+
this.pusherEventEmitter.removeAllListeners(EVENT_TYPE.ON_MEMBER_ADDED);
307+
this.pusherEventEmitter.removeAllListeners(EVENT_TYPE.ON_MEMBER_REMOVED);
308+
}
309+
310+
public async resetPusherInstance() {
311+
this.removeAllListeners();
312+
this.unsubscribeAllChannels();
313+
}
314+
280315
async subscribe(args: {
281316
channelName: string;
282317
onSubscriptionSucceeded?: (data: any) => void;

0 commit comments

Comments
 (0)