UNPKG

3.81 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7
8/**
9 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
10 *
11 * This source code is licensed under the MIT license found in the
12 * LICENSE file in the root directory of this source tree.
13 */
14
15/**
16 * First-in, First-out task queue that manages a dedicated pool
17 * for each worker as well as a shared queue. The FIFO ordering is guaranteed
18 * across the worker specific and shared queue.
19 */
20class 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 var _this$_workerQueues$w, _workerTop$previousSh, _workerTop$previousSh2;
48
49 const workerTop =
50 (_this$_workerQueues$w = this._workerQueues[workerId]) === null ||
51 _this$_workerQueues$w === void 0
52 ? void 0
53 : _this$_workerQueues$w.peek();
54 const sharedTaskIsProcessed =
55 (_workerTop$previousSh =
56 workerTop === null || workerTop === void 0
57 ? void 0
58 : (_workerTop$previousSh2 = workerTop.previousSharedTask) === null ||
59 _workerTop$previousSh2 === void 0
60 ? void 0
61 : _workerTop$previousSh2.request[1]) !== null &&
62 _workerTop$previousSh !== void 0
63 ? _workerTop$previousSh
64 : true; // Process the top task from the shared queue if
65 // - there's no task in the worker specific queue or
66 // - if the non-worker-specific task after which this worker specific task
67 // has been queued wasn't processed yet
68
69 if (workerTop != null && sharedTaskIsProcessed) {
70 var _this$_workerQueues$w2,
71 _this$_workerQueues$w3,
72 _this$_workerQueues$w4;
73
74 return (_this$_workerQueues$w2 =
75 (_this$_workerQueues$w3 = this._workerQueues[workerId]) === null ||
76 _this$_workerQueues$w3 === void 0
77 ? void 0
78 : (_this$_workerQueues$w4 = _this$_workerQueues$w3.dequeue()) ===
79 null || _this$_workerQueues$w4 === void 0
80 ? void 0
81 : _this$_workerQueues$w4.task) !== null &&
82 _this$_workerQueues$w2 !== void 0
83 ? _this$_workerQueues$w2
84 : null;
85 }
86
87 return this._sharedQueue.dequeue();
88 }
89}
90
91exports.default = FifoQueue;
92
93/**
94 * FIFO queue for a single worker / shared queue.
95 */
96class InternalQueue {
97 _head = null;
98 _last = null;
99
100 enqueue(value) {
101 const item = {
102 next: null,
103 value
104 };
105
106 if (this._last == null) {
107 this._head = item;
108 } else {
109 this._last.next = item;
110 }
111
112 this._last = item;
113 }
114
115 dequeue() {
116 if (this._head == null) {
117 return null;
118 }
119
120 const item = this._head;
121 this._head = item.next;
122
123 if (this._head == null) {
124 this._last = null;
125 }
126
127 return item.value;
128 }
129
130 peek() {
131 var _this$_head$value, _this$_head;
132
133 return (_this$_head$value =
134 (_this$_head = this._head) === null || _this$_head === void 0
135 ? void 0
136 : _this$_head.value) !== null && _this$_head$value !== void 0
137 ? _this$_head$value
138 : null;
139 }
140
141 peekLast() {
142 var _this$_last$value, _this$_last;
143
144 return (_this$_last$value =
145 (_this$_last = this._last) === null || _this$_last === void 0
146 ? void 0
147 : _this$_last.value) !== null && _this$_last$value !== void 0
148 ? _this$_last$value
149 : null;
150 }
151}