-
-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/update priority #209
base: main
Are you sure you want to change the base?
Feature/update priority #209
Conversation
requested changes have been pushed! @Richienb |
I think we should allow searching by the object returned by enqueue as well. What do you think? Does there remain a use case where a string id remains more useful? |
@Richienb that would be a very nice idea, and will not require |
@Richienb, @sindresorhus , Please suggest further. |
t.deepEqual(result, ['🐌', '🐢', '🦆']); | ||
}); | ||
|
||
test.failing('.setPriority() - with invalid "id"', async t => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test.failing
is not for tests that are intentionally failing.
const [item] = this.#queue.splice(existingIndex, 1); | ||
if (item === undefined) { | ||
throw new Error('Undefined Item - No promise function of specified id available in the queue.'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think these should throw. It should be allowed to set a priority without knowing beforehand whether the has already finished.
@@ -69,6 +69,7 @@ export type QueueAddOptions = { | |||
@default 0 | |||
*/ | |||
readonly priority?: number; | |||
id?: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doc comment.
/** | ||
Adds a sync or async task to the queue. Always returns a promise. | ||
*/ | ||
async add<TaskResultType>(function_: Task<TaskResultType>, options: {throwOnTimeout: true} & Exclude<EnqueueOptionsType, 'throwOnTimeout'>): Promise<TaskResultType>; | ||
async add<TaskResultType>(function_: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType | void>; | ||
async add<TaskResultType>(function_: Task<TaskResultType>, options: Partial<EnqueueOptionsType> = {}): Promise<TaskResultType | void> { | ||
// Incase id is not defined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Incase id is not defined | |
// In case `id` is not defined. |
@@ -228,12 +231,21 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT | |||
}); | |||
} | |||
|
|||
setPriority(id: string, priority: number) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doc comment
@@ -43,6 +43,9 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT | |||
|
|||
readonly #throwOnTimeout: boolean; | |||
|
|||
/** Use to assign a unique identifier to a promise function, if not explicitly specified */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** Use to assign a unique identifier to a promise function, if not explicitly specified */ | |
// Used to assign a unique identifier to a promise function, if not explicitly specified. |
@@ -43,6 +43,9 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT | |||
|
|||
readonly #throwOnTimeout: boolean; | |||
|
|||
/** Use to assign a unique identifier to a promise function, if not explicitly specified */ | |||
#idAssigner = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be a BigInt, to ensure it will never overflow.
if (options.id === undefined) { | ||
options.id = (this.#idAssigner++).toString(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (options.id === undefined) { | |
options.id = (this.#idAssigner++).toString(); | |
} | |
options.id ??= (this.#idAssigner++).toString(); |
@@ -69,6 +69,7 @@ export type QueueAddOptions = { | |||
@default 0 | |||
*/ | |||
readonly priority?: number; | |||
id?: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id?: string; | |
readonly id?: string; |
Needs to be added to the readme. I would like to see some more tests. And the pull request needs a proper title. |
Fixes #208 - add a
uid
to track the promise functions and update the priority on any promise.also add a new event
started
to notify when a promise function is executed.