|
1 |
| -export class PQueue<T> implements AsyncIterable<T>, Disposable { |
2 |
| - private queue: { value: T; priority: number }[] = []; |
| 1 | +export class PQueue<T> { |
| 2 | + private heap: { value: T; priority: number }[] = []; |
3 | 3 | private closed = false;
|
4 |
| - private resolvers: ((value: T) => void)[] = []; |
| 4 | + private pendingShifts: ((value: IteratorResult<T>) => void)[] = []; |
| 5 | + private pendingRejects: ((reason?: any) => void)[] = []; |
5 | 6 |
|
6 |
| - push(value: T, priority: number = 0) { |
7 |
| - if (this.closed) throw new Error("Queue closed"); |
8 |
| - |
9 |
| - let item = { value, priority }; |
10 |
| - let inserted = false; |
| 7 | + get size(): number { |
| 8 | + return this.heap.length; |
| 9 | + } |
11 | 10 |
|
12 |
| - for (let i = 0; i < this.queue.length; i++) { |
13 |
| - if (this.queue[i].priority < priority) { |
14 |
| - this.queue.splice(i, 0, item); |
15 |
| - inserted = true; |
16 |
| - break; |
17 |
| - } |
| 11 | + push(value: T, priority: number = 0) { |
| 12 | + if (this.closed) { |
| 13 | + throw new Error('Queue closed'); |
18 | 14 | }
|
19 | 15 |
|
20 |
| - if (!inserted) { |
21 |
| - this.queue.push(item); |
| 16 | + let left = 0; |
| 17 | + let right = this.heap.length; |
| 18 | + while (left < right) { |
| 19 | + let mid = (left + right) >> 1; |
| 20 | + if (this.heap[mid].priority < priority) { |
| 21 | + right = mid; |
| 22 | + } else { |
| 23 | + left = mid + 1; |
| 24 | + } |
22 | 25 | }
|
| 26 | + this.heap.splice(left, 0, { value, priority }); |
23 | 27 |
|
24 |
| - if (this.resolvers.length) { |
25 |
| - const nextItem = this.queue.shift()!; |
26 |
| - this.resolvers.shift()?.(nextItem.value); |
| 28 | + if (this.pendingShifts.length > 0) { |
| 29 | + this.pendingRejects.shift()!; |
| 30 | + let next = this.heap.shift()!; |
| 31 | + let resolve = this.pendingShifts.shift()!; |
| 32 | + resolve({ value: next.value, done: false }); |
27 | 33 | }
|
28 | 34 | }
|
29 | 35 |
|
30 | 36 | async shift(): Promise<T> {
|
31 |
| - if (this.queue.length) { |
32 |
| - return this.queue.shift()!.value; |
| 37 | + if (this.closed && this.heap.length === 0) { |
| 38 | + throw new Error('Queue closed'); |
| 39 | + } |
| 40 | + |
| 41 | + if (this.heap.length > 0) { |
| 42 | + let next = this.heap.shift()!; |
| 43 | + return next.value; |
33 | 44 | }
|
34 |
| - if (this.closed) throw new Error("Queue closed"); |
35 |
| - return new Promise<T>((resolve) => { |
36 |
| - this.resolvers.push(resolve); |
| 45 | + |
| 46 | + return new Promise<T>((resolve, reject) => { |
| 47 | + this.pendingRejects.push(reject); |
| 48 | + this.pendingShifts.push(({ value, done }) => { |
| 49 | + if (done) { |
| 50 | + reject(new Error('Queue closed')); |
| 51 | + } else { |
| 52 | + resolve(value); |
| 53 | + } |
| 54 | + }); |
37 | 55 | });
|
38 | 56 | }
|
39 | 57 |
|
40 | 58 | close() {
|
41 | 59 | this.closed = true;
|
42 |
| - while (this.resolvers.length) { |
43 |
| - this.resolvers.shift()?.(undefined as any); |
| 60 | + while (this.pendingShifts.length > 0) { |
| 61 | + this.pendingRejects.shift()!; |
| 62 | + let resolve = this.pendingShifts.shift()!; |
| 63 | + resolve({ value: undefined as any, done: true }); |
44 | 64 | }
|
45 | 65 | }
|
46 | 66 |
|
47 |
| - async next(): Promise<IteratorResult<T>> { |
48 |
| - try { |
49 |
| - return { value: await this.shift(), done: false }; |
50 |
| - } catch { |
51 |
| - return { value: undefined, done: true }; |
| 67 | + restartConsumer() { |
| 68 | + while (this.pendingShifts.length > 0) { |
| 69 | + this.pendingShifts.shift()!; |
| 70 | + let reject = this.pendingRejects.shift()!; |
| 71 | + reject(new Error('Consumer restarted')); |
52 | 72 | }
|
53 | 73 | }
|
54 | 74 |
|
55 |
| - [Symbol.asyncIterator]() { |
56 |
| - return this; |
| 75 | + async next(): Promise<IteratorResult<T>> { |
| 76 | + if (this.closed && this.heap.length === 0) { |
| 77 | + return { value: undefined as any, done: true }; |
| 78 | + } |
| 79 | + |
| 80 | + if (this.heap.length > 0) { |
| 81 | + let next = this.heap.shift()!; |
| 82 | + return { value: next.value, done: false }; |
| 83 | + } |
| 84 | + |
| 85 | + return new Promise<IteratorResult<T>>((resolve, reject) => { |
| 86 | + this.pendingShifts.push(resolve); |
| 87 | + this.pendingRejects.push(reject); |
| 88 | + }); |
57 | 89 | }
|
58 | 90 |
|
59 |
| - [Symbol.dispose]() { |
60 |
| - this.close(); |
61 |
| - this.queue.length = 0; |
62 |
| - this.resolvers.length = 0; |
| 91 | + async *[Symbol.asyncIterator]() { |
| 92 | + while (true) { |
| 93 | + // eslint-disable-next-line no-await-in-loop |
| 94 | + let { value, done } = await this.next(); |
| 95 | + if (done) break; |
| 96 | + yield value; |
| 97 | + } |
63 | 98 | }
|
64 | 99 | }
|
0 commit comments