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