Skip to content

Commit

Permalink
Add property to get the queue size (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsau- authored May 12, 2023
1 parent 59732a9 commit 3e7a732
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
15 changes: 10 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ type PromiseResolve<ValueType> = ValueType extends PromiseLike<infer ValueType>
export type ThrottledFunction<Argument extends readonly unknown[], ReturnValue> = ((...arguments: Argument) => PromiseResolve<ReturnValue>)
& {
/**
Whether future function calls should be throttled or count towards throttling thresholds.
Whether future function calls should be throttled or count towards throttling thresholds.
@default true
*/
@default true
*/
isEnabled: boolean;

/**
Abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error.
*/
The number of queued items waiting to be executed.
*/
readonly queueSize: number;

/**
Abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error.
*/
abort(): void;
};

Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ export default function pThrottle({limit, interval, strict}) {

throttled.isEnabled = true;

Object.defineProperty(throttled, 'queueSize', {
get() {
return queue.size;
},
});

return throttled;
};
}
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ strictThrottledLazyUnicorn.abort();
throttledTaggedUnicorn.abort();

expectType<boolean>(throttledUnicorn.isEnabled);
expectType<number>(throttledUnicorn.queueSize);
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ Default: `true`

Whether future function calls should be throttled and count towards throttling thresholds.

### throttledFn.queueSize

Type: `number`

The number of queued items waiting to be executed.

## Related

- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
Expand Down
19 changes: 19 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ test('main', async t => {
}));
});

test('queue size', async t => {
const limit = 10;
const interval = 100;
const throttled = pThrottle({limit, interval})(() => Date.now());
const promises = [];

t.is(throttled.queueSize, 0);

for (let index = 0; index < limit; index++) {
promises.push(throttled());
}

t.is(throttled.queueSize, limit);

await Promise.all(promises);

t.is(throttled.queueSize, 0);
});

test('strict mode', async t => {
const totalRuns = 100;
const limit = 5;
Expand Down

0 comments on commit 3e7a732

Please sign in to comment.