From 174eb0c7676a353a684be78f5c46e8bb864d3a78 Mon Sep 17 00:00:00 2001 From: Thomas Nordquist Date: Sat, 23 Feb 2019 22:34:32 +0100 Subject: [PATCH] Fix leaking connections --- backend/src/index.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/backend/src/index.ts b/backend/src/index.ts index ac82d6c..1de9726 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -21,13 +21,18 @@ export class ConnectionManager { public manageConnections() { backendEvents.subscribe(addMqttConnectionEvent, this.handleConnectionRequest) backendEvents.subscribe(removeConnection, (connectionId: string) => { - backendEvents.unsubscribeAll(makePublishEvent(connectionId)) this.removeConnection(connectionId) }) } private handleConnectionRequest = (event: AddMqttConnection) => { const connectionId = event.id + + // Prevent double connections when reloading + if (this.connections[connectionId]) { + this.removeConnection(connectionId) + } + const options = event.options const connection = new MqttSource() this.connections[connectionId] = connection @@ -56,18 +61,19 @@ export class ConnectionManager { }) } - public removeConnection(hash: string) { - const connection = this.connections[hash] + public removeConnection(conenctionId: string) { + const connection = this.connections[conenctionId] if (connection) { + backendEvents.unsubscribeAll(makePublishEvent(conenctionId)) connection.disconnect() - delete this.connections[hash] + delete this.connections[conenctionId] connection.stateMachine.onUpdate.removeAllListeners() } } public closeAllConnections() { Object.keys(this.connections) - .forEach(hash => this.removeConnection(hash)) + .forEach(conenctionId => this.removeConnection(conenctionId)) } }