Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

queue.js 5.7KB

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