Implement daemon socket listener management in BranchView.vue

- Added a watcher for the daemon socket to properly register and unregister message event listeners on socket changes.
- Simplified the event listener setup for handling daemon messages, improving code clarity and maintainability.
- Ensured that listeners are removed during component unmount to prevent memory leaks.
This commit is contained in:
Torsten Schulz (local)
2025-12-01 14:06:18 +01:00
parent f7fdd8ab08
commit 8c8841705c

View File

@@ -144,6 +144,19 @@ export default {
RevenueSection, RevenueSection,
BuyVehicleDialog, BuyVehicleDialog,
}, },
watch: {
// Wenn sich der Daemon-Socket ändert (z.B. nach Login/Reconnect),
// Listener sauber entfernen/neu registrieren.
daemonSocket(newSocket, oldSocket) {
if (oldSocket) {
oldSocket.removeEventListener('message', this.handleDaemonMessage);
}
if (newSocket) {
newSocket.addEventListener('message', this.handleDaemonMessage);
}
}
},
data() { data() {
return { return {
@@ -181,24 +194,9 @@ export default {
} }
// Live-Socket-Events (Daemon WS) // Live-Socket-Events (Daemon WS)
[ if (this.daemonSocket) {
"production_ready", "stock_change", "price_update", this.daemonSocket.addEventListener('message', this.handleDaemonMessage);
"director_death", "production_started", "selled_items", }
"knowledge_update", "falukantUpdateStatus", "falukantBranchUpdate"
].forEach(eventName => {
if (this.daemonSocket) {
this.daemonSocket.addEventListener('message', (event) => {
try {
const data = JSON.parse(event.data);
if (data.event === eventName) {
this.handleEvent(data);
}
} catch (error) {
// Ignore non-JSON messages like ping/pong
}
});
}
});
// Live-Socket-Events (Backend Socket.io) // Live-Socket-Events (Backend Socket.io)
if (this.socket) { if (this.socket) {
@@ -208,7 +206,10 @@ export default {
}, },
beforeUnmount() { beforeUnmount() {
// Daemon WebSocket wird automatisch beim Logout geschlossen // Daemon WebSocket: Listener entfernen (der Socket selbst wird beim Logout geschlossen)
if (this.daemonSocket) {
this.daemonSocket.removeEventListener('message', this.handleDaemonMessage);
}
if (this.socket) { if (this.socket) {
this.socket.off('falukantUpdateStatus'); this.socket.off('falukantUpdateStatus');
this.socket.off('falukantBranchUpdate'); this.socket.off('falukantBranchUpdate');