From 50fe584904699081518417a2a1ef07d96a44a249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20Yer=C5=9Fen?= Date: Thu, 20 May 2021 15:27:35 -0700 Subject: [PATCH] unregister "unhandled" event when socket is closed This addresses the Memory Leak warning mentioned in #223 --- lib/routes.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/routes.js b/lib/routes.js index 5c24697..dccb477 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -100,7 +100,11 @@ const setupRoutes = function setupRoutes() { // #138. handle emitted events that don't have a listener registered, and forward the message // onto the client via the socket - this.on('unhandled', ({ eventName, data }) => send(prep({ action: eventName, data }))); + const unhandled = ({ eventName, data }) => send(prep({ action: eventName, data })); + this.on('unhandled', unhandled); + socket.on('close', () => { + this.off('unhandled', unhandled); + }); send(prep({ action: 'connected' })); }