queue.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = function (worker, concurrency) {
  6. return (0, _queue2.default)(function (items, cb) {
  7. worker(items[0], cb);
  8. }, concurrency, 1);
  9. };
  10. var _queue = require('./internal/queue');
  11. var _queue2 = _interopRequireDefault(_queue);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. module.exports = exports['default'];
  14. /**
  15. * A queue of tasks for the worker function to complete.
  16. * @typedef {Object} QueueObject
  17. * @memberOf module:ControlFlow
  18. * @property {Function} length - a function returning the number of items
  19. * waiting to be processed. Invoke with `queue.length()`.
  20. * @property {boolean} started - a boolean indicating whether or not any
  21. * items have been pushed and processed by the queue.
  22. * @property {Function} running - a function returning the number of items
  23. * currently being processed. Invoke with `queue.running()`.
  24. * @property {Function} workersList - a function returning the array of items
  25. * currently being processed. Invoke with `queue.workersList()`.
  26. * @property {Function} idle - a function returning false if there are items
  27. * waiting or being processed, or true if not. Invoke with `queue.idle()`.
  28. * @property {number} concurrency - an integer for determining how many `worker`
  29. * functions should be run in parallel. This property can be changed after a
  30. * `queue` is created to alter the concurrency on-the-fly.
  31. * @property {Function} push - add a new task to the `queue`. Calls `callback`
  32. * once the `worker` has finished processing the task. Instead of a single task,
  33. * a `tasks` array can be submitted. The respective callback is used for every
  34. * task in the list. Invoke with `queue.push(task, [callback])`,
  35. * @property {Function} unshift - add a new task to the front of the `queue`.
  36. * Invoke with `queue.unshift(task, [callback])`.
  37. * @property {Function} saturated - a callback that is called when the number of
  38. * running workers hits the `concurrency` limit, and further tasks will be
  39. * queued.
  40. * @property {Function} unsaturated - a callback that is called when the number
  41. * of running workers is less than the `concurrency` & `buffer` limits, and
  42. * further tasks will not be queued.
  43. * @property {number} buffer - A minimum threshold buffer in order to say that
  44. * the `queue` is `unsaturated`.
  45. * @property {Function} empty - a callback that is called when the last item
  46. * from the `queue` is given to a `worker`.
  47. * @property {Function} drain - a callback that is called when the last item
  48. * from the `queue` has returned from the `worker`.
  49. * @property {Function} error - a callback that is called when a task errors.
  50. * Has the signature `function(error, task)`.
  51. * @property {boolean} paused - a boolean for determining whether the queue is
  52. * in a paused state.
  53. * @property {Function} pause - a function that pauses the processing of tasks
  54. * until `resume()` is called. Invoke with `queue.pause()`.
  55. * @property {Function} resume - a function that resumes the processing of
  56. * queued tasks when the queue is paused. Invoke with `queue.resume()`.
  57. * @property {Function} kill - a function that removes the `drain` callback and
  58. * empties remaining tasks from the queue forcing it to go idle. Invoke with `queue.kill()`.
  59. */
  60. /**
  61. * Creates a `queue` object with the specified `concurrency`. Tasks added to the
  62. * `queue` are processed in parallel (up to the `concurrency` limit). If all
  63. * `worker`s are in progress, the task is queued until one becomes available.
  64. * Once a `worker` completes a `task`, that `task`'s callback is called.
  65. *
  66. * @name queue
  67. * @static
  68. * @memberOf module:ControlFlow
  69. * @method
  70. * @category Control Flow
  71. * @param {Function} worker - An asynchronous function for processing a queued
  72. * task, which must call its `callback(err)` argument when finished, with an
  73. * optional `error` as an argument. If you want to handle errors from an
  74. * individual task, pass a callback to `q.push()`. Invoked with
  75. * (task, callback).
  76. * @param {number} [concurrency=1] - An `integer` for determining how many
  77. * `worker` functions should be run in parallel. If omitted, the concurrency
  78. * defaults to `1`. If the concurrency is `0`, an error is thrown.
  79. * @returns {module:ControlFlow.QueueObject} A queue object to manage the tasks. Callbacks can
  80. * attached as certain properties to listen for specific events during the
  81. * lifecycle of the queue.
  82. * @example
  83. *
  84. * // create a queue object with concurrency 2
  85. * var q = async.queue(function(task, callback) {
  86. * console.log('hello ' + task.name);
  87. * callback();
  88. * }, 2);
  89. *
  90. * // assign a callback
  91. * q.drain = function() {
  92. * console.log('all items have been processed');
  93. * };
  94. *
  95. * // add some items to the queue
  96. * q.push({name: 'foo'}, function(err) {
  97. * console.log('finished processing foo');
  98. * });
  99. * q.push({name: 'bar'}, function (err) {
  100. * console.log('finished processing bar');
  101. * });
  102. *
  103. * // add some items to the queue (batch-wise)
  104. * q.push([{name: 'baz'},{name: 'bay'},{name: 'bax'}], function(err) {
  105. * console.log('finished processing item');
  106. * });
  107. *
  108. * // add some items to the front of the queue
  109. * q.unshift({name: 'bar'}, function (err) {
  110. * console.log('finished processing bar');
  111. * });
  112. */