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:
@@ -145,6 +145,19 @@ export default {
|
|||||||
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 {
|
||||||
branches: [],
|
branches: [],
|
||||||
@@ -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');
|
||||||
|
|||||||
Reference in New Issue
Block a user