Skip to content

Commit

Permalink
Remove on('stateChange') listener once poweredOn (#24)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
quentinms authored Jan 8, 2023
1 parent 7da9436 commit 363d77c
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/aranet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down

0 comments on commit 363d77c

Please sign in to comment.