MCPcopy
hub / github.com/sindresorhus/p-queue / enqueue

Method enqueue

source/priority-queue.ts:17–48  ·  view source on GitHub ↗
(run: RunFunction, options?: Partial<PriorityQueueOptions>)

Source from the content-addressed store, hash-verified

15 #head = 0;
16
17 enqueue(run: RunFunction, options?: Partial<PriorityQueueOptions>): void {
18 const {
19 priority = 0,
20 id,
21 } = options ?? {};
22
23 const {size} = this;
24 const element = {
25 priority,
26 id,
27 run,
28 };
29
30 if (size === 0) {
31 // When the queue is logically empty, discard any consumed prefix before accepting new work.
32 this.#queue.length = 0;
33 this.#head = 0;
34 this.#queue.push(element);
35 return;
36 }
37
38 if (this.#queue.at(-1)!.priority! >= priority) {
39 // Same-priority and lower-priority items belong after the current tail. Appending preserves FIFO order for equal priorities.
40 this.#queue.push(element);
41 return;
42 }
43
44 // Binary insertion must run on the live sorted range only.
45 this.#compact();
46 const index = lowerBound(this.#queue, element, (a: Readonly<PriorityQueueOptions>, b: Readonly<PriorityQueueOptions>) => b.priority! - a.priority!);
47 this.#queue.splice(index, 0, element);
48 }
49
50 setPriority(id: string, priority: number) {
51 // A dequeued item with the same id is no longer part of the queue.

Callers 4

setPriorityMethod · 0.95
bench.tsFile · 0.80
addMethod · 0.80
priority-queue.tsFile · 0.80

Calls 2

#compactMethod · 0.95
lowerBoundFunction · 0.85

Tested by

no test coverage detected