-
Notifications
You must be signed in to change notification settings - Fork 0
/
snc.js
244 lines (224 loc) · 6.13 KB
/
snc.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* exported snc */
var snc = {
each: function (array, callback, response) {
var i = 0
var store = []
var done = function (data) {
if (undefined !== data && data !== null) store[i - 1] = data
if (i < array.length) {
var y = i
i++
setTimeout(function () {
callback(array[y], y, done, end)
}, 0)
} else if (typeof response === 'function') response(store)
}
var end = function (data) {
if (typeof response === 'function') response(data)
}
if (i < array.length) done()
else if (typeof response === 'function') response(store)
},
waterfall: function (callbacks, response) {
var i = 0
var done = function (data, respdata) {
if (i < callbacks.length - 1) {
i++
if (data !== null && data !== undefined) {
if (data === true) {
if (typeof response === 'function') response(respdata)
} else {
setTimeout(function () {
callbacks[i](done, data)
}, 0)
}
} else {
setTimeout(function () {
callbacks[i](done)
}, 0)
}
} else {
if (typeof response === 'function') {
if (data !== null && data !== undefined) {
if (data === true) response(respdata)
else response(data)
} else response()
}
}
}
if (callbacks instanceof Array) {
setTimeout(function () {
callbacks[i](done)
}, 0)
}
},
forever: function (callback, response) {
var end = function (data) {
if (typeof response === 'function') response(data)
}
var next = function () {
setTimeout(function () {
callback(next, end)
}, 0)
}
callback(next, end)
},
parallelLimit: function (limit, callbacks, response) {
var it = 0
var to = callbacks.length
var store = []
var async = function (ix) {
var done = function (data) {
to--
if (data !== null && data !== undefined) store[ix] = data
if (it !== callbacks.length) {
async(it)
it++
} else if (to === 0 && typeof response === 'function') {
response(store)
}
}
setTimeout(function () {
callbacks[ix](done)
}, 0)
}
if (callbacks instanceof Array) {
for (var i = 0; i < limit; i++) {
async(i)
it++
}
}
},
forSync: function (ini, fin, inc, callback, response) {
var i = ini
var store = []
var end = function (data) {
if (typeof response === 'function') response(data)
}
var done = function (data) {
if (undefined !== data && data !== null) store.push(data)
i += inc
if (i < fin) {
setTimeout(function () {
callback(i, done, end)
}, 0)
} else end(store)
}
if (i < fin) callback(i, done, end)
else end()
},
eachParallelLimit: function (array, limit, callback, response) {
var it = 0
var to = array.length
var store = []
var async = function (item, index) {
var done = function (data) {
to--
if (data !== null && data !== undefined) store[index] = data
if (it !== array.length) {
async(array[it], it)
it++
} else if (to === 0 && typeof response === 'function') response(store)
}
var end = function (data) {
if (typeof response === 'function') response(data)
}
setTimeout(function () {
callback(item, index, done, end)
}, 0)
}
if (array instanceof Array && array.length > 0) {
if (limit > array.length) limit = array.length
for (var i = 0; i < limit; i++) {
async(array[i], i)
it++
}
} else if (typeof response === 'function') response(store)
},
times: function (fin, callback, end) {
snc.forSync(0, fin - 1, 1, callback, end)
},
parallel: function (callbacks, response) {
var it = 0
var store = []
var async = function (ix) {
var done = function (data) {
if (data !== null && data !== undefined) store[ix] = data
if (it < callbacks.length - 1) it++
else {
if (typeof response === 'function') response(store)
}
}
setTimeout(function () {
callbacks[ix](done)
}, 0)
}
if (callbacks instanceof Array) {
for (var i = 0; i < callbacks.length; i++) {
async(i)
}
}
},
all: function (array, callback, response) {
var it = 0
var store = []
var async = function (index) {
var done = function (data) {
it++
if (data !== null && data !== undefined) store[index] = data
if (it === array.length && typeof response === 'function') response(store)
}
setTimeout(function () {
callback(array[index], index, done)
}, 0)
}
if (array instanceof Array && array.length > 0) {
for (var i = 0; i < array.length; i++) {
async(i)
}
} else if (typeof response === 'function') response(store)
},
foreverParallel: function (limit, callback, response) {
var counter = 0
var endcounter = 0
var end = function (data) {
endcounter++
if (typeof response === 'function' && endcounter === limit) response(data)
}
var done = function () {
setTimeout(function () {
counter++
callback(counter, done, end)
}, 0)
}
for (var i = 0; i < limit; i++) {
done()
}
},
now: function (operation, response) {
var then = function (data) {
if (typeof response === 'function') response(data)
}
operation(then)
},
interval: function (time, operation, response) {
var inter = null
var end = function (data) {
clearInterval(inter)
if (typeof response === 'function') response(data)
}
inter = setInterval(function () {
operation(end)
}, time)
}
}
snc.eachpl = snc.eachParallelLimit
snc.epl = snc.eachParallelLimit
snc.wf = snc.waterfall
snc.pl = snc.parallelLimit
snc.fe = snc.forever
snc.for = snc.forSync
snc.p = snc.parallel
snc.map = snc.all
snc.fp = snc.foreverParallel
if (typeof process === 'object') module.exports = snc