Skip to content

Commit ee48333

Browse files
authored
Merge pull request #47 from pixie-git/feature/PIX-41_remove-idle-lobby
Feature/pix 41 remove idle lobby
2 parents 01ccd3e + 2e68e84 commit ee48333

6 files changed

Lines changed: 58 additions & 3 deletions

File tree

client/src/services/socket.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ class SocketService {
3838
emitJoinLobby(lobbyName: string) {
3939
this.socket?.emit('JOIN_LOBBY', lobbyName);
4040
}
41+
42+
disconnect() {
43+
if (this.socket) {
44+
this.socket.disconnect();
45+
this.socket = null;
46+
}
47+
}
4148
}
4249

4350
export const socketService = new SocketService();

client/src/stores/editor.store.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,14 @@ export const useEditorStore = defineStore('editor', () => {
229229
});
230230
}
231231

232+
const cleanup = () => {
233+
socketService.disconnect();
234+
isConnected.value = false;
235+
currentLobbyName.value = '';
236+
pixelsBuffer.value = [];
237+
isDrawing.value = false;
238+
};
239+
232240
return {
233241
width,
234242
height,
@@ -247,6 +255,7 @@ export const useEditorStore = defineStore('editor', () => {
247255
endStroke,
248256
clearCanvas,
249257
isConnected,
250-
init
258+
init,
259+
cleanup
251260
};
252261
});

client/src/views/PlayView.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { getLobbyById } from '../services/api';
1111
1212
// Setup Store
1313
const store = useEditorStore();
14-
const { width, height, pixels, palette, selectedColorIndex, isConnected, pixelUpdateEvent } = storeToRefs(store);
14+
const { width, height, pixels, palette, pixelUpdateEvent } = storeToRefs(store);
1515
1616
const route = useRoute();
1717
@@ -34,6 +34,12 @@ onMounted(async () => {
3434
3535
store.init(lobbyName);
3636
});
37+
38+
import { onUnmounted } from 'vue';
39+
40+
onUnmounted(() => {
41+
store.cleanup();
42+
});
3743
</script>
3844

3945
<template>

server/src/services/canvas.service.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,23 @@ export class CanvasService {
9090

9191
console.log(`[CanvasService] Saved lobby '${lobbyName}' to DB`);
9292
}
93+
94+
// Unloads a lobby from memory, persisting it first
95+
static async unloadLobby(lobbyName: string) {
96+
if (!canvasStore.isLobbyInMemory(lobbyName)) return;
97+
98+
console.log(`[CanvasService] Unloading idle lobby: ${lobbyName}`);
99+
100+
// If there's a pending save timer, cancel it and save immediately
101+
if (this.saveTimers.has(lobbyName)) {
102+
clearTimeout(this.saveTimers.get(lobbyName));
103+
this.saveTimers.delete(lobbyName);
104+
}
105+
106+
// Force strict save
107+
await this.saveToDB(lobbyName);
108+
109+
// Remove from memory
110+
canvasStore.removeLobby(lobbyName);
111+
}
93112
}

server/src/sockets/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,20 @@ export const setupSocket = (io: Server) => {
111111

112112

113113
// --- DISCONNECTING ---
114-
socket.on('disconnecting', () => {
114+
socket.on('disconnecting', async () => {
115115
// Notify rooms that user is leaving
116116
for (const room of socket.rooms) {
117117
if (room !== socket.id) {
118118
socket.to(room).emit(CONFIG.EVENTS.SERVER.USER_LEFT, (socket as AuthenticatedSocket).user);
119+
120+
// Check if room is empty (excluding this socket)
121+
const socketsInRoom = await io.in(room).fetchSockets();
122+
const remainingUsers = socketsInRoom.length - 1; // fetchSockets includes the disconnecting socket
123+
124+
if (remainingUsers <= 0) {
125+
// Room is empty, unload from hot storage
126+
await CanvasService.unloadLobby(room);
127+
}
119128
}
120129
}
121130
});

server/src/store/canvas.store.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ export class CanvasStore {
4848
public getInMemoryLobbyIds(): string[] {
4949
return Array.from(this.lobbies.keys());
5050
}
51+
52+
public removeLobby(lobbyName: string): boolean {
53+
console.log(`[CanvasStore] Removing lobby from memory: ${lobbyName}`);
54+
return this.lobbies.delete(lobbyName);
55+
}
5156
}
5257

5358
export const canvasStore = new CanvasStore();

0 commit comments

Comments
 (0)