Skip to content

Commit

Permalink
fix: do not remove voip listeners for wrong user (#1686)
Browse files Browse the repository at this point in the history
### Overview

Consider this scenario where user A logs out and then user B logs in

1. User B logs in, new event listeners are attached
2. StreamVideo useEffect for A unmounts listeners are removed
3. Now there is no more event listeners

This is fixed by this PR

Additionally: also added a method for clearing logout callbacks
  • Loading branch information
santhoshvai authored Feb 18, 2025
1 parent 03abcd0 commit c6dd17d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import { RxUtils, StreamVideoClient, getLogger } from '@stream-io/video-client';

const logger = getLogger(['useIosVoipPushEventsSetupEffect']);

/* VoipPushNotificationLib has support for only one listener type at a time
hence to support login and logout scenario of multiple users we keep of the last count of the listener that was added
This helps in not removing the listeners when a new user logs in and overrides the last listener
*/
let lastListenerCount = 0;

function setLogoutCallback(
client: StreamVideoClient,
token: string,
Expand Down Expand Up @@ -160,12 +166,19 @@ export const useIosVoipPushEventsSetupEffect = () => {
}
}
});
lastListenerCount += 1;
const currentListenerCount = lastListenerCount;

return () => {
logger(
'debug',
'Voip event listeners are removed for user: ' +
client.streamClient._user?.id
);
const userId = client.streamClient._user?.id;
if (currentListenerCount !== lastListenerCount) {
logger(
'debug',
'Skipped removing voip event listeners for user: ' + userId
);
return;
}
logger('debug', 'Voip event listeners are removed for user: ' + userId);
voipPushNotification.removeEventListener('didLoadWithEvents');
voipPushNotification.removeEventListener('register');
};
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native-sdk/src/utils/StreamVideoRN/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export class StreamVideoRN {
return Promise.resolve();
}

static clearPushLogoutCallbacks() {
pushLogoutCallbacks.current = [];
}

/**
* This function is used to add a callback to be called when a new call notification is received.
* @param callback
Expand Down

0 comments on commit c6dd17d

Please sign in to comment.