-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (47 loc) · 1.19 KB
/
index.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
/**
* Allows batching tasks to run in specific intervals
*
* @author: Chris Moyer <[email protected]>
*/
'use strict';
var events = require('events');
var util = require('util');
/**
* Create a new Batcher
*
* @param wait_time: Time to wait before sending the values in the queue (ms)
*/
function Batcher(wait_time){
this.queue = [];
this.wait_time = wait_time;
this.timer = null;
events.EventEmitter.call(this);
}
util.inherits(Batcher, events.EventEmitter);
/**
* Send events if there are any in the queue
*/
Batcher.prototype.send = function send(){
// Clear the timer
this.timer = null;
if(this.queue && this.queue.length > 0){
// Execute the ready emitter
this.emit('ready', this.queue);
// Clear the queue
this.queue = [];
}
};
/**
* Add a new item to the queue
*/
Batcher.prototype.push = function push(val){
this.queue.push(val);
// Start a new timer if one doesn't already exist
// We start it here instead of using setInterval.unref()
// so we make sure our events are ALWAYS processed,
// even if the application exists.
if(this.timer === undefined || this.timer === null){
this.timer = setTimeout(this.send.bind(this), this.wait_time);
}
};
module.exports = Batcher;