Make session cleanup atomic in leave()
This commit is contained in:
parent
2de33370cd
commit
8ce1e098e6
2 changed files with 5 additions and 8 deletions
|
|
@ -1,10 +1,5 @@
|
|||
import { decode } from "./protocol";
|
||||
import {
|
||||
type Client,
|
||||
getOrCreateSession,
|
||||
getSession,
|
||||
removeSession,
|
||||
} from "./session";
|
||||
import { type Client, getOrCreateSession, getSession } from "./session";
|
||||
|
||||
const PORT = Number(process.env.PORT) || 4040;
|
||||
|
||||
|
|
@ -66,7 +61,6 @@ Bun.serve({
|
|||
if (ws.data.room) {
|
||||
const session = getSession(ws.data.room);
|
||||
session?.leave(client);
|
||||
removeSession(ws.data.room);
|
||||
ws.data.room = null;
|
||||
}
|
||||
break;
|
||||
|
|
@ -84,7 +78,6 @@ Bun.serve({
|
|||
if (ws.data.room && ws.data.client) {
|
||||
const session = getSession(ws.data.room);
|
||||
session?.leave(ws.data.client);
|
||||
removeSession(ws.data.room);
|
||||
}
|
||||
console.debug("client disconnected");
|
||||
},
|
||||
|
|
|
|||
|
|
@ -31,6 +31,10 @@ export class Session {
|
|||
leave(client: Client) {
|
||||
this.clients.delete(client);
|
||||
this.broadcastPeerCount();
|
||||
if (this.isEmpty()) {
|
||||
sessions.delete(this.name);
|
||||
console.debug(`session removed: ${this.name}`);
|
||||
}
|
||||
}
|
||||
|
||||
isEmpty(): boolean {
|
||||
|
|
|
|||
Loading…
Reference in a new issue