queue.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = function (worker, concurrency) {
  6. var _worker = (0, _wrapAsync2.default)(worker);
  7. return (0, _queue2.default)((items, cb) => {
  8. _worker(items[0], cb);
  9. }, concurrency, 1);
  10. };
  11. var _queue = require('./internal/queue.js');
  12. var _queue2 = _interopRequireDefault(_queue);
  13. var _wrapAsync = require('./internal/wrapAsync.js');
  14. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. module.exports = exports['default'];
  17. /**
  18. * A queue of tasks for the worker function to complete.
  19. * @typedef {Iterable} QueueObject
  20. * @memberOf module:ControlFlow
  21. * @property {Function} length - a function returning the number of items
  22. * waiting to be processed. Invoke with `queue.length()`.
  23. * @property {boolean} started - a boolean indicating whether or not any
  24. * items have been pushed and processed by the queue.
  25. * @property {Function} running - a function returning the number of items
  26. * currently being processed. Invoke with `queue.running()`.
  27. * @property {Function} workersList - a function returning the array of items
  28. * currently being processed. Invoke with `queue.workersList()`.
  29. * @property {Function} idle - a function returning false if there are items
  30. * waiting or being processed, or true if not. Invoke with `queue.idle()`.
  31. * @property {number} concurrency - an integer for determining how many `worker`
  32. * functions should be run in parallel. This property can be changed after a
  33. * `queue` is created to alter the concurrency on-the-fly.
  34. * @property {number} payload - an integer that specifies how many items are
  35. * passed to the worker function at a time. only applies if this is a
  36. * [cargo]{@link module:ControlFlow.cargo} object
  37. * @property {AsyncFunction} push - add a new task to the `queue`. Calls `callback`
  38. * once the `worker` has finished processing the task. Instead of a single task,
  39. * a `tasks` array can be submitted. The respective callback is used for every
  40. * task in the list. Invoke with `queue.push(task, [callback])`,
  41. * @property {AsyncFunction} unshift - add a new task to the front of the `queue`.
  42. * Invoke with `queue.unshift(task, [callback])`.
  43. * @property {AsyncFunction} pushAsync - the same as `q.push`, except this returns
  44. * a promise that rejects if an error occurs.
  45. * @property {AsyncFunction} unshiftAsync - the same as `q.unshift`, except this returns
  46. * a promise that rejects if an error occurs.
  47. * @property {Function} remove - remove items from the queue that match a test
  48. * function. The test function will be passed an object with a `data` property,
  49. * and a `priority` property, if this is a
  50. * [priorityQueue]{@link module:ControlFlow.priorityQueue} object.
  51. * Invoked with `queue.remove(testFn)`, where `testFn` is of the form
  52. * `function ({data, priority}) {}` and returns a Boolean.
  53. * @property {Function} saturated - a function that sets a callback that is
  54. * called when the number of running workers hits the `concurrency` limit, and
  55. * further tasks will be queued. If the callback is omitted, `q.saturated()`
  56. * returns a promise for the next occurrence.
  57. * @property {Function} unsaturated - a function that sets a callback that is
  58. * called when the number of running workers is less than the `concurrency` &
  59. * `buffer` limits, and further tasks will not be queued. If the callback is
  60. * omitted, `q.unsaturated()` returns a promise for the next occurrence.
  61. * @property {number} buffer - A minimum threshold buffer in order to say that
  62. * the `queue` is `unsaturated`.
  63. * @property {Function} empty - a function that sets a callback that is called
  64. * when the last item from the `queue` is given to a `worker`. If the callback
  65. * is omitted, `q.empty()` returns a promise for the next occurrence.
  66. * @property {Function} drain - a function that sets a callback that is called
  67. * when the last item from the `queue` has returned from the `worker`. If the
  68. * callback is omitted, `q.drain()` returns a promise for the next occurrence.
  69. * @property {Function} error - a function that sets a callback that is called
  70. * when a task errors. Has the signature `function(error, task)`. If the
  71. * callback is omitted, `error()` returns a promise that rejects on the next
  72. * error.
  73. * @property {boolean} paused - a boolean for determining whether the queue is
  74. * in a paused state.
  75. * @property {Function} pause - a function that pauses the processing of tasks
  76. * until `resume()` is called. Invoke with `queue.pause()`.
  77. * @property {Function} resume - a function that resumes the processing of
  78. * queued tasks when the queue is paused. Invoke with `queue.resume()`.
  79. * @property {Function} kill - a function that removes the `drain` callback and
  80. * empties remaining tasks from the queue forcing it to go idle. No more tasks
  81. * should be pushed to the queue after calling this function. Invoke with `queue.kill()`.
  82. *
  83. * @example
  84. * const q = async.queue(worker, 2)
  85. * q.push(item1)
  86. * q.push(item2)
  87. * q.push(item3)
  88. * // queues are iterable, spread into an array to inspect
  89. * const items = [...q] // [item1, item2, item3]
  90. * // or use for of
  91. * for (let item of q) {
  92. * console.log(item)
  93. * }
  94. *
  95. * q.drain(() => {
  96. * console.log('all done')
  97. * })
  98. * // or
  99. * await q.drain()
  100. */
  101. /**
  102. * Creates a `queue` object with the specified `concurrency`. Tasks added to the
  103. * `queue` are processed in parallel (up to the `concurrency` limit). If all
  104. * `worker`s are in progress, the task is queued until one becomes available.
  105. * Once a `worker` completes a `task`, that `task`'s callback is called.
  106. *
  107. * @name queue
  108. * @static
  109. * @memberOf module:ControlFlow
  110. * @method
  111. * @category Control Flow
  112. * @param {AsyncFunction} worker - An async function for processing a queued task.
  113. * If you want to handle errors from an individual task, pass a callback to
  114. * `q.push()`. Invoked with (task, callback).
  115. * @param {number} [concurrency=1] - An `integer` for determining how many
  116. * `worker` functions should be run in parallel. If omitted, the concurrency
  117. * defaults to `1`. If the concurrency is `0`, an error is thrown.
  118. * @returns {module:ControlFlow.QueueObject} A queue object to manage the tasks. Callbacks can be
  119. * attached as certain properties to listen for specific events during the
  120. * lifecycle of the queue.
  121. * @example
  122. *
  123. * // create a queue object with concurrency 2
  124. * var q = async.queue(function(task, callback) {
  125. * console.log('hello ' + task.name);
  126. * callback();
  127. * }, 2);
  128. *
  129. * // assign a callback
  130. * q.drain(function() {
  131. * console.log('all items have been processed');
  132. * });
  133. * // or await the end
  134. * await q.drain()
  135. *
  136. * // assign an error callback
  137. * q.error(function(err, task) {
  138. * console.error('task experienced an error');
  139. * });
  140. *
  141. * // add some items to the queue
  142. * q.push({name: 'foo'}, function(err) {
  143. * console.log('finished processing foo');
  144. * });
  145. * // callback is optional
  146. * q.push({name: 'bar'});
  147. *
  148. * // add some items to the queue (batch-wise)
  149. * q.push([{name: 'baz'},{name: 'bay'},{name: 'bax'}], function(err) {
  150. * console.log('finished processing item');
  151. * });
  152. *
  153. * // add some items to the front of the queue
  154. * q.unshift({name: 'bar'}, function (err) {
  155. * console.log('finished processing bar');
  156. * });
  157. */