-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathscheduler.js
63 lines (49 loc) · 1.76 KB
/
scheduler.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
57
58
59
60
61
62
63
'use strict';
/**
* Adds ability to postpone the execution of some function.
* If new postpone is requested, old schedule will be cancelling. So max. one schedule can exists in one time.
*
* Configuration:
*
* - Postponing time is defined in `this.config.buffer_seconds`.
* -
*/
function Scheduler(config) {
const self = this;
/** @private */
self._timeoutId = null;
/** @private */
self._totalPostponingSeconds = 0;
/**
* @property {number} buffer_seconds - Postponing time. If it is zero, the callback is always executed immediately.
*
* @property {number} buffer_max_seconds - If is defined, postponning is limited to this total time.
* So when the new postponings are request and it will exceed this value, it will be ignored.
*/
self.config = config;
};
/**
* Plan the postponed execution of callback function.
* If some plan exists, it will be cancelled and replaced by the new one.
*
* @param {function} callback
*/
Scheduler.prototype.schedule = function(callback) {
const self = this;
if (self.config.buffer_max_seconds && (self.config.buffer_max_seconds <= self._totalPostponingSeconds + self.config.buffer_seconds)) {
// Max buffer time reached. Do not replan sending.
return;
}
// If previous sending is planned, cancel it.
if (self._timeoutId) {
clearTimeout(this._timeoutId);
}
// Plan the message sending after timeout
self._timeoutId = setTimeout(function() {
self._timeoutId = null;
self._totalPostponingSeconds = 0;
callback();
}, self.config.buffer_seconds * 1000);
self._totalPostponingSeconds += this.config.buffer_seconds;
};
module.exports = Scheduler;