Projekt

Obecné

Profil

Stáhnout (5.72 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
'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)(function (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 {Object} 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 {Function} push - add a new task to the `queue`. Calls `callback`
44
 * once the `worker` has finished processing the task. Instead of a single task,
45
 * a `tasks` array can be submitted. The respective callback is used for every
46
 * task in the list. Invoke with `queue.push(task, [callback])`,
47
 * @property {Function} unshift - add a new task to the front of the `queue`.
48
 * Invoke with `queue.unshift(task, [callback])`.
49
 * @property {Function} remove - remove items from the queue that match a test
50
 * function.  The test function will be passed an object with a `data` property,
51
 * and a `priority` property, if this is a
52
 * [priorityQueue]{@link module:ControlFlow.priorityQueue} object.
53
 * Invoked with `queue.remove(testFn)`, where `testFn` is of the form
54
 * `function ({data, priority}) {}` and returns a Boolean.
55
 * @property {Function} saturated - a callback that is called when the number of
56
 * running workers hits the `concurrency` limit, and further tasks will be
57
 * queued.
58
 * @property {Function} unsaturated - a callback that is called when the number
59
 * of running workers is less than the `concurrency` & `buffer` limits, and
60
 * further tasks will not be queued.
61
 * @property {number} buffer - A minimum threshold buffer in order to say that
62
 * the `queue` is `unsaturated`.
63
 * @property {Function} empty - a callback that is called when the last item
64
 * from the `queue` is given to a `worker`.
65
 * @property {Function} drain - a callback that is called when the last item
66
 * from the `queue` has returned from the `worker`.
67
 * @property {Function} error - a callback that is called when a task errors.
68
 * Has the signature `function(error, task)`.
69
 * @property {boolean} paused - a boolean for determining whether the queue is
70
 * in a paused state.
71
 * @property {Function} pause - a function that pauses the processing of tasks
72
 * until `resume()` is called. Invoke with `queue.pause()`.
73
 * @property {Function} resume - a function that resumes the processing of
74
 * queued tasks when the queue is paused. Invoke with `queue.resume()`.
75
 * @property {Function} kill - a function that removes the `drain` callback and
76
 * empties remaining tasks from the queue forcing it to go idle. No more tasks
77
 * should be pushed to the queue after calling this function. Invoke with `queue.kill()`.
78
 */
79
80
/**
81
 * Creates a `queue` object with the specified `concurrency`. Tasks added to the
82
 * `queue` are processed in parallel (up to the `concurrency` limit). If all
83
 * `worker`s are in progress, the task is queued until one becomes available.
84
 * Once a `worker` completes a `task`, that `task`'s callback is called.
85
 *
86
 * @name queue
87
 * @static
88
 * @memberOf module:ControlFlow
89
 * @method
90
 * @category Control Flow
91
 * @param {AsyncFunction} worker - An async function for processing a queued task.
92
 * If you want to handle errors from an individual task, pass a callback to
93
 * `q.push()`. Invoked with (task, callback).
94
 * @param {number} [concurrency=1] - An `integer` for determining how many
95
 * `worker` functions should be run in parallel.  If omitted, the concurrency
96
 * defaults to `1`.  If the concurrency is `0`, an error is thrown.
97
 * @returns {module:ControlFlow.QueueObject} A queue object to manage the tasks. Callbacks can
98
 * attached as certain properties to listen for specific events during the
99
 * lifecycle of the queue.
100
 * @example
101
 *
102
 * // create a queue object with concurrency 2
103
 * var q = async.queue(function(task, callback) {
104
 *     console.log('hello ' + task.name);
105
 *     callback();
106
 * }, 2);
107
 *
108
 * // assign a callback
109
 * q.drain = function() {
110
 *     console.log('all items have been processed');
111
 * };
112
 *
113
 * // add some items to the queue
114
 * q.push({name: 'foo'}, function(err) {
115
 *     console.log('finished processing foo');
116
 * });
117
 * q.push({name: 'bar'}, function (err) {
118
 *     console.log('finished processing bar');
119
 * });
120
 *
121
 * // add some items to the queue (batch-wise)
122
 * q.push([{name: 'baz'},{name: 'bay'},{name: 'bax'}], function(err) {
123
 *     console.log('finished processing item');
124
 * });
125
 *
126
 * // add some items to the front of the queue
127
 * q.unshift({name: 'bar'}, function (err) {
128
 *     console.log('finished processing bar');
129
 * });
130
 */