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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/*! Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
/**
* Icinga.Timer
*
* Timer events are triggered once a second. Runs all reegistered callback
* functions and is able to preserve a desired scope.
*/
(function(Icinga, $) {
'use strict';
Icinga.Timer = function (icinga) {
/**
* We keep a reference to the Icinga instance even if we don't need it
*/
this.icinga = icinga;
/**
* The Interval object
*/
this.ticker = null;
/**
* Fixed default interval is 250ms
*/
this.interval = 250;
/**
* Our registerd observers
*/
this.observers = [];
/**
* Counter
*/
this.stepCounter = 0;
this.start = (new Date()).getTime();
this.lastRuntime = [];
this.isRunning = false;
};
Icinga.Timer.prototype = {
/**
* The initialization function starts our ticker
*/
initialize: function () {
this.isRunning = true;
var _this = this;
var f = function () {
if (_this.isRunning) {
_this.tick();
setTimeout(f, _this.interval);
}
};
f();
},
/**
* We will trigger our tick function once a second. It will call each
* registered observer.
*/
tick: function () {
var icinga = this.icinga;
$.each(this.observers, function (idx, observer) {
if (observer.isDue()) {
observer.run();
} else {
// Not due
}
});
icinga = null;
},
/**
* Register a given callback function to be run within an optional scope.
*/
register: function (callback, scope, interval) {
var observer;
try {
if (typeof scope === 'undefined') {
observer = new Icinga.Timer.Interval(callback, interval);
} else {
observer = new Icinga.Timer.Interval(
callback.bind(scope),
interval
);
}
this.observers.push(observer);
} catch(err) {
this.icinga.logger.error(err);
}
return observer;
},
unregister: function (observer) {
var idx = $.inArray(observer, this.observers);
if (idx > -1) {
this.observers.splice(idx, 1);
}
return this;
},
/**
* Our destroy function will clean up everything. Unused right now.
*/
destroy: function () {
this.isRunning = false;
this.icinga = null;
$.each(this.observers, function (idx, observer) {
observer.destroy();
});
this.observers = [];
}
};
Icinga.Timer.Interval = function (callback, interval) {
if ('undefined' === typeof interval) {
throw 'Timer interval is required';
}
if (interval < 100) {
throw 'Timer interval cannot be less than 100ms, got ' + interval;
}
this.lastRun = (new Date()).getTime();
this.interval = interval;
this.scheduledNextRun = this.lastRun + interval;
this.callback = callback;
};
Icinga.Timer.Interval.prototype = {
isDue: function () {
return this.scheduledNextRun < (new Date()).getTime();
},
run: function () {
this.lastRun = (new Date()).getTime();
while (this.scheduledNextRun < this.lastRun) {
this.scheduledNextRun += this.interval;
}
this.callback();
},
destroy: function () {
this.callback = null;
}
};
}(Icinga, jQuery));
|