From a949f8dfdbc7c6836e9a299c2f4a3d602bab222c Mon Sep 17 00:00:00 2001 From: Raishav Hanspal Date: Wed, 10 Jul 2024 23:57:35 +0530 Subject: [PATCH 01/19] #208 - add update priority logic, add promise started event --- source/index.ts | 18 ++++++++++++++---- source/priority-queue.ts | 6 ++++++ source/queue.ts | 1 + 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/source/index.ts b/source/index.ts index faf2c9e..eb58d27 100644 --- a/source/index.ts +++ b/source/index.ts @@ -8,7 +8,7 @@ type Task = | ((options: TaskOptions) => PromiseLike) | ((options: TaskOptions) => TaskResultType); -type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error'; +type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error' | 'started'; /** Promise queue with concurrency control. @@ -43,6 +43,8 @@ export default class PQueue(function_: Task, options: {throwOnTimeout: true} & Exclude): Promise; - async add(function_: Task, options?: Partial): Promise; - async add(function_: Task, options: Partial = {}): Promise { + async add(function_: Task, options: { throwOnTimeout: true } & Exclude, uid?: string): Promise; + async add(function_: Task, options?: Partial, uid?: string): Promise; + async add(function_: Task, options: Partial = {}, uid?: string): Promise { + // incase uid is not defined + uid = (this.#uidAssigner++).toString(); options = { timeout: this.timeout, throwOnTimeout: this.#throwOnTimeout, + uid, ...options, }; @@ -258,6 +267,7 @@ export default class PQueue { @@ -32,6 +33,11 @@ export default class PriorityQueue implements Queue) => element.uid === uid); + item && (item.priority = priority || ((item.priority || 0) + 1)); + } + dequeue(): RunFunction | undefined { const item = this.#queue.shift(); return item?.run; diff --git a/source/queue.ts b/source/queue.ts index be3316c..cd325ee 100644 --- a/source/queue.ts +++ b/source/queue.ts @@ -5,4 +5,5 @@ export type Queue = { filter: (options: Readonly>) => Element[]; dequeue: () => Element | undefined; enqueue: (run: Element, options?: Partial) => void; + prioritize: (uid: string, priority: number) => void; }; From 4cdcbf72b3966adfd66c368c9ed91fdf8f169974 Mon Sep 17 00:00:00 2001 From: Raishav Hanspal Date: Thu, 11 Jul 2024 00:05:19 +0530 Subject: [PATCH 02/19] #208 - update for uidAssigner --- source/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/index.ts b/source/index.ts index eb58d27..aecf859 100644 --- a/source/index.ts +++ b/source/index.ts @@ -43,6 +43,7 @@ export default class PQueue(function_: Task, options?: Partial, uid?: string): Promise; async add(function_: Task, options: Partial = {}, uid?: string): Promise { // incase uid is not defined - uid = (this.#uidAssigner++).toString(); + !uid && (uid = (this.#uidAssigner++).toString()); options = { timeout: this.timeout, throwOnTimeout: this.#throwOnTimeout, From 8383aacb2a984f7f2850caf73963c446ab1515d2 Mon Sep 17 00:00:00 2001 From: Raishav Hanspal Date: Thu, 11 Jul 2024 23:56:17 +0530 Subject: [PATCH 03/19] #208 - add test cases, add linting fixes --- source/index.ts | 15 +++++++------ source/priority-queue.ts | 20 +++++++++++++++-- test/test.ts | 46 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/source/index.ts b/source/index.ts index aecf859..bb2071d 100644 --- a/source/index.ts +++ b/source/index.ts @@ -43,8 +43,8 @@ export default class PQueue(function_: Task, options: { throwOnTimeout: true } & Exclude, uid?: string): Promise; + async add(function_: Task, options: {throwOnTimeout: true} & Exclude, uid?: string): Promise; async add(function_: Task, options?: Partial, uid?: string): Promise; async add(function_: Task, options: Partial = {}, uid?: string): Promise { - // incase uid is not defined - !uid && (uid = (this.#uidAssigner++).toString()); + // Incase uid is not defined + if (uid === undefined) { + uid = (this.#uidAssigner++).toString(); + } + options = { timeout: this.timeout, throwOnTimeout: this.#throwOnTimeout, @@ -268,7 +271,7 @@ export default class PQueue) => element.uid === uid); - item && (item.priority = priority || ((item.priority || 0) + 1)); + const queueIndex: number = this.#queue.findIndex((element: Readonly) => element.uid === uid); + const [item] = this.#queue.splice(queueIndex, 1); + if (item === undefined) { + return; + } + + item.priority = priority ?? ((item.priority ?? 0) + 1); + if (this.size && this.#queue[this.size - 1]!.priority! >= priority!) { + this.#queue.push(item); + return; + } + + const index = lowerBound( + this.#queue, item, + (a: Readonly, b: Readonly) => b.priority! - a.priority!, + ); + + this.#queue.splice(index, 0, item); } dequeue(): RunFunction | undefined { diff --git a/test/test.ts b/test/test.ts index d511eae..ad54b68 100644 --- a/test/test.ts +++ b/test/test.ts @@ -6,7 +6,7 @@ import inRange from 'in-range'; import timeSpan from 'time-span'; import randomInt from 'random-int'; import pDefer from 'p-defer'; -import PQueue, {AbortError} from '../source/index.js'; +import PQueue from '../source/index.js'; const fixture = Symbol('fixture'); @@ -1134,3 +1134,47 @@ test('aborting multiple jobs at the same time', async t => { await t.throwsAsync(task2, {instanceOf: DOMException}); t.like(queue, {size: 0, pending: 0}); }); + +test('.setPriority() - execute a promise before planned', async t => { + const result: string[] = []; + const queue = new PQueue({concurrency: 1}); + queue.add(async () => { + await delay(400); + result.push('🐌'); + }, {}, 'snail'); + queue.add(async () => { + await delay(400); + result.push('🦆'); + }, {}, 'duck'); + queue.add(async () => { + await delay(400); + result.push('🐢'); + }, {}, 'turtle'); + queue.setPriority('turtle', 1); + await queue.onIdle(); + t.deepEqual(result, ['🐌', '🐢', '🦆']); +}); + +test('started event to check when promise function is called', async t => { + const result: string[] = []; + const queue = new PQueue({concurrency: 1}); + queue.add(async () => { + await delay(400); + result.push('🐌'); + }, {}, '🐌'); + queue.add(async () => { + await delay(400); + result.push('🦆'); + }, {}, '🦆'); + queue.add(async () => { + await delay(400); + result.push('🐢'); + }, {}, '🐢'); + queue.on('started', uid => { + if (uid === '🦆') { + t.deepEqual(result, ['🐌', '🐢']); + } + }); + queue.setPriority('🐢', 1); + await queue.onIdle(); +}); From a60bfc19a32c1c5a9601e6b64ba54c1ee5f578ef Mon Sep 17 00:00:00 2001 From: Raishav Hanspal Date: Fri, 12 Jul 2024 09:33:03 +0530 Subject: [PATCH 04/19] #208 - review fixes, remove started event changes --- source/index.ts | 22 ++++++++++------------ source/options.ts | 1 + source/priority-queue.ts | 7 +++---- source/queue.ts | 2 +- test/test.ts | 32 ++++---------------------------- 5 files changed, 19 insertions(+), 45 deletions(-) diff --git a/source/index.ts b/source/index.ts index bb2071d..3459f98 100644 --- a/source/index.ts +++ b/source/index.ts @@ -8,7 +8,7 @@ type Task = | ((options: TaskOptions) => PromiseLike) | ((options: TaskOptions) => TaskResultType); -type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error' | 'started'; +type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error'; /** Promise queue with concurrency control. @@ -44,7 +44,7 @@ export default class PQueue(function_: Task, options: {throwOnTimeout: true} & Exclude, uid?: string): Promise; - async add(function_: Task, options?: Partial, uid?: string): Promise; - async add(function_: Task, options: Partial = {}, uid?: string): Promise { - // Incase uid is not defined - if (uid === undefined) { - uid = (this.#uidAssigner++).toString(); + async add(function_: Task, options: {throwOnTimeout: true} & Exclude): Promise; + async add(function_: Task, options?: Partial): Promise; + async add(function_: Task, options: Partial = {}): Promise { + // Incase id is not defined + if (options.id === undefined) { + options.id = (this.#idAssigner++).toString(); } options = { timeout: this.timeout, throwOnTimeout: this.#throwOnTimeout, - uid, ...options, }; @@ -271,7 +270,6 @@ export default class PQueue { @@ -33,9 +32,9 @@ export default class PriorityQueue implements Queue) => element.uid === uid); - const [item] = this.#queue.splice(queueIndex, 1); + setPriority(id: string, priority?: number) { + const existingIndex: number = this.#queue.findIndex((element: Readonly) => element.id === id); + const [item] = this.#queue.splice(existingIndex, 1); if (item === undefined) { return; } diff --git a/source/queue.ts b/source/queue.ts index cd325ee..459d29b 100644 --- a/source/queue.ts +++ b/source/queue.ts @@ -5,5 +5,5 @@ export type Queue = { filter: (options: Readonly>) => Element[]; dequeue: () => Element | undefined; enqueue: (run: Element, options?: Partial) => void; - prioritize: (uid: string, priority: number) => void; + setPriority: (id: string, priority: number) => void; }; diff --git a/test/test.ts b/test/test.ts index ad54b68..da828e0 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1141,40 +1141,16 @@ test('.setPriority() - execute a promise before planned', async t => { queue.add(async () => { await delay(400); result.push('🐌'); - }, {}, 'snail'); + }, {id: '🐌'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {}, 'duck'); + }, {id: '🦆'}); queue.add(async () => { await delay(400); result.push('🐢'); - }, {}, 'turtle'); - queue.setPriority('turtle', 1); - await queue.onIdle(); - t.deepEqual(result, ['🐌', '🐢', '🦆']); -}); - -test('started event to check when promise function is called', async t => { - const result: string[] = []; - const queue = new PQueue({concurrency: 1}); - queue.add(async () => { - await delay(400); - result.push('🐌'); - }, {}, '🐌'); - queue.add(async () => { - await delay(400); - result.push('🦆'); - }, {}, '🦆'); - queue.add(async () => { - await delay(400); - result.push('🐢'); - }, {}, '🐢'); - queue.on('started', uid => { - if (uid === '🦆') { - t.deepEqual(result, ['🐌', '🐢']); - } - }); + }, {id: '🐢'}); queue.setPriority('🐢', 1); await queue.onIdle(); + t.deepEqual(result, ['🐌', '🐢', '🦆']); }); From cafe886eada509b899c39bcb7e09846fffb1bce3 Mon Sep 17 00:00:00 2001 From: Raishav Hanspal Date: Thu, 18 Jul 2024 18:55:26 +0530 Subject: [PATCH 05/19] #208 - review fixes - add test case, update setPriority method --- source/priority-queue.ts | 13 +++++++++---- test/test.ts | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/source/priority-queue.ts b/source/priority-queue.ts index f1115aa..86d1baf 100644 --- a/source/priority-queue.ts +++ b/source/priority-queue.ts @@ -17,10 +17,11 @@ export default class PriorityQueue implements Queue= options.priority!) { + if (this.size === 0 || this.#queue[this.size - 1]!.priority! >= options.priority!) { this.#queue.push(element); return; } @@ -32,15 +33,19 @@ export default class PriorityQueue implements Queue) => element.id === id); + if (existingIndex === -1) { + throw new Error('Invalid Index - No promise function of specified id available in the queue.'); + } + const [item] = this.#queue.splice(existingIndex, 1); if (item === undefined) { return; } - item.priority = priority ?? ((item.priority ?? 0) + 1); - if (this.size && this.#queue[this.size - 1]!.priority! >= priority!) { + item.priority = priority; + if (this.size === 0 || this.#queue[this.size - 1]!.priority! >= priority) { this.#queue.push(item); return; } diff --git a/test/test.ts b/test/test.ts index da828e0..b88efab 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1154,3 +1154,23 @@ test('.setPriority() - execute a promise before planned', async t => { await queue.onIdle(); t.deepEqual(result, ['🐌', '🐢', '🦆']); }); + +test.failing('.setPriority() - with invalid "id"', async t => { + const result: string[] = []; + const queue = new PQueue({concurrency: 1}); + queue.add(async () => { + await delay(400); + result.push('🐌'); + }, {id: '🐌'}); + queue.add(async () => { + await delay(400); + result.push('🦆'); + }, {id: '🦆'}); + queue.add(async () => { + await delay(400); + result.push('🐢'); + }, {id: '🐢'}); + queue.setPriority('⚡️', 1); + await queue.onIdle(); + t.deepEqual(result, ['🐌', '🐢', '🦆']); +}); From 6a370fe71e9b911ae3b8406df3b3e7d8eaf84b36 Mon Sep 17 00:00:00 2001 From: Raishav Hanspal Date: Fri, 19 Jul 2024 07:23:22 +0530 Subject: [PATCH 06/19] #208 - throw error if undefined item in queue while setting priority --- source/priority-queue.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/priority-queue.ts b/source/priority-queue.ts index 86d1baf..9fa824d 100644 --- a/source/priority-queue.ts +++ b/source/priority-queue.ts @@ -35,13 +35,13 @@ export default class PriorityQueue implements Queue) => element.id === id); - if (existingIndex === -1) { + if (existingIndex === -1 ) { throw new Error('Invalid Index - No promise function of specified id available in the queue.'); } const [item] = this.#queue.splice(existingIndex, 1); if (item === undefined) { - return; + throw new Error('Undefined Item - No promise function of specified id available in the queue.'); } item.priority = priority; From ccb7a5398619216e6610d21f22ef8066edea2276 Mon Sep 17 00:00:00 2001 From: Raishav Hanspal Date: Fri, 19 Jul 2024 07:25:27 +0530 Subject: [PATCH 07/19] #208 - update spacing --- source/priority-queue.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/priority-queue.ts b/source/priority-queue.ts index 9fa824d..dd427ea 100644 --- a/source/priority-queue.ts +++ b/source/priority-queue.ts @@ -35,7 +35,7 @@ export default class PriorityQueue implements Queue) => element.id === id); - if (existingIndex === -1 ) { + if (existingIndex === -1) { throw new Error('Invalid Index - No promise function of specified id available in the queue.'); } From 686f0d8e21efc1cae0b4c855d6a1eab58258d436 Mon Sep 17 00:00:00 2001 From: Raishav Hanspal Date: Fri, 19 Jul 2024 07:38:37 +0530 Subject: [PATCH 08/19] #208 - use enqueue function in setPriority --- source/priority-queue.ts | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/source/priority-queue.ts b/source/priority-queue.ts index dd427ea..cc46b88 100644 --- a/source/priority-queue.ts +++ b/source/priority-queue.ts @@ -44,18 +44,7 @@ export default class PriorityQueue implements Queue= priority) { - this.#queue.push(item); - return; - } - - const index = lowerBound( - this.#queue, item, - (a: Readonly, b: Readonly) => b.priority! - a.priority!, - ); - - this.#queue.splice(index, 0, item); + this.enqueue(item.run, {priority, id}); } dequeue(): RunFunction | undefined { From ecc003da5f71aa9b2bd042c2137389d76ccd78df Mon Sep 17 00:00:00 2001 From: Raishav Hanspal Date: Thu, 21 Nov 2024 00:29:03 +0530 Subject: [PATCH 09/19] feedback fixes - add test cases, update code comments, update readme --- readme.md | 38 ++++++++++++ source/index.ts | 13 ++-- source/options.ts | 7 +++ source/priority-queue.ts | 6 +- test/test.ts | 126 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 177 insertions(+), 13 deletions(-) diff --git a/readme.md b/readme.md index bf148e1..dcc303a 100644 --- a/readme.md +++ b/readme.md @@ -236,6 +236,44 @@ console.log(queue.sizeBy({priority: 0})); //=> 1 ``` +#### .setPriority(id, priority) + +Update priority of a known promise function, using the `id` identifier, and a priority value to override existing priority value. The updated value of priority ensures whether to execute this promise function sooner or later. + +Function works only when we specify a defined concurrency to change any priorities. + +For example, this can be used to make a promise function run earlier. + +```js +import PQueue from 'p-queue'; + +const queue = new PQueue({concurrency: 1}); + +queue.add(async () => '🦄', {priority: 1}); +queue.add(async () => '🦀', {priority: 0, id: '🦀'}); +queue.add(async () => '🦄', {priority: 1}); +queue.add(async () => '🦄', {priority: 1}); + +queue.setPriority('🦀', 2); +``` +In above case, promise function with '🦀' executes second. + +We can also delay a promise function. + +```js +import PQueue from 'p-queue'; + +const queue = new PQueue({concurrency: 1}); + +queue.add(async () => '🦄', {priority: 1}); +queue.add(async () => '🦀', {priority: 1, id: '🦀'}); +queue.add(async () => '🦄'); +queue.add(async () => '🦄', {priority: 0}); + +queue.setPriority('🦀', -1); +``` +In above case, promise function with '🦀' executes last. + #### .pending Number of running items (no longer in the queue). diff --git a/source/index.ts b/source/index.ts index 3459f98..65331de 100644 --- a/source/index.ts +++ b/source/index.ts @@ -43,8 +43,8 @@ export default class PQueue(function_: Task, options: {throwOnTimeout: true} & Exclude): Promise; async add(function_: Task, options?: Partial): Promise; async add(function_: Task, options: Partial = {}): Promise { - // Incase id is not defined - if (options.id === undefined) { - options.id = (this.#idAssigner++).toString(); - } + // In case `id` is not defined. + options.id ??= (this.#idAssigner++).toString(); options = { timeout: this.timeout, diff --git a/source/options.ts b/source/options.ts index 37f5a9e..7bc4c64 100644 --- a/source/options.ts +++ b/source/options.ts @@ -69,6 +69,13 @@ export type QueueAddOptions = { @default 0 */ readonly priority?: number; + /** + Unique identifier for the promise function. This can be used to update priority, before it gets executed. + + @default '1' + + Value of `id` will be assigned using a bigint number assigner which increments for every new promise function with unspecified id + */ id?: string; } & TaskOptions & TimeoutOptions; diff --git a/source/priority-queue.ts b/source/priority-queue.ts index cc46b88..90148e6 100644 --- a/source/priority-queue.ts +++ b/source/priority-queue.ts @@ -40,11 +40,7 @@ export default class PriorityQueue implements Queue { t.deepEqual(result, ['🐌', '🐢', '🦆']); }); -test.failing('.setPriority() - with invalid "id"', async t => { +test('.setPriority() - execute a promise after planned', async t => { const result: string[] = []; const queue = new PQueue({concurrency: 1}); queue.add(async () => { @@ -1166,11 +1166,133 @@ test.failing('.setPriority() - with invalid "id"', async t => { await delay(400); result.push('🦆'); }, {id: '🦆'}); + queue.add(async () => { + await delay(400); + result.push('🦆'); + }, {id: '🦆'}); queue.add(async () => { await delay(400); result.push('🐢'); }, {id: '🐢'}); + queue.add(async () => { + await delay(400); + result.push('🦆'); + }, {id: '🦆'}); + queue.add(async () => { + await delay(400); + result.push('🦆'); + }, {id: '🦆'}); + queue.setPriority('🐢', -1); + await queue.onIdle(); + t.deepEqual(result, ['🐌', '🦆', '🦆', '🦆', '🦆', '🐢']); +}); + +test('.setPriority() - execute a promise before planned - concurrency 2', async t => { + const result: string[] = []; + const queue = new PQueue({concurrency: 2}); + queue.add(async () => { + await delay(400); + result.push('🐌'); + }, {id: '🐌'}); + queue.add(async () => { + await delay(400); + result.push('🦆'); + }, {id: '🦆'}); + queue.add(async () => { + await delay(400); + result.push('🐢'); + }, {id: '🐢'}); + queue.add(async () => { + await delay(400); + result.push('⚡️'); + }, {id: '⚡️'}); queue.setPriority('⚡️', 1); await queue.onIdle(); - t.deepEqual(result, ['🐌', '🐢', '🦆']); + t.deepEqual(result, ['🐌', '🦆', '⚡️', '🐢']); }); + +test('.setPriority() - execute a promise before planned - concurrency 3', async t => { + const result: string[] = []; + const queue = new PQueue({concurrency: 3}); + queue.add(async () => { + await delay(400); + result.push('🐌'); + }, {id: '🐌'}); + queue.add(async () => { + await delay(400); + result.push('🦆'); + }, {id: '🦆'}); + queue.add(async () => { + await delay(400); + result.push('🐢'); + }, {id: '🐢'}); + queue.add(async () => { + await delay(400); + result.push('⚡️'); + }, {id: '⚡️'}); + queue.add(async () => { + await delay(400); + result.push('🦀'); + }, {id: '🦀'}); + queue.setPriority('🦀', 1); + await queue.onIdle(); + t.deepEqual(result, ['🐌', '🦆', '🐢', '🦀', '⚡️']); +}); + +test('.setPriority() - execute a multiple promise before planned, with variable priority', async t => { + const result: string[] = []; + const queue = new PQueue({concurrency: 2}); + queue.add(async () => { + await delay(400); + result.push('🐌'); + }, {id: '🐌'}); + queue.add(async () => { + await delay(400); + result.push('🦆'); + }, {id: '🦆'}); + queue.add(async () => { + await delay(400); + result.push('🐢'); + }, {id: '🐢'}); + queue.add(async () => { + await delay(400); + result.push('⚡️'); + }, {id: '⚡️'}); + queue.add(async () => { + await delay(400); + result.push('🦀'); + }, {id: '🦀'}); + queue.setPriority('⚡️', 1); + queue.setPriority('🦀', 2); + await queue.onIdle(); + t.deepEqual(result, ['🐌', '🦆', '🦀', '⚡️', '🐢']); +}); + +test('.setPriority() - execute a promise before planned - concurrency 3 and unspecified `id`', async t => { + const result: string[] = []; + const queue = new PQueue({concurrency: 3}); + queue.add(async () => { + await delay(400); + result.push('🐌'); + }); + queue.add(async () => { + await delay(400); + result.push('🦆'); + }); + queue.add(async () => { + await delay(400); + result.push('🐢'); + }); + queue.add(async () => { + await delay(400); + result.push('⚡️'); + }); + queue.add(async () => { + await delay(400); + result.push('🦀'); + }); + queue.setPriority('5', 1); + await queue.onIdle(); + t.deepEqual(result, ['🐌', '🦆', '🐢', '🦀', '⚡️']); +}); + From f7aa85a8480685b07de847426bfddbb0faffcbeb Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 21 Nov 2024 03:55:28 +0700 Subject: [PATCH 10/19] Update options.ts --- source/options.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/source/options.ts b/source/options.ts index 7bc4c64..364ee0b 100644 --- a/source/options.ts +++ b/source/options.ts @@ -69,6 +69,7 @@ export type QueueAddOptions = { @default 0 */ readonly priority?: number; + /** Unique identifier for the promise function. This can be used to update priority, before it gets executed. From ff4a1770f153b2a5f2016af45c5d2d06360f0f9d Mon Sep 17 00:00:00 2001 From: Raishav Hanspal Date: Thu, 21 Nov 2024 08:20:20 +0530 Subject: [PATCH 11/19] update readme, update comments --- readme.md | 18 +++++++++++------- source/index.ts | 4 +++- source/options.ts | 6 +----- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/readme.md b/readme.md index dcc303a..a6c3626 100644 --- a/readme.md +++ b/readme.md @@ -137,6 +137,12 @@ Default: `0` Priority of operation. Operations with greater priority will be scheduled first. +##### id + +Type `string` + +Unique identifier for the promise function, used to update its priority before execution. If not specified, it is auto-assigned as an incrementing bigint starting from 1n. + ##### signal [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for cancellation of the operation. When aborted, it will be removed from the queue and the `queue.add()` call will reject with an [error](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/reason). If the operation is already running, the signal will need to be handled by the operation itself. @@ -238,11 +244,9 @@ console.log(queue.sizeBy({priority: 0})); #### .setPriority(id, priority) -Update priority of a known promise function, using the `id` identifier, and a priority value to override existing priority value. The updated value of priority ensures whether to execute this promise function sooner or later. - -Function works only when we specify a defined concurrency to change any priorities. +Updates the priority of a promise function by its id, affecting its execution order. Requires a defined concurrency limit to take effect. -For example, this can be used to make a promise function run earlier. +For example, this can be used to prioritize a promise function to run earlier. ```js import PQueue from 'p-queue'; @@ -256,9 +260,9 @@ queue.add(async () => '🦄', {priority: 1}); queue.setPriority('🦀', 2); ``` -In above case, promise function with '🦀' executes second. +In this case, the promise function with id: '🦀' runs second. -We can also delay a promise function. +You can also deprioritize a promise function to delay its execution: ```js import PQueue from 'p-queue'; @@ -272,7 +276,7 @@ queue.add(async () => '🦄', {priority: 0}); queue.setPriority('🦀', -1); ``` -In above case, promise function with '🦀' executes last. +Here, the promise function with id: '🦀' executes last. #### .pending diff --git a/source/index.ts b/source/index.ts index 65331de..f34899b 100644 --- a/source/index.ts +++ b/source/index.ts @@ -232,7 +232,9 @@ export default class PQueue Date: Thu, 21 Nov 2024 08:25:04 +0530 Subject: [PATCH 12/19] update spacing --- source/options.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/options.ts b/source/options.ts index 1886231..ed4e0d1 100644 --- a/source/options.ts +++ b/source/options.ts @@ -71,7 +71,7 @@ export type QueueAddOptions = { readonly priority?: number; /** - Unique identifier for the promise function, used to update its priority before execution. If not specified, it is auto-assigned as an incrementing bigint starting from 1n. + Unique identifier for the promise function, used to update its priority before execution. If not specified, it is auto-assigned as an incrementing bigint starting from 1n. */ id?: string; } & TaskOptions & TimeoutOptions; From a62603c524a60e18b26b55d48151b1514927b15b Mon Sep 17 00:00:00 2001 From: Raishav Hanspal Date: Wed, 27 Nov 2024 18:10:35 +0530 Subject: [PATCH 13/19] match doc comment for .setPriority with readme --- source/index.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/source/index.ts b/source/index.ts index f34899b..9e9f35a 100644 --- a/source/index.ts +++ b/source/index.ts @@ -235,6 +235,36 @@ export default class PQueue '🦄', {priority: 1}); + queue.add(async () => '🦀', {priority: 0, id: '🦀'}); + queue.add(async () => '🦄', {priority: 1}); + queue.add(async () => '🦄', {priority: 1}); + + queue.setPriority('🦀', 2); + ``` + In this case, the promise function with id: '🦀' runs second. + + You can also deprioritize a promise function to delay its execution: + + ```js + import PQueue from 'p-queue'; + + const queue = new PQueue({concurrency: 1}); + + queue.add(async () => '🦄', {priority: 1}); + queue.add(async () => '🦀', {priority: 1, id: '🦀'}); + queue.add(async () => '🦄'); + queue.add(async () => '🦄', {priority: 0}); + + queue.setPriority('🦀', -1); + ``` + Here, the promise function with id: '🦀' executes last. */ setPriority(id: string, priority: number) { this.#queue.setPriority(id, priority); From 46965f7cac89089be2a84d2fc2ea9562f420377c Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 3 Dec 2024 23:58:27 +0100 Subject: [PATCH 14/19] Update readme.md --- readme.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index a6c3626..e47e6be 100644 --- a/readme.md +++ b/readme.md @@ -260,7 +260,8 @@ queue.add(async () => '🦄', {priority: 1}); queue.setPriority('🦀', 2); ``` -In this case, the promise function with id: '🦀' runs second. + +In this case, the promise function with `id: '🦀'` runs second. You can also deprioritize a promise function to delay its execution: @@ -276,7 +277,8 @@ queue.add(async () => '🦄', {priority: 0}); queue.setPriority('🦀', -1); ``` -Here, the promise function with id: '🦀' executes last. + +Here, the promise function with `id: '🦀'` executes last. #### .pending From ee1ab21534902fd2c53c65608f07353594c871e6 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 3 Dec 2024 23:59:15 +0100 Subject: [PATCH 15/19] Update index.ts --- source/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/index.ts b/source/index.ts index 9e9f35a..7967018 100644 --- a/source/index.ts +++ b/source/index.ts @@ -248,7 +248,8 @@ export default class PQueue Date: Tue, 3 Dec 2024 23:59:58 +0100 Subject: [PATCH 16/19] Update test.ts --- test/test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test.ts b/test/test.ts index 3fc3c78..748ee29 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1295,4 +1295,3 @@ test('.setPriority() - execute a promise before planned - concurrency 3 and unsp await queue.onIdle(); t.deepEqual(result, ['🐌', '🦆', '🐢', '🦀', '⚡️']); }); - From 253d67fe72f1a3c91b9709b47f410a651928e797 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 4 Dec 2024 02:41:16 +0100 Subject: [PATCH 17/19] Update index.ts --- source/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/index.ts b/source/index.ts index 7967018..9a16cdb 100644 --- a/source/index.ts +++ b/source/index.ts @@ -248,7 +248,7 @@ export default class PQueue Date: Fri, 3 Jan 2025 21:38:00 +0530 Subject: [PATCH 18/19] implement review comments - update id to index, few message/comment updates --- readme.md | 16 +++++++------- source/index.ts | 18 +++++++-------- source/options.ts | 4 ++-- source/priority-queue.ts | 10 ++++----- source/queue.ts | 2 +- test/test.ts | 48 ++++++++++++++++++++-------------------- 6 files changed, 49 insertions(+), 49 deletions(-) diff --git a/readme.md b/readme.md index a6c3626..f8edaa9 100644 --- a/readme.md +++ b/readme.md @@ -137,11 +137,11 @@ Default: `0` Priority of operation. Operations with greater priority will be scheduled first. -##### id +##### index Type `string` -Unique identifier for the promise function, used to update its priority before execution. If not specified, it is auto-assigned as an incrementing bigint starting from 1n. +Unique identifier for the promise function, used to update its priority before execution. If not specified, it is auto-assigned an incrementing BigInt starting from `1n`. ##### signal @@ -242,9 +242,9 @@ console.log(queue.sizeBy({priority: 0})); //=> 1 ``` -#### .setPriority(id, priority) +#### .setPriority(index, priority) -Updates the priority of a promise function by its id, affecting its execution order. Requires a defined concurrency limit to take effect. +Updates the priority of a promise function by its index, affecting its execution order. Requires a defined concurrency limit to take effect. For example, this can be used to prioritize a promise function to run earlier. @@ -254,13 +254,13 @@ import PQueue from 'p-queue'; const queue = new PQueue({concurrency: 1}); queue.add(async () => '🦄', {priority: 1}); -queue.add(async () => '🦀', {priority: 0, id: '🦀'}); +queue.add(async () => '🦀', {priority: 0, index: '🦀'}); queue.add(async () => '🦄', {priority: 1}); queue.add(async () => '🦄', {priority: 1}); queue.setPriority('🦀', 2); ``` -In this case, the promise function with id: '🦀' runs second. +In this case, the promise function with index: '🦀' runs second. You can also deprioritize a promise function to delay its execution: @@ -270,13 +270,13 @@ import PQueue from 'p-queue'; const queue = new PQueue({concurrency: 1}); queue.add(async () => '🦄', {priority: 1}); -queue.add(async () => '🦀', {priority: 1, id: '🦀'}); +queue.add(async () => '🦀', {priority: 1, index: '🦀'}); queue.add(async () => '🦄'); queue.add(async () => '🦄', {priority: 0}); queue.setPriority('🦀', -1); ``` -Here, the promise function with id: '🦀' executes last. +Here, the promise function with index: '🦀' executes last. #### .pending diff --git a/source/index.ts b/source/index.ts index 9e9f35a..37e4169 100644 --- a/source/index.ts +++ b/source/index.ts @@ -232,7 +232,7 @@ export default class PQueue '🦄', {priority: 1}); - queue.add(async () => '🦀', {priority: 0, id: '🦀'}); + queue.add(async () => '🦀', {priority: 0, index: '🦀'}); queue.add(async () => '🦄', {priority: 1}); queue.add(async () => '🦄', {priority: 1}); queue.setPriority('🦀', 2); ``` - In this case, the promise function with id: '🦀' runs second. + In this case, the promise function with index: '🦀' runs second. You can also deprioritize a promise function to delay its execution: @@ -258,16 +258,16 @@ export default class PQueue '🦄', {priority: 1}); - queue.add(async () => '🦀', {priority: 1, id: '🦀'}); + queue.add(async () => '🦀', {priority: 1, index: '🦀'}); queue.add(async () => '🦄'); queue.add(async () => '🦄', {priority: 0}); queue.setPriority('🦀', -1); ``` - Here, the promise function with id: '🦀' executes last. + Here, the promise function with index: '🦀' executes last. */ - setPriority(id: string, priority: number) { - this.#queue.setPriority(id, priority); + setPriority(index: string, priority: number) { + this.#queue.setPriority(index, priority); } /** @@ -276,8 +276,8 @@ export default class PQueue(function_: Task, options: {throwOnTimeout: true} & Exclude): Promise; async add(function_: Task, options?: Partial): Promise; async add(function_: Task, options: Partial = {}): Promise { - // In case `id` is not defined. - options.id ??= (this.#idAssigner++).toString(); + // In case `index` is not defined. + options.index ??= (this.#idAssigner++).toString(); options = { timeout: this.timeout, diff --git a/source/options.ts b/source/options.ts index ed4e0d1..2c304e9 100644 --- a/source/options.ts +++ b/source/options.ts @@ -71,9 +71,9 @@ export type QueueAddOptions = { readonly priority?: number; /** - Unique identifier for the promise function, used to update its priority before execution. If not specified, it is auto-assigned as an incrementing bigint starting from 1n. + Unique identifier for the promise function, used to update its priority before execution. If not specified, it is auto-assigned an incrementing BigInt starting from `1n`. */ - id?: string; + index?: string; } & TaskOptions & TimeoutOptions; export type TaskOptions = { diff --git a/source/priority-queue.ts b/source/priority-queue.ts index 90148e6..db8d529 100644 --- a/source/priority-queue.ts +++ b/source/priority-queue.ts @@ -17,7 +17,7 @@ export default class PriorityQueue implements Queue) => element.id === id); + setPriority(index: string, priority: number) { + const existingIndex: number = this.#queue.findIndex((element: Readonly) => element.index === index); if (existingIndex === -1) { - throw new Error('Invalid Index - No promise function of specified id available in the queue.'); + throw new ReferenceError(`No promise function with the index "${index}" exists in the queue.`); } const [item] = this.#queue.splice(existingIndex, 1); - this.enqueue(item!.run, {priority, id}); + this.enqueue(item!.run, {priority, index}); } dequeue(): RunFunction | undefined { diff --git a/source/queue.ts b/source/queue.ts index 459d29b..74bae9d 100644 --- a/source/queue.ts +++ b/source/queue.ts @@ -5,5 +5,5 @@ export type Queue = { filter: (options: Readonly>) => Element[]; dequeue: () => Element | undefined; enqueue: (run: Element, options?: Partial) => void; - setPriority: (id: string, priority: number) => void; + setPriority: (index: string, priority: number) => void; }; diff --git a/test/test.ts b/test/test.ts index 3fc3c78..4cb3fa5 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1141,15 +1141,15 @@ test('.setPriority() - execute a promise before planned', async t => { queue.add(async () => { await delay(400); result.push('🐌'); - }, {id: '🐌'}); + }, {index: '🐌'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {id: '🦆'}); + }, {index: '🦆'}); queue.add(async () => { await delay(400); result.push('🐢'); - }, {id: '🐢'}); + }, {index: '🐢'}); queue.setPriority('🐢', 1); await queue.onIdle(); t.deepEqual(result, ['🐌', '🐢', '🦆']); @@ -1161,27 +1161,27 @@ test('.setPriority() - execute a promise after planned', async t => { queue.add(async () => { await delay(400); result.push('🐌'); - }, {id: '🐌'}); + }, {index: '🐌'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {id: '🦆'}); + }, {index: '🦆'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {id: '🦆'}); + }, {index: '🦆'}); queue.add(async () => { await delay(400); result.push('🐢'); - }, {id: '🐢'}); + }, {index: '🐢'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {id: '🦆'}); + }, {index: '🦆'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {id: '🦆'}); + }, {index: '🦆'}); queue.setPriority('🐢', -1); await queue.onIdle(); t.deepEqual(result, ['🐌', '🦆', '🦆', '🦆', '🦆', '🐢']); @@ -1193,19 +1193,19 @@ test('.setPriority() - execute a promise before planned - concurrency 2', async queue.add(async () => { await delay(400); result.push('🐌'); - }, {id: '🐌'}); + }, {index: '🐌'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {id: '🦆'}); + }, {index: '🦆'}); queue.add(async () => { await delay(400); result.push('🐢'); - }, {id: '🐢'}); + }, {index: '🐢'}); queue.add(async () => { await delay(400); result.push('⚡️'); - }, {id: '⚡️'}); + }, {index: '⚡️'}); queue.setPriority('⚡️', 1); await queue.onIdle(); t.deepEqual(result, ['🐌', '🦆', '⚡️', '🐢']); @@ -1217,23 +1217,23 @@ test('.setPriority() - execute a promise before planned - concurrency 3', async queue.add(async () => { await delay(400); result.push('🐌'); - }, {id: '🐌'}); + }, {index: '🐌'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {id: '🦆'}); + }, {index: '🦆'}); queue.add(async () => { await delay(400); result.push('🐢'); - }, {id: '🐢'}); + }, {index: '🐢'}); queue.add(async () => { await delay(400); result.push('⚡️'); - }, {id: '⚡️'}); + }, {index: '⚡️'}); queue.add(async () => { await delay(400); result.push('🦀'); - }, {id: '🦀'}); + }, {index: '🦀'}); queue.setPriority('🦀', 1); await queue.onIdle(); t.deepEqual(result, ['🐌', '🦆', '🐢', '🦀', '⚡️']); @@ -1245,30 +1245,30 @@ test('.setPriority() - execute a multiple promise before planned, with variable queue.add(async () => { await delay(400); result.push('🐌'); - }, {id: '🐌'}); + }, {index: '🐌'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {id: '🦆'}); + }, {index: '🦆'}); queue.add(async () => { await delay(400); result.push('🐢'); - }, {id: '🐢'}); + }, {index: '🐢'}); queue.add(async () => { await delay(400); result.push('⚡️'); - }, {id: '⚡️'}); + }, {index: '⚡️'}); queue.add(async () => { await delay(400); result.push('🦀'); - }, {id: '🦀'}); + }, {index: '🦀'}); queue.setPriority('⚡️', 1); queue.setPriority('🦀', 2); await queue.onIdle(); t.deepEqual(result, ['🐌', '🦆', '🦀', '⚡️', '🐢']); }); -test('.setPriority() - execute a promise before planned - concurrency 3 and unspecified `id`', async t => { +test('.setPriority() - execute a promise before planned - concurrency 3 and unspecified `index`', async t => { const result: string[] = []; const queue = new PQueue({concurrency: 3}); queue.add(async () => { From 21a7501e14fc3be2f93fed7137c8c045053c43b0 Mon Sep 17 00:00:00 2001 From: Raishav Hanspal Date: Fri, 3 Jan 2025 23:47:09 +0530 Subject: [PATCH 19/19] revert back index to id --- readme.md | 16 ++++++++------ source/index.ts | 19 ++++++++-------- source/options.ts | 2 +- source/priority-queue.ts | 14 ++++++------ source/queue.ts | 2 +- test/test.ts | 48 ++++++++++++++++++++-------------------- 6 files changed, 52 insertions(+), 49 deletions(-) diff --git a/readme.md b/readme.md index f8edaa9..154a099 100644 --- a/readme.md +++ b/readme.md @@ -137,7 +137,7 @@ Default: `0` Priority of operation. Operations with greater priority will be scheduled first. -##### index +##### id Type `string` @@ -242,9 +242,9 @@ console.log(queue.sizeBy({priority: 0})); //=> 1 ``` -#### .setPriority(index, priority) +#### .setPriority(id, priority) -Updates the priority of a promise function by its index, affecting its execution order. Requires a defined concurrency limit to take effect. +Updates the priority of a promise function by its id, affecting its execution order. Requires a defined concurrency limit to take effect. For example, this can be used to prioritize a promise function to run earlier. @@ -254,13 +254,14 @@ import PQueue from 'p-queue'; const queue = new PQueue({concurrency: 1}); queue.add(async () => '🦄', {priority: 1}); -queue.add(async () => '🦀', {priority: 0, index: '🦀'}); +queue.add(async () => '🦀', {priority: 0, id: '🦀'}); queue.add(async () => '🦄', {priority: 1}); queue.add(async () => '🦄', {priority: 1}); queue.setPriority('🦀', 2); ``` -In this case, the promise function with index: '🦀' runs second. + +In this case, the promise function with `id: '🦀'` runs second. You can also deprioritize a promise function to delay its execution: @@ -270,13 +271,14 @@ import PQueue from 'p-queue'; const queue = new PQueue({concurrency: 1}); queue.add(async () => '🦄', {priority: 1}); -queue.add(async () => '🦀', {priority: 1, index: '🦀'}); +queue.add(async () => '🦀', {priority: 1, id: '🦀'}); queue.add(async () => '🦄'); queue.add(async () => '🦄', {priority: 0}); queue.setPriority('🦀', -1); ``` -Here, the promise function with index: '🦀' executes last. + +Here, the promise function with `id: '🦀'` executes last. #### .pending diff --git a/source/index.ts b/source/index.ts index 37e4169..9a16cdb 100644 --- a/source/index.ts +++ b/source/index.ts @@ -232,7 +232,7 @@ export default class PQueue '🦄', {priority: 1}); - queue.add(async () => '🦀', {priority: 0, index: '🦀'}); + queue.add(async () => '🦀', {priority: 0, id: '🦀'}); queue.add(async () => '🦄', {priority: 1}); queue.add(async () => '🦄', {priority: 1}); queue.setPriority('🦀', 2); ``` - In this case, the promise function with index: '🦀' runs second. + + In this case, the promise function with `id: '🦀'` runs second. You can also deprioritize a promise function to delay its execution: @@ -258,16 +259,16 @@ export default class PQueue '🦄', {priority: 1}); - queue.add(async () => '🦀', {priority: 1, index: '🦀'}); + queue.add(async () => '🦀', {priority: 1, id: '🦀'}); queue.add(async () => '🦄'); queue.add(async () => '🦄', {priority: 0}); queue.setPriority('🦀', -1); ``` - Here, the promise function with index: '🦀' executes last. + Here, the promise function with `id: '🦀'` executes last. */ - setPriority(index: string, priority: number) { - this.#queue.setPriority(index, priority); + setPriority(id: string, priority: number) { + this.#queue.setPriority(id, priority); } /** @@ -276,8 +277,8 @@ export default class PQueue(function_: Task, options: {throwOnTimeout: true} & Exclude): Promise; async add(function_: Task, options?: Partial): Promise; async add(function_: Task, options: Partial = {}): Promise { - // In case `index` is not defined. - options.index ??= (this.#idAssigner++).toString(); + // In case `id` is not defined. + options.id ??= (this.#idAssigner++).toString(); options = { timeout: this.timeout, diff --git a/source/options.ts b/source/options.ts index 2c304e9..c1ae5a9 100644 --- a/source/options.ts +++ b/source/options.ts @@ -73,7 +73,7 @@ export type QueueAddOptions = { /** Unique identifier for the promise function, used to update its priority before execution. If not specified, it is auto-assigned an incrementing BigInt starting from `1n`. */ - index?: string; + id?: string; } & TaskOptions & TimeoutOptions; export type TaskOptions = { diff --git a/source/priority-queue.ts b/source/priority-queue.ts index db8d529..944514a 100644 --- a/source/priority-queue.ts +++ b/source/priority-queue.ts @@ -17,7 +17,7 @@ export default class PriorityQueue implements Queue) => element.index === index); - if (existingIndex === -1) { - throw new ReferenceError(`No promise function with the index "${index}" exists in the queue.`); + setPriority(id: string, priority: number) { + const index: number = this.#queue.findIndex((element: Readonly) => element.id === id); + if (index === -1) { + throw new ReferenceError(`No promise function with the id "${id}" exists in the queue.`); } - const [item] = this.#queue.splice(existingIndex, 1); - this.enqueue(item!.run, {priority, index}); + const [item] = this.#queue.splice(index, 1); + this.enqueue(item!.run, {priority, id}); } dequeue(): RunFunction | undefined { diff --git a/source/queue.ts b/source/queue.ts index 74bae9d..459d29b 100644 --- a/source/queue.ts +++ b/source/queue.ts @@ -5,5 +5,5 @@ export type Queue = { filter: (options: Readonly>) => Element[]; dequeue: () => Element | undefined; enqueue: (run: Element, options?: Partial) => void; - setPriority: (index: string, priority: number) => void; + setPriority: (id: string, priority: number) => void; }; diff --git a/test/test.ts b/test/test.ts index 44a1ae6..748ee29 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1141,15 +1141,15 @@ test('.setPriority() - execute a promise before planned', async t => { queue.add(async () => { await delay(400); result.push('🐌'); - }, {index: '🐌'}); + }, {id: '🐌'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {index: '🦆'}); + }, {id: '🦆'}); queue.add(async () => { await delay(400); result.push('🐢'); - }, {index: '🐢'}); + }, {id: '🐢'}); queue.setPriority('🐢', 1); await queue.onIdle(); t.deepEqual(result, ['🐌', '🐢', '🦆']); @@ -1161,27 +1161,27 @@ test('.setPriority() - execute a promise after planned', async t => { queue.add(async () => { await delay(400); result.push('🐌'); - }, {index: '🐌'}); + }, {id: '🐌'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {index: '🦆'}); + }, {id: '🦆'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {index: '🦆'}); + }, {id: '🦆'}); queue.add(async () => { await delay(400); result.push('🐢'); - }, {index: '🐢'}); + }, {id: '🐢'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {index: '🦆'}); + }, {id: '🦆'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {index: '🦆'}); + }, {id: '🦆'}); queue.setPriority('🐢', -1); await queue.onIdle(); t.deepEqual(result, ['🐌', '🦆', '🦆', '🦆', '🦆', '🐢']); @@ -1193,19 +1193,19 @@ test('.setPriority() - execute a promise before planned - concurrency 2', async queue.add(async () => { await delay(400); result.push('🐌'); - }, {index: '🐌'}); + }, {id: '🐌'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {index: '🦆'}); + }, {id: '🦆'}); queue.add(async () => { await delay(400); result.push('🐢'); - }, {index: '🐢'}); + }, {id: '🐢'}); queue.add(async () => { await delay(400); result.push('⚡️'); - }, {index: '⚡️'}); + }, {id: '⚡️'}); queue.setPriority('⚡️', 1); await queue.onIdle(); t.deepEqual(result, ['🐌', '🦆', '⚡️', '🐢']); @@ -1217,23 +1217,23 @@ test('.setPriority() - execute a promise before planned - concurrency 3', async queue.add(async () => { await delay(400); result.push('🐌'); - }, {index: '🐌'}); + }, {id: '🐌'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {index: '🦆'}); + }, {id: '🦆'}); queue.add(async () => { await delay(400); result.push('🐢'); - }, {index: '🐢'}); + }, {id: '🐢'}); queue.add(async () => { await delay(400); result.push('⚡️'); - }, {index: '⚡️'}); + }, {id: '⚡️'}); queue.add(async () => { await delay(400); result.push('🦀'); - }, {index: '🦀'}); + }, {id: '🦀'}); queue.setPriority('🦀', 1); await queue.onIdle(); t.deepEqual(result, ['🐌', '🦆', '🐢', '🦀', '⚡️']); @@ -1245,30 +1245,30 @@ test('.setPriority() - execute a multiple promise before planned, with variable queue.add(async () => { await delay(400); result.push('🐌'); - }, {index: '🐌'}); + }, {id: '🐌'}); queue.add(async () => { await delay(400); result.push('🦆'); - }, {index: '🦆'}); + }, {id: '🦆'}); queue.add(async () => { await delay(400); result.push('🐢'); - }, {index: '🐢'}); + }, {id: '🐢'}); queue.add(async () => { await delay(400); result.push('⚡️'); - }, {index: '⚡️'}); + }, {id: '⚡️'}); queue.add(async () => { await delay(400); result.push('🦀'); - }, {index: '🦀'}); + }, {id: '🦀'}); queue.setPriority('⚡️', 1); queue.setPriority('🦀', 2); await queue.onIdle(); t.deepEqual(result, ['🐌', '🦆', '🦀', '⚡️', '🐢']); }); -test('.setPriority() - execute a promise before planned - concurrency 3 and unspecified `index`', async t => { +test('.setPriority() - execute a promise before planned - concurrency 3 and unspecified `id`', async t => { const result: string[] = []; const queue = new PQueue({concurrency: 3}); queue.add(async () => {