cargo.js 4.5 KB

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