From 1aee4165b02a0b55f126cb0c8443ca13109d673d Mon Sep 17 00:00:00 2001 From: rory Date: Tue, 21 May 2024 18:32:29 -0700 Subject: [PATCH 1/2] Unsubscribe from pusher channels if there are no more events subscribed --- src/libs/Pusher/pusher.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/libs/Pusher/pusher.ts b/src/libs/Pusher/pusher.ts index 48c5e5c1409f..da8c674101f6 100644 --- a/src/libs/Pusher/pusher.ts +++ b/src/libs/Pusher/pusher.ts @@ -72,6 +72,8 @@ let pusherSocketID = ''; const socketEventCallbacks: SocketEventCallback[] = []; let customAuthorizer: ChannelAuthorizerGenerator; +const eventsBoundToChannels = new Map>(); + /** * Trigger each of the socket event callbacks with the event information */ @@ -153,7 +155,7 @@ function getChannel(channelName: string): Channel | undefined { * Binds an event callback to a channel + eventName */ function bindEventToChannel(channel: Channel | undefined, eventName: EventName, eventCallback: (data: EventData) => void = () => {}) { - if (!eventName) { + if (!eventName || !channel) { return; } @@ -213,7 +215,8 @@ function bindEventToChannel(channel: Channel } }; - channel?.bind(eventName, callback); + channel.bind(eventName, callback); + eventsBoundToChannels.get(channel)?.add(eventName); } /** @@ -288,6 +291,11 @@ function unsubscribe(channelName: string, eventName: PusherEventName = '') { if (eventName) { Log.info('[Pusher] Unbinding event', false, {eventName, channelName}); channel.unbind(eventName); + eventsBoundToChannels.get(channel)?.delete(eventName); + if (eventsBoundToChannels.get(channel)?.size === 0) { + Log.info(`[Pusher] After unbinding ${eventName} from channel ${channelName}, no other events were bound to that channel. Unsubscribing...`, false); + socket?.unsubscribe(channelName); + } } else { if (!channel.subscribed) { Log.info('Pusher] Attempted to unsubscribe from channel, but we are not subscribed to begin with', false, {channelName}); From ee70b5c4159cedf171da7e6fa4694e407d4542ca Mon Sep 17 00:00:00 2001 From: rory Date: Wed, 22 May 2024 11:54:52 -0700 Subject: [PATCH 2/2] Correct add map entries --- src/libs/Pusher/pusher.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libs/Pusher/pusher.ts b/src/libs/Pusher/pusher.ts index da8c674101f6..11aa2b9000d9 100644 --- a/src/libs/Pusher/pusher.ts +++ b/src/libs/Pusher/pusher.ts @@ -216,6 +216,9 @@ function bindEventToChannel(channel: Channel }; channel.bind(eventName, callback); + if (!eventsBoundToChannels.has(channel)) { + eventsBoundToChannels.set(channel, new Set()); + } eventsBoundToChannels.get(channel)?.add(eventName); } @@ -294,6 +297,7 @@ function unsubscribe(channelName: string, eventName: PusherEventName = '') { eventsBoundToChannels.get(channel)?.delete(eventName); if (eventsBoundToChannels.get(channel)?.size === 0) { Log.info(`[Pusher] After unbinding ${eventName} from channel ${channelName}, no other events were bound to that channel. Unsubscribing...`, false); + eventsBoundToChannels.delete(channel); socket?.unsubscribe(channelName); } } else {