-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (69 loc) · 1.55 KB
/
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
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
module.exports = function(){
var errand = {
args:arguments,
errands:[],
asyncs:[],
index:0,
running:false,
next:function(){
if(errand.errands[errand.index]){
errand.running = true;
var fn = errand.errands[errand.index];
setTimeout(function(){
fn.apply(errand, errand.args);
if(!errand.asyncs[errand.index - 1]){
errand.next();
}
}, 0);
errand.index++;
}else{
errand.running = false;
}
return errand;
},
run:function(fn, async){
async = async || 0;
if(typeof fn === 'function'){
errand.errands.push(fn);
errand.asyncs.push(async);
}else if(typeof fn === 'object'){
for(var i in fn){
errand.errands.push(fn[i]);
errand.asyncs.push(async);
}
}
return !errand.running ? errand.next() : errand;
},
wait:function(fn){
fn = fn || 1000;
if(typeof fn === 'number'){
errand.run(function(){
setTimeout(this.next, fn);
}, 1);
}else{
errand.run(fn, 1);
}
return !errand.running ? errand.next() : errand;
},
repeat:function(i){
i = i || 1;
var fn = errand.errands[errand.errands.length - 1];
var async = errand.asyncs[errand.asyncs.length - 1];
if(fn){
for(; i; i--){
errand.run(fn, async);
}
}
return !errand.running ? errand.next() : errand;
},
skip:function(i){
i = i || 1;
errand.run(function(){
errand.index += i;
errand.next();
}, 0)
return !errand.running ? errand.next() : errand;
}
}
return errand;
}