root / HServer / 00.Server / 00.Program / node_modules / async / cargo.js
이력 | 보기 | 이력해설 | 다운로드 (4.63 KB)
1 | 39 | HKM | '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 {Function} worker - An asynchronous function for processing an array
|
||
63 | * of queued tasks, which must call its `callback(err)` argument when finished,
|
||
64 | * with an optional `err` argument. Invoked with `(tasks, callback)`.
|
||
65 | * @param {number} [payload=Infinity] - An optional `integer` for determining
|
||
66 | * how many tasks should be processed per round; if omitted, the default is
|
||
67 | * unlimited.
|
||
68 | * @returns {module:ControlFlow.CargoObject} A cargo object to manage the tasks. Callbacks can
|
||
69 | * attached as certain properties to listen for specific events during the
|
||
70 | * lifecycle of the cargo and inner queue.
|
||
71 | * @example
|
||
72 | *
|
||
73 | * // create a cargo object with payload 2
|
||
74 | * var cargo = async.cargo(function(tasks, callback) {
|
||
75 | * for (var i=0; i<tasks.length; i++) {
|
||
76 | * console.log('hello ' + tasks[i].name);
|
||
77 | * }
|
||
78 | * callback();
|
||
79 | * }, 2);
|
||
80 | *
|
||
81 | * // add some items
|
||
82 | * cargo.push({name: 'foo'}, function(err) {
|
||
83 | * console.log('finished processing foo');
|
||
84 | * });
|
||
85 | * cargo.push({name: 'bar'}, function(err) {
|
||
86 | * console.log('finished processing bar');
|
||
87 | * });
|
||
88 | * cargo.push({name: 'baz'}, function(err) {
|
||
89 | * console.log('finished processing baz');
|
||
90 | * });
|
||
91 | */
|
||
92 | function cargo(worker, payload) { |
||
93 | return (0, _queue2.default)(worker, 1, payload); |
||
94 | } |
||
95 | module.exports = exports['default']; |