From 7332e6e6c295ce324ebb61765749acbeed4428bb Mon Sep 17 00:00:00 2001 From: Dor Meiri <37194716+dormeiri@users.noreply.github.com> Date: Wed, 14 Aug 2024 15:42:55 +0300 Subject: [PATCH] Pass the given call arguments to `onDelay` (#56) Co-authored-by: Sindre Sorhus --- index.d.ts | 26 +++++++++++++------------- index.js | 2 +- readme.md | 24 ++++++++++++------------ test.js | 17 ++++++++++------- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/index.d.ts b/index.d.ts index 723505f..d916bd8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -44,7 +44,7 @@ export type Options = { readonly strict?: boolean; /** - Get notified when function calls are delayed due to exceeding the `limit` of allowed calls within the given `interval`. + Get notified when function calls are delayed due to exceeding the `limit` of allowed calls within the given `interval`. The delayed call arguments are passed to the `onDelay` callback. Can be useful for monitoring the throttling efficiency. @@ -55,25 +55,25 @@ export type Options = { const throttle = pThrottle({ limit: 2, interval: 1000, - onDelay: () => { - console.log('Reached interval limit, call is delayed'); + onDelay: (a, b) => { + console.log(`Reached interval limit, call is delayed for ${a} ${b}`); }, }); - const throttled = throttle(() => { - console.log('Executing...'); + const throttled = throttle((a, b) => { + console.log(`Executing with ${a} ${b}...`); }); - await throttled(); - await throttled(); - await throttled(); - //=> Executing... - //=> Executing... - //=> Reached interval limit, call is delayed - //=> Executing... + await throttled(1, 2); + await throttled(3, 4); + await throttled(5, 6); + //=> Executing with 1 2... + //=> Executing with 3 4... + //=> Reached interval limit, call is delayed for 5 6 + //=> Executing with 5 6... ``` */ - readonly onDelay?: () => void; + readonly onDelay?: (...arguments_: readonly any[]) => void; }; /** diff --git a/index.js b/index.js index 40ab78f..1d49060 100644 --- a/index.js +++ b/index.js @@ -84,7 +84,7 @@ export default function pThrottle({limit, interval, strict, onDelay}) { if (delay > 0) { timeoutId = setTimeout(execute, delay); queue.set(timeoutId, reject); - onDelay?.(); + onDelay?.(...arguments_); } else { execute(); } diff --git a/readme.md b/readme.md index 6eb9a5c..176ff31 100644 --- a/readme.md +++ b/readme.md @@ -79,7 +79,7 @@ Use a strict, more resource intensive, throttling algorithm. The default algorit Type: `Function` -Get notified when function calls are delayed due to exceeding the `limit` of allowed calls within the given `interval`. +Get notified when function calls are delayed due to exceeding the `limit` of allowed calls within the given `interval`. The delayed call arguments are passed to the `onDelay` callback. Can be useful for monitoring the throttling efficiency. @@ -91,22 +91,22 @@ import pThrottle from 'p-throttle'; const throttle = pThrottle({ limit: 2, interval: 1000, - onDelay: () => { - console.log('Reached interval limit, call is delayed'); + onDelay: (a, b) => { + console.log(`Reached interval limit, call is delayed for ${a} ${b}`); }, }); -const throttled = throttle(() => { - console.log('Executing...'); +const throttled = throttle((a, b) => { + console.log(`Executing with ${a} ${b}...`); }); -await throttled(); -await throttled(); -await throttled(); -//=> Executing... -//=> Executing... -//=> Reached interval limit, call is delayed -//=> Executing... +await throttled(1, 2); +await throttled(3, 4); +await throttled(5, 6); +//=> Executing with 1 2... +//=> Executing with 3 4... +//=> Reached interval limit, call is delayed for 5 6 +//=> Executing with 5 6... ``` ### throttle(function_) diff --git a/test.js b/test.js index 6648ddb..785733b 100644 --- a/test.js +++ b/test.js @@ -402,25 +402,28 @@ test('manages rapid successive calls', async t => { }); test('onDelay', async t => { - let delayedCounter = 0; + const delayedIndices = []; const limit = 10; const interval = 100; const delayedExecutions = 20; - const onDelay = () => delayedCounter++; - const throttled = pThrottle({limit, interval, onDelay})(() => Date.now()); + const onDelay = (keyPrefix, index) => { + delayedIndices.push(keyPrefix + index); + }; + + const throttled = pThrottle({limit, interval, onDelay})((_keyPrefix, _index) => Date.now()); const promises = []; for (let index = 0; index < limit; index++) { - promises.push(throttled()); + promises.push(throttled('a', index)); } - t.is(delayedCounter, 0); + t.deepEqual(delayedIndices, []); for (let index = 0; index < delayedExecutions; index++) { - promises.push(throttled()); + promises.push(throttled('b', index)); } - t.is(delayedCounter, delayedExecutions); + t.like(delayedIndices, {0: 'b0', 1: 'b1', 19: 'b19', 20: undefined}); await Promise.all(promises); });