1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | class FifoQueue {
|
21 | _workerQueues = [];
|
22 | _sharedQueue = new InternalQueue();
|
23 |
|
24 | enqueue(task, workerId) {
|
25 | if (workerId == null) {
|
26 | this._sharedQueue.enqueue(task);
|
27 |
|
28 | return;
|
29 | }
|
30 |
|
31 | let workerQueue = this._workerQueues[workerId];
|
32 |
|
33 | if (workerQueue == null) {
|
34 | workerQueue = this._workerQueues[workerId] = new InternalQueue();
|
35 | }
|
36 |
|
37 | const sharedTop = this._sharedQueue.peekLast();
|
38 |
|
39 | const item = {
|
40 | previousSharedTask: sharedTop,
|
41 | task
|
42 | };
|
43 | workerQueue.enqueue(item);
|
44 | }
|
45 |
|
46 | dequeue(workerId) {
|
47 | const workerTop = this._workerQueues[workerId]?.peek();
|
48 | const sharedTaskIsProcessed =
|
49 | workerTop?.previousSharedTask?.request[1] ?? true;
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | if (workerTop != null && sharedTaskIsProcessed) {
|
55 | return this._workerQueues[workerId]?.dequeue()?.task ?? null;
|
56 | }
|
57 |
|
58 | return this._sharedQueue.dequeue();
|
59 | }
|
60 | }
|
61 |
|
62 | exports.default = FifoQueue;
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | class InternalQueue {
|
68 | _head = null;
|
69 | _last = null;
|
70 |
|
71 | enqueue(value) {
|
72 | const item = {
|
73 | next: null,
|
74 | value
|
75 | };
|
76 |
|
77 | if (this._last == null) {
|
78 | this._head = item;
|
79 | } else {
|
80 | this._last.next = item;
|
81 | }
|
82 |
|
83 | this._last = item;
|
84 | }
|
85 |
|
86 | dequeue() {
|
87 | if (this._head == null) {
|
88 | return null;
|
89 | }
|
90 |
|
91 | const item = this._head;
|
92 | this._head = item.next;
|
93 |
|
94 | if (this._head == null) {
|
95 | this._last = null;
|
96 | }
|
97 |
|
98 | return item.value;
|
99 | }
|
100 |
|
101 | peek() {
|
102 | return this._head?.value ?? null;
|
103 | }
|
104 |
|
105 | peekLast() {
|
106 | return this._last?.value ?? null;
|
107 | }
|
108 | }
|