throttled-queue
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "throttled-queue", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Throttles arbitrary code to execute a maximum number of times per interval. Best for making throttled API requests.", | ||
@@ -5,0 +5,0 @@ "main": "throttled-queue.js", |
@@ -76,3 +76,3 @@ # throttled-queue | ||
throttle(function() { | ||
// This will fire at most 10 at a time, one after the other. | ||
// This will fire at most 10 a second, as rapidly as possible. | ||
}); | ||
@@ -90,3 +90,3 @@ } | ||
throttle(function() { | ||
// This will fire at most 10 at a time, spacing them out instead of in a burst. | ||
// This will fire at most 10 requests a second, spacing them out instead of in a burst. | ||
}); | ||
@@ -93,0 +93,0 @@ } |
@@ -37,2 +37,6 @@ "use strict"; | ||
if (interval < 200) { | ||
console.warn('An interval of less than 200ms can create performance issues.'); | ||
} | ||
var queue = []; | ||
@@ -60,20 +64,7 @@ var last_called = Date.now(); | ||
var num_requests = 0; | ||
var callbacks = queue.splice(0, max_requests_per_interval); | ||
for(var x = 0; x < callbacks.length; x++) { | ||
callbacks[x](); | ||
} | ||
/** | ||
* Execute the callbacks. | ||
*/ | ||
while (queue.length && num_requests < max_requests_per_interval) { | ||
var callback = queue[num_requests++]; | ||
callback(); | ||
} | ||
/** | ||
* Push the called items off the queue. | ||
*/ | ||
if (num_requests < queue.length) { | ||
queue = queue.slice(num_requests, queue.length); | ||
} else { | ||
queue = []; | ||
} | ||
last_called = Date.now(); | ||
@@ -80,0 +71,0 @@ timeout = setTimeout(dequeue, interval); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42214
17
221