Queue.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. "use strict";
  2. const DoublyLinkedList = require("./DoublyLinkedList");
  3. const Deque = require("./Deque");
  4. /**
  5. * Sort of a internal queue for holding the waiting
  6. * resource requets for a given "priority".
  7. * Also handles managing timeouts rejections on items (is this the best place for this?)
  8. * This is the last point where we know which queue a resourceRequest is in
  9. *
  10. */
  11. class Queue extends Deque {
  12. /**
  13. * Adds the obj to the end of the list for this slot
  14. * we completely override the parent method because we need access to the
  15. * node for our rejection handler
  16. * @param {any} resourceRequest [description]
  17. */
  18. push(resourceRequest) {
  19. const node = DoublyLinkedList.createNode(resourceRequest);
  20. resourceRequest.promise.catch(this._createTimeoutRejectionHandler(node));
  21. this._list.insertEnd(node);
  22. }
  23. _createTimeoutRejectionHandler(node) {
  24. return reason => {
  25. if (reason.name === "TimeoutError") {
  26. this._list.remove(node);
  27. }
  28. };
  29. }
  30. }
  31. module.exports = Queue;