summaryrefslogtreecommitdiffstats
path: root/web/js/labitrack.d/15-ratelimit.js
diff options
context:
space:
mode:
Diffstat (limited to 'web/js/labitrack.d/15-ratelimit.js')
-rw-r--r--web/js/labitrack.d/15-ratelimit.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/web/js/labitrack.d/15-ratelimit.js b/web/js/labitrack.d/15-ratelimit.js
new file mode 100644
index 0000000..0af1ddc
--- /dev/null
+++ b/web/js/labitrack.d/15-ratelimit.js
@@ -0,0 +1,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);
+ };
+ };
+}());