Skip to content

Commit

Permalink
Pass the given call arguments to onDelay (#56)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
dormeiri and sindresorhus authored Aug 14, 2024
1 parent 0ed167a commit 7332e6e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
26 changes: 13 additions & 13 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
24 changes: 12 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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_)
Expand Down
17 changes: 10 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 7332e6e

Please sign in to comment.