source: trip-planner-front/node_modules/async/internal/queue.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.6 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = queue;
7
8var _baseIndexOf = require('lodash/_baseIndexOf');
9
10var _baseIndexOf2 = _interopRequireDefault(_baseIndexOf);
11
12var _isArray = require('lodash/isArray');
13
14var _isArray2 = _interopRequireDefault(_isArray);
15
16var _noop = require('lodash/noop');
17
18var _noop2 = _interopRequireDefault(_noop);
19
20var _onlyOnce = require('./onlyOnce');
21
22var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
23
24var _setImmediate = require('./setImmediate');
25
26var _setImmediate2 = _interopRequireDefault(_setImmediate);
27
28var _DoublyLinkedList = require('./DoublyLinkedList');
29
30var _DoublyLinkedList2 = _interopRequireDefault(_DoublyLinkedList);
31
32var _wrapAsync = require('./wrapAsync');
33
34var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
35
36function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
37
38function queue(worker, concurrency, payload) {
39 if (concurrency == null) {
40 concurrency = 1;
41 } else if (concurrency === 0) {
42 throw new Error('Concurrency must not be zero');
43 }
44
45 var _worker = (0, _wrapAsync2.default)(worker);
46 var numRunning = 0;
47 var workersList = [];
48
49 var processingScheduled = false;
50 function _insert(data, insertAtFront, callback) {
51 if (callback != null && typeof callback !== 'function') {
52 throw new Error('task callback must be a function');
53 }
54 q.started = true;
55 if (!(0, _isArray2.default)(data)) {
56 data = [data];
57 }
58 if (data.length === 0 && q.idle()) {
59 // call drain immediately if there are no tasks
60 return (0, _setImmediate2.default)(function () {
61 q.drain();
62 });
63 }
64
65 for (var i = 0, l = data.length; i < l; i++) {
66 var item = {
67 data: data[i],
68 callback: callback || _noop2.default
69 };
70
71 if (insertAtFront) {
72 q._tasks.unshift(item);
73 } else {
74 q._tasks.push(item);
75 }
76 }
77
78 if (!processingScheduled) {
79 processingScheduled = true;
80 (0, _setImmediate2.default)(function () {
81 processingScheduled = false;
82 q.process();
83 });
84 }
85 }
86
87 function _next(tasks) {
88 return function (err) {
89 numRunning -= 1;
90
91 for (var i = 0, l = tasks.length; i < l; i++) {
92 var task = tasks[i];
93
94 var index = (0, _baseIndexOf2.default)(workersList, task, 0);
95 if (index === 0) {
96 workersList.shift();
97 } else if (index > 0) {
98 workersList.splice(index, 1);
99 }
100
101 task.callback.apply(task, arguments);
102
103 if (err != null) {
104 q.error(err, task.data);
105 }
106 }
107
108 if (numRunning <= q.concurrency - q.buffer) {
109 q.unsaturated();
110 }
111
112 if (q.idle()) {
113 q.drain();
114 }
115 q.process();
116 };
117 }
118
119 var isProcessing = false;
120 var q = {
121 _tasks: new _DoublyLinkedList2.default(),
122 concurrency: concurrency,
123 payload: payload,
124 saturated: _noop2.default,
125 unsaturated: _noop2.default,
126 buffer: concurrency / 4,
127 empty: _noop2.default,
128 drain: _noop2.default,
129 error: _noop2.default,
130 started: false,
131 paused: false,
132 push: function (data, callback) {
133 _insert(data, false, callback);
134 },
135 kill: function () {
136 q.drain = _noop2.default;
137 q._tasks.empty();
138 },
139 unshift: function (data, callback) {
140 _insert(data, true, callback);
141 },
142 remove: function (testFn) {
143 q._tasks.remove(testFn);
144 },
145 process: function () {
146 // Avoid trying to start too many processing operations. This can occur
147 // when callbacks resolve synchronously (#1267).
148 if (isProcessing) {
149 return;
150 }
151 isProcessing = true;
152 while (!q.paused && numRunning < q.concurrency && q._tasks.length) {
153 var tasks = [],
154 data = [];
155 var l = q._tasks.length;
156 if (q.payload) l = Math.min(l, q.payload);
157 for (var i = 0; i < l; i++) {
158 var node = q._tasks.shift();
159 tasks.push(node);
160 workersList.push(node);
161 data.push(node.data);
162 }
163
164 numRunning += 1;
165
166 if (q._tasks.length === 0) {
167 q.empty();
168 }
169
170 if (numRunning === q.concurrency) {
171 q.saturated();
172 }
173
174 var cb = (0, _onlyOnce2.default)(_next(tasks));
175 _worker(data, cb);
176 }
177 isProcessing = false;
178 },
179 length: function () {
180 return q._tasks.length;
181 },
182 running: function () {
183 return numRunning;
184 },
185 workersList: function () {
186 return workersList;
187 },
188 idle: function () {
189 return q._tasks.length + numRunning === 0;
190 },
191 pause: function () {
192 q.paused = true;
193 },
194 resume: function () {
195 if (q.paused === false) {
196 return;
197 }
198 q.paused = false;
199 (0, _setImmediate2.default)(q.process);
200 }
201 };
202 return q;
203}
204module.exports = exports['default'];
Note: See TracBrowser for help on using the repository browser.