From 363d77c7548fd99d378ae8cf6b6c428cb1446597 Mon Sep 17 00:00:00 2001 From: Quentin Mazars-Simon Date: Sun, 8 Jan 2023 18:07:49 +0100 Subject: [PATCH] Remove `on('stateChange')` listener once poweredOn (#24) Remove the listener so we start fresh each time and we try to avoid the ``` MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 stateChange listeners added to [Noble]. Use emitter.setMaxListeners() to increase limit ``` warning Might help for #20 --- src/aranet.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/aranet.ts b/src/aranet.ts index de3f85a..7cfd501 100644 --- a/src/aranet.ts +++ b/src/aranet.ts @@ -63,12 +63,15 @@ export class Aranet4Device { return resolve(true); } - noble.on('stateChange', async (state) => { + const stateChangeHandler = async (state) => { logger.debug(state); if (state === 'poweredOn') { + // Remove listener to avoid `Possible EventEmitter memory leak detected` warning + noble.removeListener('stateChange', stateChangeHandler); return resolve(true); } - }); + }; + noble.on('stateChange', stateChangeHandler); }); }