clean up listeners

This commit is contained in:
Marcus Westin 2019-12-31 13:46:10 +01:00
parent c8dae032a9
commit 83dc9924fd

View file

@ -219,7 +219,11 @@ class PlejdService extends EventEmitter {
setTimeout(() => {
if (self.state !== STATE_CONNECTED && self.state !== STATE_AUTHENTICATED) {
if (self.deviceIdx < Object.keys(self.devices).length) {
logger('connection timed out after 10 s. trying next.');
logger('connection timed out after 10 s. cleaning up and trying next.');
self.device.removeAllListeners('servicesDiscover');
self.device.removeAllListeners('connect');
self.device.removeAllListeners('disconnect');
self.deviceIdx++;
self.connect();
@ -240,12 +244,16 @@ class PlejdService extends EventEmitter {
disconnect() {
logger('disconnect()');
if (this.state !== STATE_CONNECTED) {
if (this.state !== STATE_CONNECTED && this.state !== STATE_AUTHENTICATED) {
return;
}
clearInterval(this.pingRef);
this.device.removeAllListeners('servicesDiscover');
this.device.removeAllListeners('connect');
this.device.removeAllListeners('disconnect');
this.unsubscribeCharacteristics();
this.device.disconnect();