UpdatedData

 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
function UpdatedData(channel) {
this.channel = channel;
this.values = [];
};
UpdatedData.prototype.softPush = function(value) {
for (var i = 0, len = this.values.length; i < len; i++) {
var item = this.values[i].value;
if (!this.equals(item, value)) continue;
if (item.timer) clearTimeout(item.timer);
this.values[i] = {value: value, copy: duplicate(value)};
value.timer = setTimeout(timeoutFunc, 12*1000, {elem: this.values[i], channel: this.channel});
return;
}
this.values.push({value: value, copy: duplicate(value)});
};
UpdatedData.prototype.equals = function(first, second) {
if (first.plugin === second.plugin &&
first.plugin_instance === second.plugin_instance &&
first.type === second.type &&
first.type_instance === second.type_instance) return true;
return false
};
UpdatedData.prototype.getValues = function() {
var returned = [];
for (var i = 0, len = this.values.length; i < len; i++) {
var item = this.values[i].copy;
returned.push(item);
}
return returned;
};
function timeoutFunc(map) {
map.elem.value.isValid = map.elem.copy.isValid = false;
clearTimeout(map.elem.value.timer);
map.channel.emit('channel_data', JSON.stringify(map.elem.copy));
map.elem.value.timer = setTimeout(timeoutFunc, 12*1000, map);
};
function duplicate(object) {
return JSON.parse(JSON.stringify(object));
};
module.exports = UpdatedData;