fix: delete user presence when leaving or on exit (#698)
This commit is contained in:
parent
6cb5b4a75a
commit
a5e2b55294
1 changed files with 14 additions and 32 deletions
|
@ -95,7 +95,12 @@ export function useMultiplayerState(roomId: string) {
|
|||
|
||||
// Handle changes to other users' presence
|
||||
unsubs.push(
|
||||
room.subscribe('others', (others) => {
|
||||
room.subscribe('others', (others, event) => {
|
||||
if (event.type === 'leave') {
|
||||
if (event.user.presence) {
|
||||
app?.removeUser(event.user.presence.id)
|
||||
}
|
||||
} else {
|
||||
app.updateUsers(
|
||||
others
|
||||
.toArray()
|
||||
|
@ -103,33 +108,10 @@ export function useMultiplayerState(roomId: string) {
|
|||
.map((other) => other.presence!.user)
|
||||
.filter(Boolean)
|
||||
)
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
// Handle events from the room
|
||||
unsubs.push(
|
||||
room.subscribe(
|
||||
'event',
|
||||
(e: { connectionId: number; event: { name: string; userId: string } }) => {
|
||||
switch (e.event.name) {
|
||||
case 'exit': {
|
||||
app?.removeUser(e.event.userId)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
// Send the exit event when the tab closes
|
||||
function handleExit() {
|
||||
if (!(room && app?.room)) return
|
||||
room?.broadcastEvent({ name: 'exit', userId: app.room.userId })
|
||||
}
|
||||
|
||||
window.addEventListener('beforeunload', handleExit)
|
||||
unsubs.push(() => window.removeEventListener('beforeunload', handleExit))
|
||||
|
||||
let stillAlive = true
|
||||
|
||||
// Setup the document's storage and subscriptions
|
||||
|
|
Loading…
Reference in a new issue