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 { decode } from "./protocol";
|
||||||
import {
|
import { type Client, getOrCreateSession, getSession } from "./session";
|
||||||
type Client,
|
|
||||||
getOrCreateSession,
|
|
||||||
getSession,
|
|
||||||
removeSession,
|
|
||||||
} from "./session";
|
|
||||||
|
|
||||||
const PORT = Number(process.env.PORT) || 4040;
|
const PORT = Number(process.env.PORT) || 4040;
|
||||||
|
|
||||||
|
|
@ -66,7 +61,6 @@ Bun.serve({
|
||||||
if (ws.data.room) {
|
if (ws.data.room) {
|
||||||
const session = getSession(ws.data.room);
|
const session = getSession(ws.data.room);
|
||||||
session?.leave(client);
|
session?.leave(client);
|
||||||
removeSession(ws.data.room);
|
|
||||||
ws.data.room = null;
|
ws.data.room = null;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -84,7 +78,6 @@ Bun.serve({
|
||||||
if (ws.data.room && ws.data.client) {
|
if (ws.data.room && ws.data.client) {
|
||||||
const session = getSession(ws.data.room);
|
const session = getSession(ws.data.room);
|
||||||
session?.leave(ws.data.client);
|
session?.leave(ws.data.client);
|
||||||
removeSession(ws.data.room);
|
|
||||||
}
|
}
|
||||||
console.debug("client disconnected");
|
console.debug("client disconnected");
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,10 @@ export class Session {
|
||||||
leave(client: Client) {
|
leave(client: Client) {
|
||||||
this.clients.delete(client);
|
this.clients.delete(client);
|
||||||
this.broadcastPeerCount();
|
this.broadcastPeerCount();
|
||||||
|
if (this.isEmpty()) {
|
||||||
|
sessions.delete(this.name);
|
||||||
|
console.debug(`session removed: ${this.name}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isEmpty(): boolean {
|
isEmpty(): boolean {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue