Skip to content

Commit 2e68e84

Browse files
committed
PIX-41 Introduce cleanup logic to disconnect the socket and reset editor state when PlayView unmounts.
1 parent 511eb6a commit 2e68e84

3 files changed

Lines changed: 24 additions & 2 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>

0 commit comments

Comments
 (0)