UNPKG

2.65 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7exports.default = function (worker, concurrency) {
8 // Start with a normal queue
9 var q = (0, _queue2.default)(worker, concurrency);
10
11 q._tasks = new _Heap2.default();
12
13 // Override push to accept second parameter representing priority
14 q.push = function (data, priority = 0, callback = () => {}) {
15 if (typeof callback !== 'function') {
16 throw new Error('task callback must be a function');
17 }
18 q.started = true;
19 if (!Array.isArray(data)) {
20 data = [data];
21 }
22 if (data.length === 0 && q.idle()) {
23 // call drain immediately if there are no tasks
24 return (0, _setImmediate2.default)(() => q.drain());
25 }
26
27 for (var i = 0, l = data.length; i < l; i++) {
28 var item = {
29 data: data[i],
30 priority,
31 callback
32 };
33
34 q._tasks.push(item);
35 }
36
37 (0, _setImmediate2.default)(q.process);
38 };
39
40 // Remove unshift function
41 delete q.unshift;
42
43 return q;
44};
45
46var _setImmediate = require('./setImmediate');
47
48var _setImmediate2 = _interopRequireDefault(_setImmediate);
49
50var _queue = require('./queue');
51
52var _queue2 = _interopRequireDefault(_queue);
53
54var _Heap = require('./internal/Heap');
55
56var _Heap2 = _interopRequireDefault(_Heap);
57
58function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
59
60module.exports = exports['default'];
61
62/**
63 * The same as [async.queue]{@link module:ControlFlow.queue} only tasks are assigned a priority and
64 * completed in ascending priority order.
65 *
66 * @name priorityQueue
67 * @static
68 * @memberOf module:ControlFlow
69 * @method
70 * @see [async.queue]{@link module:ControlFlow.queue}
71 * @category Control Flow
72 * @param {AsyncFunction} worker - An async function for processing a queued task.
73 * If you want to handle errors from an individual task, pass a callback to
74 * `q.push()`.
75 * Invoked with (task, callback).
76 * @param {number} concurrency - An `integer` for determining how many `worker`
77 * functions should be run in parallel. If omitted, the concurrency defaults to
78 * `1`. If the concurrency is `0`, an error is thrown.
79 * @returns {module:ControlFlow.QueueObject} A priorityQueue object to manage the tasks. There are two
80 * differences between `queue` and `priorityQueue` objects:
81 * * `push(task, priority, [callback])` - `priority` should be a number. If an
82 * array of `tasks` is given, all tasks will be assigned the same priority.
83 * * The `unshift` method was removed.
84 */
\No newline at end of file