Projekt

Obecné

Profil

Stáhnout (4.54 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
'use strict';
2
3
Object.defineProperty(exports, "__esModule", {
4
  value: true
5
});
6
exports.default = cargo;
7
8
var _queue = require('./internal/queue');
9
10
var _queue2 = _interopRequireDefault(_queue);
11
12
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14
/**
15
 * A cargo of tasks for the worker function to complete. Cargo inherits all of
16
 * the same methods and event callbacks as [`queue`]{@link module:ControlFlow.queue}.
17
 * @typedef {Object} CargoObject
18
 * @memberOf module:ControlFlow
19
 * @property {Function} length - A function returning the number of items
20
 * waiting to be processed. Invoke like `cargo.length()`.
21
 * @property {number} payload - An `integer` for determining how many tasks
22
 * should be process per round. This property can be changed after a `cargo` is
23
 * created to alter the payload on-the-fly.
24
 * @property {Function} push - Adds `task` to the `queue`. The callback is
25
 * called once the `worker` has finished processing the task. Instead of a
26
 * single task, an array of `tasks` can be submitted. The respective callback is
27
 * used for every task in the list. Invoke like `cargo.push(task, [callback])`.
28
 * @property {Function} saturated - A callback that is called when the
29
 * `queue.length()` hits the concurrency and further tasks will be queued.
30
 * @property {Function} empty - A callback that is called when the last item
31
 * from the `queue` is given to a `worker`.
32
 * @property {Function} drain - A callback that is called when the last item
33
 * from the `queue` has returned from the `worker`.
34
 * @property {Function} idle - a function returning false if there are items
35
 * waiting or being processed, or true if not. Invoke like `cargo.idle()`.
36
 * @property {Function} pause - a function that pauses the processing of tasks
37
 * until `resume()` is called. Invoke like `cargo.pause()`.
38
 * @property {Function} resume - a function that resumes the processing of
39
 * queued tasks when the queue is paused. Invoke like `cargo.resume()`.
40
 * @property {Function} kill - a function that removes the `drain` callback and
41
 * empties remaining tasks from the queue forcing it to go idle. Invoke like `cargo.kill()`.
42
 */
43
44
/**
45
 * Creates a `cargo` object with the specified payload. Tasks added to the
46
 * cargo will be processed altogether (up to the `payload` limit). If the
47
 * `worker` is in progress, the task is queued until it becomes available. Once
48
 * the `worker` has completed some tasks, each callback of those tasks is
49
 * called. Check out [these](https://camo.githubusercontent.com/6bbd36f4cf5b35a0f11a96dcd2e97711ffc2fb37/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313637363837312f36383130382f62626330636662302d356632392d313165322d393734662d3333393763363464633835382e676966) [animations](https://camo.githubusercontent.com/f4810e00e1c5f5f8addbe3e9f49064fd5d102699/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313637363837312f36383130312f38346339323036362d356632392d313165322d383134662d3964336430323431336266642e676966)
50
 * for how `cargo` and `queue` work.
51
 *
52
 * While [`queue`]{@link module:ControlFlow.queue} passes only one task to one of a group of workers
53
 * at a time, cargo passes an array of tasks to a single worker, repeating
54
 * when the worker is finished.
55
 *
56
 * @name cargo
57
 * @static
58
 * @memberOf module:ControlFlow
59
 * @method
60
 * @see [async.queue]{@link module:ControlFlow.queue}
61
 * @category Control Flow
62
 * @param {AsyncFunction} worker - An asynchronous function for processing an array
63
 * of queued tasks. Invoked with `(tasks, callback)`.
64
 * @param {number} [payload=Infinity] - An optional `integer` for determining
65
 * how many tasks should be processed per round; if omitted, the default is
66
 * unlimited.
67
 * @returns {module:ControlFlow.CargoObject} A cargo object to manage the tasks. Callbacks can
68
 * attached as certain properties to listen for specific events during the
69
 * lifecycle of the cargo and inner queue.
70
 * @example
71
 *
72
 * // create a cargo object with payload 2
73
 * var cargo = async.cargo(function(tasks, callback) {
74
 *     for (var i=0; i<tasks.length; i++) {
75
 *         console.log('hello ' + tasks[i].name);
76
 *     }
77
 *     callback();
78
 * }, 2);
79
 *
80
 * // add some items
81
 * cargo.push({name: 'foo'}, function(err) {
82
 *     console.log('finished processing foo');
83
 * });
84
 * cargo.push({name: 'bar'}, function(err) {
85
 *     console.log('finished processing bar');
86
 * });
87
 * cargo.push({name: 'baz'}, function(err) {
88
 *     console.log('finished processing baz');
89
 * });
90
 */
91
function cargo(worker, payload) {
92
  return (0, _queue2.default)(worker, 1, payload);
93
}
94
module.exports = exports['default'];