Skip to content

Commit 8b4508a

Browse files
Merge pull request #2 from bryce-seifert/fix/destroyPolling
Fix: Stop polling on module disable
2 parents 2560cd4 + 551b2ce commit 8b4508a

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

index.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var instance_skel = require('../../instance_skel')
1+
const instance_skel = require('../../instance_skel')
22
const actions = require('./actions')
33

44
const WebSocket = require('ws')
@@ -35,6 +35,9 @@ class instance extends instance_skel {
3535
this.ws.close(1000)
3636
delete this.ws
3737
}
38+
if (this.reconnect) {
39+
clearInterval(this.reconnect)
40+
}
3841
}
3942

4043
updateConfig(config) {
@@ -43,7 +46,9 @@ class instance extends instance_skel {
4346
}
4447

4548
initWebSocket() {
46-
clearInterval(this.reconnect)
49+
if (this.reconnect) {
50+
clearInterval(this.reconnect)
51+
}
4752

4853
if (this.ws !== undefined) {
4954
this.ws.close(1000)
@@ -68,23 +73,29 @@ class instance extends instance_skel {
6873
this.ws.on('close', (code) => {
6974
if (code !== 1000) {
7075
this.debug('error', `Websocket closed: ${code}`)
71-
this.reconnect = setInterval(() => {
72-
this.initWebSocket()
73-
}, 500)
7476
}
77+
this.reconnect = setInterval(() => {
78+
this.initWebSocket()
79+
}, 1000)
7580
})
7681

7782
this.ws.on('message', this.messageReceivedFromWebSocket.bind(this))
7883

7984
this.ws.on('error', (data) => {
80-
this.status(this.STATUS_ERROR)
81-
this.log('error', `WebSocket ${data}`)
85+
if (this.currentStatus != 2) {
86+
this.status(this.STATUS_ERROR)
87+
if (data?.code == 'ENOTFOUND') {
88+
this.log('error', `Unable to reach api.vdo.ninja`)
89+
} else {
90+
this.log('error', `WebSocket ${data}`)
91+
}
92+
}
8293
this.ws.close()
8394
})
8495
}
8596

8697
messageReceivedFromWebSocket(data) {
87-
this.log('debug', `Message received: ${data}`)
98+
this.debug(`Message received: ${data}`)
8899
}
89100

90101
config_fields() {

0 commit comments

Comments
 (0)