File | 32 |33 | | Statements | 34 |35 | | Branches | 36 |37 | | Functions | 38 |39 | | Lines | 40 |41 | |
---|---|---|---|---|---|---|---|---|---|
index.js | 45 |46 | | 100% | 47 |(37 / 37) | 48 |92.86% | 49 |(13 / 14) | 50 |100% | 51 |(7 / 7) | 52 |100% | 53 |(37 / 37) | 54 |
1 29 | 2 30 | 3 31 | 4 32 | 5 33 | 6 34 | 7 35 | 8 36 | 9 37 | 10 38 | 11 39 | 12 40 | 13 41 | 14 42 | 15 43 | 16 44 | 17 45 | 18 46 | 19 47 | 20 48 | 21 49 | 22 50 | 23 51 | 24 52 | 25 53 | 26 54 | 27 55 | 28 56 | 29 57 | 30 58 | 31 59 | 32 60 | 33 61 | 34 62 | 35 63 | 36 64 | 37 65 | 38 66 | 39 67 | 40 68 | 41 69 | 42 70 | 43 71 | 44 72 | 45 73 | 46 74 | 47 75 | 48 76 | 49 77 | 50 78 | 51 79 | 52 80 | 53 81 | 54 82 | 55 83 | 56 84 | 57 85 | 58 86 | 59 87 | 60 88 | 61 89 | 62 90 | 63 91 | 64 92 | 65 93 | 66 94 | 67 95 | 68 | 96 | 97 | 1 98 | 7 99 | 1 100 | 101 | 102 | 6 103 | 6 104 | 6 105 | 6 106 | 6 107 | 6 108 | 109 | 110 | 1 111 | 112 | 113 | 114 | 115 | 116 | 1 117 | 3 118 | 13 119 | 13 120 | 13 121 | 122 | 123 | 124 | 1 125 | 126 | 19 127 | 128 | 129 | 130 | 1 131 | 45 132 | 6 133 | 134 | 39 135 | 13 136 | 13 137 | 13 138 | 13 139 | 140 | 141 | 39 142 | 18 143 | 6 144 | 6 145 | 146 | 147 | 148 | 149 | 1 150 | 6 151 | 6 152 | 6 153 | 154 | 155 | 156 | 1 157 | 13 158 | 13 159 | 160 | 161 | 1 162 | | 'use strict';
163 |
164 | function Queue(options) {
165 | if (!(this instanceof Queue)) {
166 | return new Queue(options);
167 | }
168 |
169 | options = options || {};
170 | this.concurrency = options.concurrency || Infinity;
171 | this.pending = 0;
172 | this.jobs = [];
173 | this.cbs = [];
174 | this._done = done.bind(this);
175 | }
176 |
177 | var arrayAddMethods = [
178 | 'push',
179 | 'unshift',
180 | 'splice'
181 | ];
182 |
183 | arrayAddMethods.forEach(function(method) {
184 | Queue.prototype[method] = function() {
185 | var methodResult = Array.prototype[method].apply(this.jobs, arguments);
186 | this._run();
187 | return methodResult;
188 | };
189 | });
190 |
191 | Object.defineProperty(Queue.prototype, 'length', {
192 | get: function() {
193 | return this.pending + this.jobs.length;
194 | }
195 | });
196 |
197 | Queue.prototype._run = function() {
198 | if (this.pending === this.concurrency) {
199 | return;
200 | }
201 | if (this.jobs.length) {
202 | var job = this.jobs.shift();
203 | this.pending++;
204 | job(this._done);
205 | this._run();
206 | }
207 |
208 | if (this.pending === 0) {
209 | while (this.cbs.length !== 0) {
210 | var cb = this.cbs.pop();
211 | process.nextTick(cb);
212 | }
213 | }
214 | };
215 |
216 | Queue.prototype.onDone = function(cb) {
217 | Eif (typeof cb === 'function') {
218 | this.cbs.push(cb);
219 | this._run();
220 | }
221 | };
222 |
223 | function done() {
224 | this.pending--;
225 | this._run();
226 | }
227 |
228 | module.exports = Queue;
229 | |
File | 32 |33 | | Statements | 34 |35 | | Branches | 36 |37 | | Functions | 38 |39 | | Lines | 40 |41 | |
---|---|---|---|---|---|---|---|---|---|
async-throttle/ | 45 |46 | | 100% | 47 |(37 / 37) | 48 |92.86% | 49 |(13 / 14) | 50 |100% | 51 |(7 / 7) | 52 |100% | 53 |(37 / 37) | 54 |