-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttl.js
38 lines (32 loc) · 927 Bytes
/
ttl.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
const EventEmitter = require('events');
class TTLObject extends EventEmitter {
constructor() {
super();
this.received;
this.ttlTimeout = null;
this.values = [];
}
setValues(values) {
this.values = values.values;
this.received = values.received;
};
start() {
let lowest = Math.min.apply(Math, Object.values(this.values));
let received = this.received;
let that = this;
if (received && lowest) {
this.ttlTimeout = setTimeout(function() {
that.emit('serviceDown', {received:received, lowest:lowest, now:Math.round((new Date()).getTime() / 1000)});
//console.log('TTLObject expired received: '+received+', ttl: '+lowest);
}, lowest*1000);
} else {
console.log('mdns-cast-browser, error: TTLObject.start(), without lowest ('+lowest+') or received ('+received+')');
}
};
reset() {
if (this.ttlTimeout) {
clearTimeout(this.ttlTimeout);
}
};
}
module.exports = TTLObject;