Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions async-kicker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module.exports = Kicker;

/*
Some of the tests in this project rely on the event loop being instant while the
reality is quite different. This helper tries to fix that discrepancy. Usage is as follows:

var kicker = new Kicker(10) // kicks things to the next possible 10 ms

setTimeout(() => kicker.add(), 2) // About +0
setTimeout(() => kicker.add(), 13) // About +10
setTimeout(() => kicker.add(), 25) // About +10
setTimeout(() => kicker.add(), 38) // About +10
setTimeout(() => kicker.add(), 41) // About +0

setTimeout(() => console.log(kicker.kick()), 50) // [0, 10, 20, 30, 30]

*/

function Kicker(snap) {
if (!(this instanceof Kicker)) return new Kicker(snap);
this.snap = snap
this.times = []
this.start = new Date()
}

Kicker.prototype.add = function add() {
this.times.push(new Date() - this.start);
}

Kicker.prototype.kicked = function kick() {
var kickedTimes = []
var time = 0;
var offset = 0;
for (var i = 0; i < this.times.length; i++) {
var time = this.times[i] - offset;
var kicked = Math.floor(time / this.snap) * this.snap;
offset += time - kicked;
time = kicked;
kickedTimes.push(time)
}
return kickedTimes
}
26 changes: 7 additions & 19 deletions throttle/test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
var assert = require('assert');
var sinon = require('sinon');
var Kicker = require('../async-kicker');
var throttle = require('./');

describe('throttle', function() {
var clock;

beforeEach(function () { clock = sinon.useFakeTimers(); });
afterEach(function () { clock.restore(); });

it('executes right away', function() {
var passed = false;
var throttled = throttle(function() {
passed = true;
}, 10);
throttled();
assert(passed);
clock.tick(100);
});

it("won't execute more than once within the threshold", function(done) {
Expand All @@ -30,7 +24,6 @@ describe('throttle', function() {
assert.equal(called, 1);
done();
}, 5);
clock.tick(100);
});

it("will execute at least once more to make up for swallowed calls", function(done) {
Expand All @@ -44,25 +37,21 @@ describe('throttle', function() {
assert.equal(called, 2);
done();
}, 15);
clock.tick(100);
});

it('will execute every threshold ms', function(done) {
var startTime = new Date();
var calledTimes = [];
it('will execute every ${threshold} ms', function(done) {
var kicker = new Kicker(10)
var throttled = throttle(function() {
calledTimes.push(new Date() - startTime);
kicker.add();
}, 10);
throttled(); //start now
var interval = setInterval(throttled, 1);
var interval = setInterval(throttled, 5);
setTimeout(function() {
clearInterval(interval);

assert.deepEqual(calledTimes, [0, 11, 22, 33, 44, 55]);
assert.deepEqual(kicker.kicked(), [0, 10, 20, 30, 40, 50]);

done();
}, 59);
clock.tick(100);
}, 69);

});

Expand Down Expand Up @@ -96,7 +85,6 @@ describe('throttle', function() {
assert.deepEqual(args, [33, 44, 55]);
done();
}, 15)
clock.tick(100);
});


Expand Down