p-throttle
Advanced tools
Comparing version 4.1.1 to 5.0.0
112
index.d.ts
@@ -1,17 +0,13 @@ | ||
declare class AbortErrorClass extends Error { | ||
export class AbortError extends Error { | ||
readonly name: 'AbortError'; | ||
/** | ||
Abort pending execution. All unresolved promised are rejected with a `AbortError` error. | ||
*/ | ||
constructor(); | ||
private constructor(); | ||
} | ||
// TODO: Use the `Awaited` utility instead when targeting TS 4.5. | ||
type PromiseResolve<ValueType> = ValueType extends PromiseLike<infer ValueType> ? Promise<ValueType> : Promise<ValueType>; | ||
declare namespace pThrottle { | ||
type ThrottledFunction<Argument extends readonly unknown[], ReturnValue> = (( | ||
...arguments: Argument | ||
) => PromiseResolve<ReturnValue>) & { | ||
/** | ||
export type ThrottledFunction<Argument extends readonly unknown[], ReturnValue> = ((...arguments: Argument) => PromiseResolve<ReturnValue>) | ||
& { | ||
/** | ||
Whether future function calls should be throttled or count towards throttling thresholds. | ||
@@ -21,65 +17,65 @@ | ||
*/ | ||
isEnabled: boolean; | ||
isEnabled: boolean; | ||
/** | ||
/** | ||
Abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error. | ||
*/ | ||
abort(): void; | ||
}; | ||
abort(): void; | ||
}; | ||
interface Options { | ||
/** | ||
Maximum number of calls within an `interval`. | ||
*/ | ||
readonly limit: number; | ||
export interface Options { | ||
/** | ||
The maximum number of calls within an `interval`. | ||
*/ | ||
readonly limit: number; | ||
/** | ||
Timespan for `limit` in milliseconds. | ||
*/ | ||
readonly interval: number; | ||
/** | ||
The timespan for `limit` in milliseconds. | ||
*/ | ||
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. | ||
/** | ||
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; | ||
@default false | ||
*/ | ||
readonly strict?: boolean; | ||
} | ||
declare const pThrottle: { | ||
AbortError: typeof AbortErrorClass; | ||
/** | ||
[Throttle](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning/async/normal functions. | ||
/** | ||
[Throttle](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning/async/normal functions. | ||
@returns A throttle function. | ||
@returns A throttled version of `fn`. | ||
Both the `limit` and `interval` options must be specified. | ||
Both the `limit` and `interval` options must be specified. | ||
@example | ||
``` | ||
import pThrottle from 'p-throttle'; | ||
@example | ||
``` | ||
import pThrottle from 'p-throttle'; | ||
const now = Date.now(); | ||
const throttle = pThrottle({ | ||
limit: 2, | ||
interval: 1000 | ||
}); | ||
const throttle = pThrottle({ | ||
limit: 2, | ||
interval: 1000 | ||
}); | ||
const throttled = throttle(async index => { | ||
return index * 2; | ||
}); | ||
const throttled = throttle(async index => { | ||
const secDiff = ((Date.now() - now) / 1000).toFixed(); | ||
return `${index}: ${secDiff}s`; | ||
}); | ||
for (let i = 1; i <= 6; i++) { | ||
throttled(i).then(console.log); | ||
} | ||
``` | ||
*/ | ||
( | ||
options: pThrottle.Options | ||
): <Argument extends readonly unknown[], ReturnValue>(function_: (...arguments: Argument) => ReturnValue) => pThrottle.ThrottledFunction<Argument, ReturnValue>; | ||
}; | ||
export = pThrottle; | ||
for (let index = 1; index <= 6; index++) { | ||
(async () => { | ||
console.log(await throttled(index)); | ||
})(); | ||
} | ||
//=> 1: 0s | ||
//=> 2: 0s | ||
//=> 3: 1s | ||
//=> 4: 1s | ||
//=> 5: 2s | ||
//=> 6: 2s | ||
``` | ||
*/ | ||
export default function pThrottle(options: Options): <Argument extends readonly unknown[], ReturnValue>(function_: (...arguments: Argument) => ReturnValue) => ThrottledFunction<Argument, ReturnValue>; |
11
index.js
@@ -1,4 +0,2 @@ | ||
'use strict'; | ||
class AbortError extends Error { | ||
export class AbortError extends Error { | ||
constructor() { | ||
@@ -10,3 +8,3 @@ super('Throttled function aborted'); | ||
const pThrottle = ({limit, interval, strict}) => { | ||
export default function pThrottle({limit, interval, strict}) { | ||
if (!Number.isFinite(limit)) { | ||
@@ -100,5 +98,2 @@ throw new TypeError('Expected `limit` to be a finite number'); | ||
}; | ||
}; | ||
module.exports = pThrottle; | ||
module.exports.AbortError = AbortError; | ||
} |
{ | ||
"name": "p-throttle", | ||
"version": "4.1.1", | ||
"version": "5.0.0", | ||
"description": "Throttle promise-returning & async functions", | ||
@@ -13,4 +13,6 @@ "license": "MIT", | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -45,9 +47,9 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"ava": "^3.15.0", | ||
"delay": "^5.0.0", | ||
"in-range": "^2.0.0", | ||
"time-span": "^4.0.0", | ||
"tsd": "^0.14.0", | ||
"xo": "^0.38.1" | ||
"in-range": "^3.0.0", | ||
"time-span": "^5.0.0", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.45.0" | ||
} | ||
} |
@@ -11,5 +11,5 @@ # p-throttle | ||
```sh | ||
npm install p-throttle | ||
``` | ||
$ npm install p-throttle | ||
``` | ||
@@ -21,3 +21,3 @@ ## Usage | ||
```js | ||
const pThrottle = require('p-throttle'); | ||
import pThrottle from 'p-throttle'; | ||
@@ -31,9 +31,11 @@ const now = Date.now(); | ||
const throttled = throttle(index => { | ||
const throttled = throttle(async index => { | ||
const secDiff = ((Date.now() - now) / 1000).toFixed(); | ||
return Promise.resolve(`${index}: ${secDiff}s`); | ||
return `${index}: ${secDiff}s`; | ||
}); | ||
for (let i = 1; i <= 6; i++) { | ||
throttled(i).then(console.log); | ||
for (let index = 1; index <= 6; index++) { | ||
(async () => { | ||
console.log(await throttled(index)); | ||
})(); | ||
} | ||
@@ -52,6 +54,4 @@ //=> 1: 0s | ||
Returns a `throttle` function. | ||
Returns a throttle function. | ||
Returns a throttled version of `fn`. | ||
#### options | ||
@@ -67,3 +67,3 @@ | ||
Maximum number of calls within an `interval`. | ||
The maximum number of calls within an `interval`. | ||
@@ -74,3 +74,3 @@ ##### interval | ||
Timespan for `limit` in milliseconds. | ||
The timespan for `limit` in milliseconds. | ||
@@ -86,2 +86,4 @@ #### strict | ||
Returns a throttled version of `function_`. | ||
#### function_ | ||
@@ -91,3 +93,3 @@ | ||
Promise-returning/async function or a normal function. | ||
A promise-returning/async function or a normal function. | ||
@@ -94,0 +96,0 @@ ### throttledFn.abort() |
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
8420
105
Yes
138