1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, '__esModule', {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | function _defineProperty(obj, key, value) {
|
---|
9 | if (key in obj) {
|
---|
10 | Object.defineProperty(obj, key, {
|
---|
11 | value: value,
|
---|
12 | enumerable: true,
|
---|
13 | configurable: true,
|
---|
14 | writable: true
|
---|
15 | });
|
---|
16 | } else {
|
---|
17 | obj[key] = value;
|
---|
18 | }
|
---|
19 | return obj;
|
---|
20 | }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
---|
24 | *
|
---|
25 | * This source code is licensed under the MIT license found in the
|
---|
26 | * LICENSE file in the root directory of this source tree.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * First-in, First-out task queue that manages a dedicated pool
|
---|
31 | * for each worker as well as a shared queue. The FIFO ordering is guaranteed
|
---|
32 | * across the worker specific and shared queue.
|
---|
33 | */
|
---|
34 | class FifoQueue {
|
---|
35 | constructor() {
|
---|
36 | _defineProperty(this, '_workerQueues', []);
|
---|
37 |
|
---|
38 | _defineProperty(this, '_sharedQueue', new InternalQueue());
|
---|
39 | }
|
---|
40 |
|
---|
41 | enqueue(task, workerId) {
|
---|
42 | if (workerId == null) {
|
---|
43 | this._sharedQueue.enqueue(task);
|
---|
44 |
|
---|
45 | return;
|
---|
46 | }
|
---|
47 |
|
---|
48 | let workerQueue = this._workerQueues[workerId];
|
---|
49 |
|
---|
50 | if (workerQueue == null) {
|
---|
51 | workerQueue = this._workerQueues[workerId] = new InternalQueue();
|
---|
52 | }
|
---|
53 |
|
---|
54 | const sharedTop = this._sharedQueue.peekLast();
|
---|
55 |
|
---|
56 | const item = {
|
---|
57 | previousSharedTask: sharedTop,
|
---|
58 | task
|
---|
59 | };
|
---|
60 | workerQueue.enqueue(item);
|
---|
61 | }
|
---|
62 |
|
---|
63 | dequeue(workerId) {
|
---|
64 | var _this$_workerQueues$w, _workerTop$previousSh, _workerTop$previousSh2;
|
---|
65 |
|
---|
66 | const workerTop =
|
---|
67 | (_this$_workerQueues$w = this._workerQueues[workerId]) === null ||
|
---|
68 | _this$_workerQueues$w === void 0
|
---|
69 | ? void 0
|
---|
70 | : _this$_workerQueues$w.peek();
|
---|
71 | const sharedTaskIsProcessed =
|
---|
72 | (_workerTop$previousSh =
|
---|
73 | workerTop === null || workerTop === void 0
|
---|
74 | ? void 0
|
---|
75 | : (_workerTop$previousSh2 = workerTop.previousSharedTask) === null ||
|
---|
76 | _workerTop$previousSh2 === void 0
|
---|
77 | ? void 0
|
---|
78 | : _workerTop$previousSh2.request[1]) !== null &&
|
---|
79 | _workerTop$previousSh !== void 0
|
---|
80 | ? _workerTop$previousSh
|
---|
81 | : true; // Process the top task from the shared queue if
|
---|
82 | // - there's no task in the worker specific queue or
|
---|
83 | // - if the non-worker-specific task after which this worker specifif task
|
---|
84 | // hasn been queued wasn't processed yet
|
---|
85 |
|
---|
86 | if (workerTop != null && sharedTaskIsProcessed) {
|
---|
87 | var _this$_workerQueues$w2,
|
---|
88 | _this$_workerQueues$w3,
|
---|
89 | _this$_workerQueues$w4;
|
---|
90 |
|
---|
91 | return (_this$_workerQueues$w2 =
|
---|
92 | (_this$_workerQueues$w3 = this._workerQueues[workerId]) === null ||
|
---|
93 | _this$_workerQueues$w3 === void 0
|
---|
94 | ? void 0
|
---|
95 | : (_this$_workerQueues$w4 = _this$_workerQueues$w3.dequeue()) ===
|
---|
96 | null || _this$_workerQueues$w4 === void 0
|
---|
97 | ? void 0
|
---|
98 | : _this$_workerQueues$w4.task) !== null &&
|
---|
99 | _this$_workerQueues$w2 !== void 0
|
---|
100 | ? _this$_workerQueues$w2
|
---|
101 | : null;
|
---|
102 | }
|
---|
103 |
|
---|
104 | return this._sharedQueue.dequeue();
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | exports.default = FifoQueue;
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * FIFO queue for a single worker / shared queue.
|
---|
112 | */
|
---|
113 | class InternalQueue {
|
---|
114 | constructor() {
|
---|
115 | _defineProperty(this, '_head', null);
|
---|
116 |
|
---|
117 | _defineProperty(this, '_last', null);
|
---|
118 | }
|
---|
119 |
|
---|
120 | enqueue(value) {
|
---|
121 | const item = {
|
---|
122 | next: null,
|
---|
123 | value
|
---|
124 | };
|
---|
125 |
|
---|
126 | if (this._last == null) {
|
---|
127 | this._head = item;
|
---|
128 | } else {
|
---|
129 | this._last.next = item;
|
---|
130 | }
|
---|
131 |
|
---|
132 | this._last = item;
|
---|
133 | }
|
---|
134 |
|
---|
135 | dequeue() {
|
---|
136 | if (this._head == null) {
|
---|
137 | return null;
|
---|
138 | }
|
---|
139 |
|
---|
140 | const item = this._head;
|
---|
141 | this._head = item.next;
|
---|
142 |
|
---|
143 | if (this._head == null) {
|
---|
144 | this._last = null;
|
---|
145 | }
|
---|
146 |
|
---|
147 | return item.value;
|
---|
148 | }
|
---|
149 |
|
---|
150 | peek() {
|
---|
151 | var _this$_head$value, _this$_head;
|
---|
152 |
|
---|
153 | return (_this$_head$value =
|
---|
154 | (_this$_head = this._head) === null || _this$_head === void 0
|
---|
155 | ? void 0
|
---|
156 | : _this$_head.value) !== null && _this$_head$value !== void 0
|
---|
157 | ? _this$_head$value
|
---|
158 | : null;
|
---|
159 | }
|
---|
160 |
|
---|
161 | peekLast() {
|
---|
162 | var _this$_last$value, _this$_last;
|
---|
163 |
|
---|
164 | return (_this$_last$value =
|
---|
165 | (_this$_last = this._last) === null || _this$_last === void 0
|
---|
166 | ? void 0
|
---|
167 | : _this$_last.value) !== null && _this$_last$value !== void 0
|
---|
168 | ? _this$_last$value
|
---|
169 | : null;
|
---|
170 | }
|
---|
171 | }
|
---|