From 353dcb4b6987c0bb8f726e86af1d39278b7e4dd9 Mon Sep 17 00:00:00 2001 From: Matthijs van Henten Date: Wed, 25 Oct 2017 22:51:44 +0000 Subject: [PATCH] throttle publishing --- node_modules/c9/throttle.js | 18 +++++ node_modules/c9/throttle_test.js | 135 +++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 node_modules/c9/throttle.js create mode 100644 node_modules/c9/throttle_test.js diff --git a/node_modules/c9/throttle.js b/node_modules/c9/throttle.js new file mode 100644 index 00000000..890ab1ee --- /dev/null +++ b/node_modules/c9/throttle.js @@ -0,0 +1,18 @@ +/** + * Return a function that ensures callback is + * not called more often ten once per tpms (times per ms) + */ +function throttle(tpms) { + var last = Date.now(); + + return function(callback) { + var now = Date.now(); + var delay = Math.max(0, last - now); + + last = now + delay + tpms; + + setTimeout(callback, delay); + }; +} + +module.exports = throttle; \ No newline at end of file diff --git a/node_modules/c9/throttle_test.js b/node_modules/c9/throttle_test.js new file mode 100644 index 00000000..b4cc6cd3 --- /dev/null +++ b/node_modules/c9/throttle_test.js @@ -0,0 +1,135 @@ +/*global describe it beforeEach afterEach*/ +"use strict"; +"use server"; +"use mocha"; + +require("c9/inline-mocha")(module); + +var throttle = require("./throttle"); +var sinon = require("sinon"); +var assert = require("assert"); +var async = require("async"); + +describe("urls", function() { + + this.timeout(15000); + + it("calls first invocation with no delay", function(done) { + var tpms = 10; + var throttler = throttle(tpms); + + var start = Date.now(); + + throttler(function() { + var end = Date.now() - start; + assert.ok(end < tpms, "invocation was immediate"); + done(); + }); + }); + + it("calls second invocation delayed by tpms", function(done) { + var tpms = 10; + var throttler = throttle(tpms); + + var start = Date.now(); + var first = sinon.stub(); + + throttler(first); + + throttler(function() { + var end = Date.now() - start; + assert(first.calledOnce); + assert.ok(end >= tpms); + done(); + }); + }); + + it("calls third invocation delayed by 2 * tpms", function(done) { + var tpms = 10; + var throttler = throttle(tpms); + + var start = Date.now(); + var first = sinon.stub(); + var second = sinon.stub(); + + throttler(first); + throttler(second); + + throttler(function() { + var end = Date.now() - start; + assert(first.calledOnce); + assert(second.calledOnce); + + assert.ok(end >= tpms * 2); + done(); + }); + }); + + it("does not delay after enough time has passed", function(done) { + var tpms = 10; + var throttler = throttle(tpms); + + var first = sinon.stub(); + var second = sinon.stub(); + + throttler(first); + throttler(second); + + setTimeout(function() { + var start = Date.now(); + + assert(first.calledOnce); + assert(second.calledOnce); + + throttler(function() { + var end = Date.now() - start; + + assert.ok(end < tpms, "third call was not delayed at all"); + + done(); + }); + }, tpms * 3); + + }); + + it("delays callback chains", function(done) { + var tpms = 30; + var throttler = throttle(tpms); + + var start = Date.now(); + + async.each([1, 2, 3, 4], function(a, next) { + throttler(next); + }, function() { + var end = Date.now() - start; + assert.ok(end >= tpms * 3, "End result was delayed"); + done(); + }); + }); + + it("delays a .series", function(done) { + var tpms = 30; + var throttler = throttle(tpms); + + var start = Date.now(); + + async.series([ + throttler, + throttler, + function(next) { + var end = Date.now() - start; + assert.ok(end >= tpms, "got delayed"); + throttler(next); + } + + ], function() { + var end = Date.now() - start; + assert.ok(end >= tpms * 2, "delayed 2 times"); + done(); + }); + + }); + + + +}); \ No newline at end of file