FifoQueue.js 4.1 KB

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