source: trip-planner-front/node_modules/async-limiter/index.js@ 59329aa

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

initial commit

  • Property mode set to 100644
File size: 1.2 KB
RevLine 
[6a3a178]1'use strict';
2
3function Queue(options) {
4 if (!(this instanceof Queue)) {
5 return new Queue(options);
6 }
7
8 options = options || {};
9 this.concurrency = options.concurrency || Infinity;
10 this.pending = 0;
11 this.jobs = [];
12 this.cbs = [];
13 this._done = done.bind(this);
14}
15
16var arrayAddMethods = [
17 'push',
18 'unshift',
19 'splice'
20];
21
22arrayAddMethods.forEach(function(method) {
23 Queue.prototype[method] = function() {
24 var methodResult = Array.prototype[method].apply(this.jobs, arguments);
25 this._run();
26 return methodResult;
27 };
28});
29
30Object.defineProperty(Queue.prototype, 'length', {
31 get: function() {
32 return this.pending + this.jobs.length;
33 }
34});
35
36Queue.prototype._run = function() {
37 if (this.pending === this.concurrency) {
38 return;
39 }
40 if (this.jobs.length) {
41 var job = this.jobs.shift();
42 this.pending++;
43 job(this._done);
44 this._run();
45 }
46
47 if (this.pending === 0) {
48 while (this.cbs.length !== 0) {
49 var cb = this.cbs.pop();
50 process.nextTick(cb);
51 }
52 }
53};
54
55Queue.prototype.onDone = function(cb) {
56 if (typeof cb === 'function') {
57 this.cbs.push(cb);
58 this._run();
59 }
60};
61
62function done() {
63 this.pending--;
64 this._run();
65}
66
67module.exports = Queue;
Note: See TracBrowser for help on using the repository browser.