event-horizon
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -16,3 +16,3 @@ /*global module*/ | ||
for (index = 0; index < max; index += 1) { | ||
if (time - timeArray[index] < conf.max) { | ||
if ((time - timeArray[index]) < conf.window) { | ||
break; // we reached the window again | ||
@@ -26,3 +26,3 @@ } | ||
return { | ||
run: function (func, eatenCB) { | ||
run: function (func, eat) { | ||
var time = new Date().getTime(); | ||
@@ -39,3 +39,3 @@ | ||
if (timeArray.length >= conf.max) { | ||
eatenCB(); | ||
eat(); | ||
return; | ||
@@ -42,0 +42,0 @@ } |
{ | ||
"name": "event-horizon", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Stay below a certain number of things per second", | ||
@@ -11,3 +11,3 @@ "main": "event-horizon.js", | ||
"scripts": { | ||
"test": "gulp test" | ||
"test": "mocha test" | ||
}, | ||
@@ -14,0 +14,0 @@ "keywords": [ |
14
test.js
@@ -32,3 +32,3 @@ /*global require, describe, it, console, setTimeout*/ | ||
it('should use a sliding window', function (done) { | ||
var t = horizon.instance({ window: 20, max: 10 }), | ||
var t = horizon.instance({ window: 100, max: 10 }), | ||
run = 0, | ||
@@ -42,14 +42,14 @@ eaten = 0, | ||
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; }); | ||
}, 18); | ||
}, 70); | ||
setTimeout(function () { | ||
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; }); | ||
assert.equal(10, run); | ||
assert.equal(10, run); | ||
assert.equal(190, eaten); | ||
done(); | ||
}, 21); | ||
}, 105); | ||
}); | ||
it('should reset when I go over window', function (done) { | ||
var t = horizon.instance({ window: 10, max: 10 }), | ||
var t = horizon.instance({ window: 100, max: 10 }), | ||
run = 0, | ||
@@ -69,5 +69,5 @@ eaten = 0, | ||
done(); | ||
}, 11); | ||
}, 11); | ||
}, 102); | ||
}, 102); | ||
}); | ||
}); |
4351