Skip to content

Commit 199614e

Browse files
authored
Don't count aborted jobs in intervalCount (#220)
1 parent 9dff653 commit 199614e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

source/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,10 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
289289
return new Promise((resolve, reject) => {
290290
this.#queue.enqueue(async () => {
291291
this.#pending++;
292-
this.#intervalCount++;
293292

294293
try {
295294
options.signal?.throwIfAborted();
295+
this.#intervalCount++;
296296

297297
let operation = function_({signal: options.signal});
298298

test/test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,26 @@ test('should pass AbortSignal instance to job', async t => {
11161116
}, {signal: controller.signal});
11171117
});
11181118

1119+
test('aborted jobs do not use interval cap', async t => {
1120+
const queue = new PQueue({
1121+
concurrency: 1,
1122+
interval: 100,
1123+
intervalCap: 1,
1124+
});
1125+
1126+
const controller = new AbortController();
1127+
1128+
for (let index = 0; index < 5; index++) {
1129+
queue.add(() => {}, {signal: controller.signal}).catch(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function
1130+
}
1131+
1132+
queue.add(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function
1133+
1134+
controller.abort();
1135+
await delay(150);
1136+
t.is(queue.size, 0);
1137+
});
1138+
11191139
test('aborting multiple jobs at the same time', async t => {
11201140
const queue = new PQueue({concurrency: 1});
11211141

0 commit comments

Comments
 (0)