-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpoll-for-dings.js
More file actions
33 lines (24 loc) · 820 Bytes
/
Copy pathpoll-for-dings.js
File metadata and controls
33 lines (24 loc) · 820 Bytes
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
'use strict'
const SECOND = require( 'time-constants' ).SECOND
// polling every five seconds seems to be the rate the ring app uses:
const POLL_FREQUENCY = 5 * SECOND
module.exports = bottle => bottle.service( 'pollForDings', pollForDings,
'getActiveDings',
'events',
'logger'
)
function pollForDings( getActiveDings, events, logger ) {
const poll = async() => {
const dings = await getActiveDings()
logger( `polling found ${dings.length} active dings`, dings )
dings.forEach( ding => events.emit( 'activity', ding ))
}
let interval
return {
start: () => {
logger( `will poll for dings every ${POLL_FREQUENCY}ms` )
interval = setInterval( poll, POLL_FREQUENCY )
},
stop: () => clearInterval( interval )
}
}