+4
-2
@@ -21,8 +21,10 @@ 'use strict'; | ||
| throttleIdent = setTimeout(function () { | ||
| var oldQueue = queue; | ||
| queue = []; | ||
| throttleIdent = null; | ||
| resCache = run(function () { | ||
| var cb; | ||
| while (cb = queue.shift()) { | ||
| while (cb = oldQueue.shift()) { | ||
| cb.apply(null, arguments); | ||
| } | ||
| throttleIdent = null; | ||
| }); | ||
@@ -29,0 +31,0 @@ }, expires - Date.now()); |
+1
-1
| { | ||
| "name": "throttling", | ||
| "version": "1.0.0", | ||
| "version": "1.0.1", | ||
| "description": "Throttle a function and cache the result for x milliseconds", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+31
-12
@@ -5,6 +5,6 @@ 'use strict'; | ||
| var test = require('tape'); | ||
| var chrottle = require('./'); | ||
| var throttle = require('./'); | ||
| test('should return the return value of the throttled function', function (t) { | ||
| var run = chrottle(function () { | ||
| var run = throttle(function () { | ||
| return 42; | ||
@@ -18,3 +18,3 @@ }); | ||
| var calls = 0; | ||
| var run = chrottle(function () { | ||
| var run = throttle(function () { | ||
| return ++calls; | ||
@@ -29,3 +29,3 @@ }); | ||
| var calls = 0; | ||
| var run = chrottle(30, function () { | ||
| var run = throttle(30, function () { | ||
| return ++calls; | ||
@@ -44,3 +44,3 @@ }); | ||
| var calls = 0; | ||
| var run = chrottle({ timeout: 30 }, function () { | ||
| var run = throttle({ timeout: 30 }, function () { | ||
| return ++calls; | ||
@@ -59,3 +59,3 @@ }); | ||
| var calls = 0; | ||
| var run = chrottle(30, function (cb) { | ||
| var run = throttle(30, function (cb) { | ||
| cb(++calls); | ||
@@ -78,3 +78,3 @@ }); | ||
| test('should parse on all arugments to the callback', function (t) { | ||
| var run = chrottle(function (cb) { | ||
| var run = throttle(function (cb) { | ||
| cb(1,2,3); | ||
@@ -91,3 +91,3 @@ }); | ||
| test('should allow the runner callback to be optional', function (t) { | ||
| var run = chrottle(function (cb) { | ||
| var run = throttle(function (cb) { | ||
| cb(); | ||
@@ -101,3 +101,3 @@ t.ok(true); | ||
| test('should use the cached arguments for a 2nd call', function (t) { | ||
| var run = chrottle(function (cb) { | ||
| var run = throttle(function (cb) { | ||
| cb(Math.random()); | ||
@@ -114,3 +114,3 @@ }); | ||
| test('should expire the arguments cache after the timeout', function (t) { | ||
| var run = chrottle(30, function (cb) { | ||
| var run = throttle(30, function (cb) { | ||
| cb(Math.random()); | ||
@@ -129,3 +129,3 @@ }); | ||
| test('should clear the caches if an error occurs', function (t) { | ||
| var run = chrottle(function (cb) { | ||
| var run = throttle(function (cb) { | ||
| var rand = Math.random(); | ||
@@ -149,3 +149,3 @@ cb(new Error(), rand); | ||
| test('should throttle instead of cache if opts.wait is set', function (t) { | ||
| var run = chrottle({ timeout: 50, wait: true }, function (cb) { | ||
| var run = throttle({ timeout: 50, wait: true }, function (cb) { | ||
| cb(Math.random()); | ||
@@ -172,1 +172,20 @@ }); | ||
| }); | ||
| test.only('recursive', function (t) { | ||
| var run = throttle({ timeout: 50, wait: true }, function (cb) { | ||
| setTimeout(function () { | ||
| cb(Math.random()); | ||
| }, 10); | ||
| }); | ||
| var times = 5; | ||
| run(function loop (result) { | ||
| if (!--times) { | ||
| setTimeout((function (oldTimes) { | ||
| t.equal(times, oldTimes); | ||
| t.end(); | ||
| process.exit(); | ||
| })(times), 30); | ||
| } | ||
| run(loop); | ||
| }); | ||
| }); |
23191
2.02%221
9.95%