Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / batch / index.js
1 /**
2  * Module dependencies.
3  */
4
5 try {
6   var EventEmitter = require('events').EventEmitter;
7 } catch (err) {
8   var Emitter = require('emitter');
9 }
10
11 /**
12  * Noop.
13  */
14
15 function noop(){}
16
17 /**
18  * Expose `Batch`.
19  */
20
21 module.exports = Batch;
22
23 /**
24  * Create a new Batch.
25  */
26
27 function Batch() {
28   if (!(this instanceof Batch)) return new Batch;
29   this.fns = [];
30   this.concurrency(Infinity);
31   this.throws(true);
32   for (var i = 0, len = arguments.length; i < len; ++i) {
33     this.push(arguments[i]);
34   }
35 }
36
37 /**
38  * Inherit from `EventEmitter.prototype`.
39  */
40
41 if (EventEmitter) {
42   Batch.prototype.__proto__ = EventEmitter.prototype;
43 } else {
44   Emitter(Batch.prototype);
45 }
46
47 /**
48  * Set concurrency to `n`.
49  *
50  * @param {Number} n
51  * @return {Batch}
52  * @api public
53  */
54
55 Batch.prototype.concurrency = function(n){
56   this.n = n;
57   return this;
58 };
59
60 /**
61  * Queue a function.
62  *
63  * @param {Function} fn
64  * @return {Batch}
65  * @api public
66  */
67
68 Batch.prototype.push = function(fn){
69   this.fns.push(fn);
70   return this;
71 };
72
73 /**
74  * Set wether Batch will or will not throw up.
75  *
76  * @param  {Boolean} throws
77  * @return {Batch}
78  * @api public
79  */
80 Batch.prototype.throws = function(throws) {
81   this.e = !!throws;
82   return this;
83 };
84
85 /**
86  * Execute all queued functions in parallel,
87  * executing `cb(err, results)`.
88  *
89  * @param {Function} cb
90  * @return {Batch}
91  * @api public
92  */
93
94 Batch.prototype.end = function(cb){
95   var self = this
96     , total = this.fns.length
97     , pending = total
98     , results = []
99     , errors = []
100     , cb = cb || noop
101     , fns = this.fns
102     , max = this.n
103     , throws = this.e
104     , index = 0
105     , done;
106
107   // empty
108   if (!fns.length) return cb(null, results);
109
110   // process
111   function next() {
112     var i = index++;
113     var fn = fns[i];
114     if (!fn) return;
115     var start = new Date;
116
117     try {
118       fn(callback);
119     } catch (err) {
120       callback(err);
121     }
122
123     function callback(err, res){
124       if (done) return;
125       if (err && throws) return done = true, cb(err);
126       var complete = total - pending + 1;
127       var end = new Date;
128
129       results[i] = res;
130       errors[i] = err;
131
132       self.emit('progress', {
133         index: i,
134         value: res,
135         error: err,
136         pending: pending,
137         total: total,
138         complete: complete,
139         percent: complete / total * 100 | 0,
140         start: start,
141         end: end,
142         duration: end - start
143       });
144
145       if (--pending) next();
146       else if(!throws) cb(errors, results);
147       else cb(null, results);
148     }
149   }
150
151   // concurrency
152   for (var i = 0; i < fns.length; i++) {
153     if (i == max) break;
154     next();
155   }
156
157   return this;
158 };