blob: 0af1ddcc6798079abc736837b9e5a0461d6b6787 (
plain) (
blame)
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
|
(function(){
function ratelimiter(args) {
this.in_progress = null;
this.needs_triggering = null;
this.callback = args[1];
this.ctx = args[2];
this.wait = args[0];
}
var rlpt = ratelimiter.prototype;
function trigger()
{
var t = this;
setTimeout(function(){
var args = t.in_progress;
t.in_progress = null;
if (t.needs_triggering !== null) {
t.in_progress = t.needs_triggering;
t.needs_triggering = null;
setTimeout(function(){
trigger.call(t);
}, 0);
}
t.callback.apply(t.ctx, args);
}, t.wait);
}
rlpt.kick = function(args){
if (this.in_progress !== null) {
this.needs_triggering = args;
} else {
this.in_progress = args;
trigger.apply(this);
}
};
λ.ratelimiter = function(){
var rl = new ratelimiter(arguments);
return function(){
rl.kick(arguments);
};
};
}());
|