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