FifoQueue.js 3.7 KB

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