-
Notifications
You must be signed in to change notification settings - Fork 3
/
async2.js
139 lines (139 loc) · 5.94 KB
/
async2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Generated by CoffeeScript 1.7.1
!(function(context, definition) {
if ('function' === typeof define) {
return define(function() {
return definition;
});
} else if ('function' === typeof require && typeof exports === typeof module) {
return module.exports = definition;
}
return context['async'] = definition;
})(this, (function() {
var A, _callback, _static;
A = function async() {
if (typeof this.serial === 'undefined') {
var a = new A(), k;
for (k in arguments[0]) {
a[k](arguments[0][k]);
}
return a;
}
this.a = [];
this._reset();
this.beginning_results = [].slice.call(arguments, 0);
};;
A.prototype._reset = function() {
this.beginning_results = [];
this.beginning_length = 0;
this.processing = false;
return this.processed = 0;
};
A.prototype._apply = function(args) {
if (this.a.length) {
return (args[0] ? this.a.splice(0, this.a.length)[0] : this.a[this.a.length - 1]).apply({}, args);
}
};
A.prototype._next = function(parallel) {
return (function(_this) {
return function() {
_this.processed++;
if (arguments[0]) {
return _this._apply(arguments);
}
_this.afterEach_callback.apply(_this._next(!_this.afterEach_callback.length), arguments);
if (!parallel || _this.processed === _this.beginning_length) {
while (_this._apply(arguments) && parallel) {}
}
};
})(this);
};
A.prototype._push = function(args, parallel) {
var dont_end, key, task, _fn;
task = args[0];
'[object Function]' === Object.prototype.toString.call(task) && (dont_end = task = [task]);
_fn = (function(_this) {
return function(cb, parallel) {
_this.beginning_length++;
return _this.a.push(function() {
var next;
_this.a.pop();
_this.beforeEach_callback.apply(_this._next(!_this.beforeEach_callback.length), arguments);
args = Array.prototype.slice.apply(arguments).slice(1);
cb.apply((next = _this._next(parallel)), args.concat(next));
return parallel && (1 === _this.a.length || !!_this._apply(arguments));
});
};
})(this);
for (key in task) {
_fn(task[key], parallel === null ? !task[key].length : parallel);
}
return (dont_end ? this : this.end(typeof args[1] === 'function' ? args[1] : function() {}));
};
A.prototype.end = A.prototype["finally"] = A.prototype.ensure = A.prototype.afterAll = A.prototype.after = A.prototype.complete = A.prototype.done = A.prototype.go = function(cb) {
if (this.processing) {
return;
}
this.processing = true;
this.a.push((function(_this) {
return function() {
_this.a.pop();
if (arguments[0]) {
_this.error_callback.apply(_this._next(!_this.error_callback.length), arguments);
} else {
_this.success_callback.apply(_this._next(!_this.success_callback.length), arguments);
}
if (typeof cb === 'function') {
cb.apply(null, arguments);
}
return _this._reset();
};
})(this));
this.a.reverse();
(this.begin_callback = this.begin_callback || function() {}) && (this.beforeAll_callback = this.beforeAll_callback || function() {}) && (this.beforeEach_callback = this.beforeEach_callback || function() {}) && (this.afterEach_callback = this.afterEach_callback || function() {}) && (this.error_callback = this.error_callback || function() {}) && (this.success_callback = this.success_callback || function() {});
this.beforeAll_callback.apply(this._next(!this.beforeAll_callback.length), arguments);
this._apply([null].concat(this.beginning_results));
return this;
};
A.prototype.serial = A.prototype.series = A.prototype.blocking = A.prototype.waterfall = function() {
return this._push(arguments, false);
};
A.prototype.parallel = A.prototype.nonblocking = function() {
return this._push(arguments, true);
};
A.prototype["do"] = A.prototype.then = A.prototype["try"] = A.prototype.begin = A.prototype.start = A.prototype.auto = function() {
return this._push(arguments, null);
};
A.prototype["new"] = A.prototype.flow = A.prototype["with"] = function() {
this.beginning_results = [].slice.call(arguments, 0);
return this;
};
(_callback = function(func) {
return function(cb) {
this[func + '_callback'] = cb;
return this;
};
}) && (A.prototype.beforeAll = A.prototype.before = _callback('beforeAll')) && (A.prototype.beforeEach = _callback('beforeEach')) && (A.prototype.afterEach = A.prototype.between = A.prototype.inbetween = _callback('afterEach')) && (A.prototype.error = A.prototype["catch"] = A.prototype.rescue = _callback('error')) && (A.prototype.success = A.prototype["else"] = _callback('success'));
(_static = function(func) {
return function() {
var b;
return (b = new A)[func].apply(b, arguments);
};
}) && (A.serial = A.series = A.blocking = A.waterfall = _static('serial')) && (A.parallel = A.nonblocking = _static('parallel')) && (A["do"] = A.then = A["try"] = A.begin = A.start = A.auto = _static('do')) && (A.end = A["finally"] = A.ensure = A.afterAll = A.after = A.complete = A.done = A.go = _static('end')) && (A["new"] = A.flow = A["with"] = _static('new')) && (A.beforeAll = A.before = _static('beforeAll')) && (A.beforeEach = _static('beforeEach')) && (A.afterEach = A.between = A.inbetween = _static('afterEach')) && (A.error = A["catch"] = A.rescue = _static('error')) && (A.success = A["else"] = _static('success'));
A.q = {};
A.nextTickGroup = A.push = function(g, f) {
A.q[g] = A.q[g] || new A;
A.q[g].serial(f).go();
return A;
};
A.whilst = function(test, iterator, cb) {
(test() && iterator((function(_this) {
return function(err) {
return (!!err && cb(err)) || _this.whilst(test, iterator, cb);
};
})(this))) || cb();
};
A.delay = function(ms, f) {
return setTimeout(f, ms);
};
return A;
})());