queue.js 836 B

1234567891011121314151617181920212223242526272829
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Queue = void 0;
  4. /**
  5. * This is a custom stateless queue to track concurrent async fs calls.
  6. * It increments a counter whenever a call is queued and decrements it
  7. * as soon as it completes. When the counter hits 0, it calls onQueueEmpty.
  8. */
  9. class Queue {
  10. onQueueEmpty;
  11. count = 0;
  12. constructor(onQueueEmpty) {
  13. this.onQueueEmpty = onQueueEmpty;
  14. }
  15. enqueue() {
  16. this.count++;
  17. return this.count;
  18. }
  19. dequeue(error, output) {
  20. if (this.onQueueEmpty && (--this.count <= 0 || error)) {
  21. this.onQueueEmpty(error, output);
  22. if (error) {
  23. output.controller.abort();
  24. this.onQueueEmpty = undefined;
  25. }
  26. }
  27. }
  28. }
  29. exports.Queue = Queue;