-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (34 loc) · 902 Bytes
/
index.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
module.exports = lock;
function lock(avgs,options,locals,exec){
// break default exec.
exec(false);
// init and set locals.lock data.
if(!locals.lock){
locals.lock = {
reqs:[] // request's arguments repository
,running:false
}
}
var reqs = locals.lock.reqs;
var avgsClone = avgs.concat([]);
reqs.push(avgsClone)
if(!locals.lock.running) {
locals.lock.running = true;
run()
}
function run(){
var req = reqs.shift();
var callback = req.pop();
function cb(){
var args = arguments;
callback.apply(null,args);
if(reqs.length > 0){
run();
}else{
locals.lock.running = false;
}
}
req.push(cb);
exec(req);
}
}