priorityQueue.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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'];
  46. /**
  47. * The same as [async.queue]{@link module:ControlFlow.queue} only tasks are assigned a priority and
  48. * completed in ascending priority order.
  49. *
  50. * @name priorityQueue
  51. * @static
  52. * @memberOf module:ControlFlow
  53. * @method
  54. * @see [async.queue]{@link module:ControlFlow.queue}
  55. * @category Control Flow
  56. * @param {AsyncFunction} worker - An async function for processing a queued task.
  57. * If you want to handle errors from an individual task, pass a callback to
  58. * `q.push()`.
  59. * Invoked with (task, callback).
  60. * @param {number} concurrency - An `integer` for determining how many `worker`
  61. * functions should be run in parallel. If omitted, the concurrency defaults to
  62. * `1`. If the concurrency is `0`, an error is thrown.
  63. * @returns {module:ControlFlow.QueueObject} A priorityQueue object to manage the tasks. There are three
  64. * differences between `queue` and `priorityQueue` objects:
  65. * * `push(task, priority, [callback])` - `priority` should be a number. If an
  66. * array of `tasks` is given, all tasks will be assigned the same priority.
  67. * * `pushAsync(task, priority, [callback])` - the same as `priorityQueue.push`,
  68. * except this returns a promise that rejects if an error occurs.
  69. * * The `unshift` and `unshiftAsync` methods were removed.
  70. */