priorityQueue.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = function (worker, concurrency) {
  6. // Start with a normal queue
  7. var q = (0, _queue2.default)(worker, concurrency);
  8. var {
  9. push,
  10. pushAsync
  11. } = q;
  12. q._tasks = new _Heap2.default();
  13. q._createTaskItem = ({ data, priority }, callback) => {
  14. return {
  15. data,
  16. priority,
  17. callback
  18. };
  19. };
  20. function createDataItems(tasks, priority) {
  21. if (!Array.isArray(tasks)) {
  22. return { data: tasks, priority };
  23. }
  24. return tasks.map(data => {
  25. return { data, priority };
  26. });
  27. }
  28. // Override push to accept second parameter representing priority
  29. q.push = function (data, priority = 0, callback) {
  30. return push(createDataItems(data, priority), callback);
  31. };
  32. q.pushAsync = function (data, priority = 0, callback) {
  33. return pushAsync(createDataItems(data, priority), callback);
  34. };
  35. // Remove unshift functions
  36. delete q.unshift;
  37. delete q.unshiftAsync;
  38. return q;
  39. };
  40. var _queue = require('./queue.js');
  41. var _queue2 = _interopRequireDefault(_queue);
  42. var _Heap = require('./internal/Heap.js');
  43. var _Heap2 = _interopRequireDefault(_Heap);
  44. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  45. module.exports = exports.default;