-
Notifications
You must be signed in to change notification settings - Fork 27
/
parallel.js
52 lines (46 loc) · 1.2 KB
/
parallel.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
/**
* Parallel resource loader
*
* Flag "parallel"
*
* This plugin provides private "parallel" function
*/
/**
* @name sandbox
*/
(function (sb) {
function parallel(method, items, callback) {
var i = 0,
j = 0,
c = items.length,
results = [],
ready;
var readyFactory = function (index) {
return function (data) {
// keep the order
results[index] = data;
j++;
if (j >= c) {
callback.apply(sb.global, results);
}
}
};
for (; i < c; i++) {
ready = readyFactory(i);
method(items[i], ready)/*if ($P.PROMISE) {*/.then(ready, ready)/*}*/;
}
}
/**
* @event *:request-parallel parallel module request for require.async(['a', 'b', 'c']) etc
*
* @param {Array} moduleNames list of modules to init
* @param {Function} callback this callback will be called when module inited
* @param {Function} method method to call for init
*
* @retuns yes empty environment
*/
sb.on('*:request-parallel', function (moduleNames, callback, method) {
parallel(method, moduleNames, callback);
return [];
});
}(sandbox));