forked from caolan/async
-
Notifications
You must be signed in to change notification settings - Fork 2
/
control.js
150 lines (137 loc) · 4.24 KB
/
control.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
140
141
142
143
144
145
146
147
148
149
150
'use strict';
var antisync = {
each: require( './each' ),
eachSeries: require( './eachseries' )
};
var utils = require( './utils' );
var control = module.exports = {};
control.limited = function limited( limit ) {
return function( arr, iterator, callback ) {
callback = callback || function() {};
if ( !arr.length || limit <= 0 ) {
return callback();
}
var completed = 0;
var started = 0;
var running = 0;
( function replenish() {
if ( completed >= arr.length ) {
return callback();
}
function iterate( err ) {
if ( err ) {
callback( err );
callback = function() {};
}
else {
completed += 1;
running -= 1;
if ( completed >= arr.length ) {
callback();
}
else {
replenish();
}
}
}
while ( running < limit && started < arr.length ) {
started += 1;
running += 1;
iterator( arr[ started - 1 ], iterate );
}
} )();
};
};
control.parallel = function parallel( fn ) {
return function() {
var args = Array.prototype.slice.call( arguments );
return fn.apply( null, [ antisync.each ].concat( args ) );
};
};
control.parallelLimit = function parallelLimit( limit, fn ) {
return function() {
var args = Array.prototype.slice.call( arguments );
return fn.apply( null, [ control.limited( limit ) ].concat( args ) );
};
};
control.series = function series( fn ) {
return function() {
var args = Array.prototype.slice.call( arguments );
return fn.apply( null, [ antisync.eachSeries ].concat( args ) );
};
};
control.iterator = function iterator( tasks ) {
var makeCallback = function( index ) {
var fn = function() {
if ( tasks.length ) {
tasks[ index ].apply( null, arguments );
}
return fn.next();
};
fn.next = function() {
return ( index < tasks.length - 1 ) ? makeCallback( index + 1 ) : null;
};
return fn;
};
return makeCallback( 0 );
};
control.parallelApply = function parallelApply( eachfn, tasks, callback ) {
callback = callback || function() {};
if ( utils.isArray( tasks ) ) {
eachfn.map( tasks, function( fn, callback ) {
if ( fn ) {
fn( function( err ) {
var args = Array.prototype.slice.call( arguments, 1 );
if ( args.length <= 1 ) {
args = args[ 0 ];
}
callback.call( null, err, args );
} );
}
}, callback );
}
else {
var results = {};
eachfn.each( utils.keys( tasks ), function( k, callback ) {
tasks[ k ]( function( err ) {
var args = Array.prototype.slice.call( arguments, 1 );
if ( args.length <= 1 ) {
args = args[ 0 ];
}
results[ k ] = args;
callback( err );
} );
}, function( err ) {
callback( err, results );
} );
}
};
control.applyEach = function applyEach( eachfn, fns /*args...*/ ) {
var go = function() {
var that = this;
var args = Array.prototype.slice.call( arguments );
var callback = args.pop();
return eachfn( fns, function( fn, cb ) {
fn.apply( that, args.concat( [ cb ] ) );
},
callback );
};
if ( arguments.length > 2 ) {
var args = Array.prototype.slice.call( arguments, 2 );
return go.apply( this, args );
}
else {
return go;
}
};
control.concat = function concat( eachfn, arr, fn, callback ) {
var r = [];
eachfn( arr, function( x, cb ) {
fn( x, function( err, y ) {
r = r.concat( y || [] );
cb( err );
} );
}, function( err ) {
callback( err, r );
} );
};