p-throttle
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -10,7 +10,16 @@ declare class AbortErrorClass extends Error { | ||
type PromiseResolve<ValueType> = ValueType extends PromiseLike<infer ValueType> ? Promise<ValueType> : Promise<ValueType>; | ||
declare namespace pThrottle { | ||
type ThrottledFunction<FunctionType extends (...args: any) => any> = (( | ||
...arguments: Parameters<FunctionType> | ||
) => ReturnType<FunctionType>) & { | ||
type ThrottledFunction<Argument, ReturnValue> = (( | ||
...arguments: Argument[] | ||
) => PromiseResolve<ReturnValue>) & { | ||
/** | ||
Whether future function calls should be throttled or count towards throttling thresholds. | ||
@default true | ||
*/ | ||
isEnabled: boolean; | ||
/** | ||
Abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error. | ||
@@ -25,3 +34,3 @@ */ | ||
*/ | ||
limit: number; | ||
readonly limit: number; | ||
@@ -31,11 +40,13 @@ /** | ||
*/ | ||
interval: number; | ||
readonly interval: number; | ||
/** | ||
Use a strict, more resource intensive, throttling algorithm. The default algorithm uses a windowed approach that will work correctly in most cases, limiting the total number of calls at the specified limit per interval window. The strict algorithm throttles each call individually, ensuring the limit is not exceeded for any interval. | ||
@default false | ||
*/ | ||
readonly strict?: boolean; | ||
} | ||
type AbortError = AbortErrorClass; | ||
/** | ||
@param fn - Promise-returning/async function or a normal function. | ||
*/ | ||
type Throttle<FunctionType extends (...args: any) => any> = (fn: (...arguments: Parameters<FunctionType>) => ReturnType<FunctionType>) => ThrottledFunction<FunctionType>; | ||
} | ||
@@ -71,7 +82,7 @@ | ||
*/ | ||
<FunctionType extends (...args: any) => any>( | ||
( | ||
options: pThrottle.Options | ||
): pThrottle.Throttle<FunctionType>; | ||
): <Argument, ReturnValue>(function_: (...arguments: Argument[]) => ReturnValue) => pThrottle.ThrottledFunction<Argument, ReturnValue>; | ||
}; | ||
export = pThrottle; |
65
index.js
@@ -10,3 +10,3 @@ 'use strict'; | ||
const pThrottle = ({limit, interval}) => { | ||
const pThrottle = ({limit, interval, strict}) => { | ||
if (!Number.isFinite(limit)) { | ||
@@ -25,4 +25,50 @@ throw new TypeError('Expected `limit` to be a finite number'); | ||
function windowedDelay() { | ||
const now = Date.now(); | ||
if ((now - currentTick) > interval) { | ||
activeCount = 1; | ||
currentTick = now; | ||
return 0; | ||
} | ||
if (activeCount < limit) { | ||
activeCount++; | ||
} else { | ||
currentTick += interval; | ||
activeCount = 1; | ||
} | ||
return currentTick - now; | ||
} | ||
const strictTicks = []; | ||
function strictDelay() { | ||
const now = Date.now(); | ||
if (strictTicks.length < limit) { | ||
strictTicks.push(now); | ||
return 0; | ||
} | ||
const earliestTime = strictTicks.shift() + interval; | ||
if (now >= earliestTime) { | ||
strictTicks.push(now); | ||
return 0; | ||
} | ||
strictTicks.push(earliestTime); | ||
return earliestTime - now; | ||
} | ||
const getDelay = strict ? strictDelay : windowedDelay; | ||
return function_ => { | ||
const throttled = function (...args) { | ||
if (!throttled.isEnabled) { | ||
return (async () => function_.apply(this, args))(); | ||
} | ||
let timeout; | ||
@@ -35,16 +81,4 @@ return new Promise((resolve, reject) => { | ||
const now = Date.now(); | ||
timeout = setTimeout(execute, getDelay()); | ||
if ((now - currentTick) > interval) { | ||
activeCount = 1; | ||
currentTick = now; | ||
} else if (activeCount < limit) { | ||
activeCount++; | ||
} else { | ||
currentTick += interval; | ||
activeCount = 1; | ||
} | ||
timeout = setTimeout(execute, currentTick - now); | ||
queue.set(timeout, reject); | ||
@@ -61,4 +95,7 @@ }); | ||
queue.clear(); | ||
strictTicks.splice(0, strictTicks.length); | ||
}; | ||
throttled.isEnabled = true; | ||
return throttled; | ||
@@ -65,0 +102,0 @@ }; |
{ | ||
"name": "p-throttle", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Throttle promise-returning & async functions", | ||
@@ -45,8 +45,8 @@ "license": "MIT", | ||
"ava": "^2.4.0", | ||
"delay": "^4.4.0", | ||
"delay": "^5.0.0", | ||
"in-range": "^2.0.0", | ||
"time-span": "^4.0.0", | ||
"tsd": "^0.14.0", | ||
"xo": "^0.37.1" | ||
"xo": "^0.38.1" | ||
} | ||
} |
@@ -71,2 +71,9 @@ # p-throttle | ||
#### strict | ||
Type: `boolean`\ | ||
Default: `false` | ||
Use a strict, more resource intensive, throttling algorithm. The default algorithm uses a windowed approach that will work correctly in most cases, limiting the total number of calls at the specified limit per interval window. The strict algorithm throttles each call individually, ensuring the limit is not exceeded for any interval. | ||
### throttle(function_) | ||
@@ -84,2 +91,9 @@ | ||
### throttledFn.isEnabled | ||
Type: `boolean`\ | ||
Default: `true` | ||
Whether future function calls should be throttled and count towards throttling thresholds. | ||
## Related | ||
@@ -86,0 +100,0 @@ |
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
8333
143
103