Skip to content
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

[ECO-5002] Fix usePresence won't leave presence if unmount triggered during channel attaching #1884

Merged
merged 1 commit into from
Oct 4, 2024
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
8 changes: 5 additions & 3 deletions src/platform/react-hooks/src/hooks/usePresence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ export function usePresence<T = any>(
return () => {
// here we use the ably.connection.state property, which upon this cleanup function call
// will have the current connection state for that connection, thanks to us accessing the Ably instance here by reference.
// if the connection is in one of the inactive states or the channel is not attached, a presence.leave call will produce an exception.
// so we only leave presence in other cases.
if (channel.state === 'attached' && !INACTIVE_CONNECTION_STATES.includes(ably.connection.state)) {
// if the connection is in one of the inactive states or the channel is not attached/attaching, a presence.leave call will produce an exception.
// so we should only leave presence in other cases.
const canLeaveFromConnectionState = !INACTIVE_CONNECTION_STATES.includes(ably.connection.state);
const canLeaveFromChannelState = ['attached', 'attaching'].includes(channel.state);
if (canLeaveFromChannelState && canLeaveFromConnectionState) {
VeskeR marked this conversation as resolved.
Show resolved Hide resolved
channel.presence.leave();
}
};
Expand Down
Loading