-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (46 loc) · 1.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict'
const settings = require('standard-settings')
const { SpacebroClient } = require('spacebro-client')
let idle = false
let timer = null
let threshold = settings.get('threshold') || 1000
let lastPing = Date.now()
let events = settings.get('events') || ['*']
(!Array.isArray(events)) && (events = [events])
const client = new SpacebroClient({
host: settings.get('spacebro:host') || '127.0.0.1',
port: settings.get('spacebro:port') || 8888,
channelName: settings.get('spacebro:channelName') || '',
client: {
name: 'idle',
description: 'send an event when idle'
},
verbose: settings.get('verbose') || true
})
function setTimer () {
timer = setTimeout(() => {
const prev = idle
idle = true
client.emit('idle-start')
}, threshold)
}
client.on('newClient', () => {
setTimeout(() => {
client.emit(idle ? 'idle-start' : 'idle-stop')
}, 500)
})
client.on('get-idle-status', () => {
client.emit(idle ? 'idle-start' : 'idle-stop')
})
for (let i = 0; i < events.length; i++) {
const event = events[i]
client.on(event, (datas) => {
clearTimeout(timer)
if (idle) {
client.emit('idle-stop')
idle = false
}
setTimer()
})
}
setTimer()