priorityQueue.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 processingScheduled = false;
  9. q._tasks = new _Heap2.default();
  10. // Override push to accept second parameter representing priority
  11. q.push = function (data, priority = 0, callback = () => {}) {
  12. if (typeof callback !== 'function') {
  13. throw new Error('task callback must be a function');
  14. }
  15. q.started = true;
  16. if (!Array.isArray(data)) {
  17. data = [data];
  18. }
  19. if (data.length === 0 && q.idle()) {
  20. // call drain immediately if there are no tasks
  21. return (0, _setImmediate2.default)(() => q.drain());
  22. }
  23. for (var i = 0, l = data.length; i < l; i++) {
  24. var item = {
  25. data: data[i],
  26. priority,
  27. callback
  28. };
  29. q._tasks.push(item);
  30. }
  31. if (!processingScheduled) {
  32. processingScheduled = true;
  33. (0, _setImmediate2.default)(() => {
  34. processingScheduled = false;
  35. q.process();
  36. });
  37. }
  38. };
  39. // Remove unshift function
  40. delete q.unshift;
  41. return q;
  42. };
  43. var _setImmediate = require('./setImmediate.js');
  44. var _setImmediate2 = _interopRequireDefault(_setImmediate);
  45. var _queue = require('./queue.js');
  46. var _queue2 = _interopRequireDefault(_queue);
  47. var _Heap = require('./internal/Heap.js');
  48. var _Heap2 = _interopRequireDefault(_Heap);
  49. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  50. module.exports = exports['default'];
  51. /**
  52. * The same as [async.queue]{@link module:ControlFlow.queue} only tasks are assigned a priority and
  53. * completed in ascending priority order.
  54. *
  55. * @name priorityQueue
  56. * @static
  57. * @memberOf module:ControlFlow
  58. * @method
  59. * @see [async.queue]{@link module:ControlFlow.queue}
  60. * @category Control Flow
  61. * @param {AsyncFunction} worker - An async function for processing a queued task.
  62. * If you want to handle errors from an individual task, pass a callback to
  63. * `q.push()`.
  64. * Invoked with (task, callback).
  65. * @param {number} concurrency - An `integer` for determining how many `worker`
  66. * functions should be run in parallel. If omitted, the concurrency defaults to
  67. * `1`. If the concurrency is `0`, an error is thrown.
  68. * @returns {module:ControlFlow.QueueObject} A priorityQueue object to manage the tasks. There are two
  69. * differences between `queue` and `priorityQueue` objects:
  70. * * `push(task, priority, [callback])` - `priority` should be a number. If an
  71. * array of `tasks` is given, all tasks will be assigned the same priority.
  72. * * The `unshift` method was removed.
  73. */