-
Notifications
You must be signed in to change notification settings - Fork 0
/
webmp.js
134 lines (103 loc) · 3.16 KB
/
webmp.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
(function(global) {
var fnRegex = new RegExp('^function.*?{([\\s\\S]*)}$');
var extractFunction = function(fn) {
return fnRegex.exec(fn.toString())[1];
};
var sendMessage = function(worker, type, content) {
worker.postMessage({
type: type,
content: content
});
};
var runFunction = function(worker, context, fn, responseHandler) {
var eventHandler = function(event) {
switch (event.data.type) {
case 'response':
responseHandler(event.data.content, stop);
break;
case 'log':
console.log(event.data.content);
break;
}
};
var stop = function() {
worker.removeEventListener('message', eventHandler);
};
sendMessage(worker, 'script', fn);
worker.addEventListener('message', eventHandler);
sendMessage(worker, 'eval', context);
};
var splitList = function(list, count) {
var splits = [];
var size = Math.ceil(list.length / count);
for (i = 0; i < count; i++) {
splits[i] = list.slice(i * size, (i + 1) * size);
}
return splits;
};
var WebMP = function(count, options) {
this.count = count;
this.pool = [];
this.name = options.name;
this.libURL = options.libURL || 'webmp.js';
this.workerURL = options.workerURL || 'webmp_worker.js';
for (i = 0; i < count; i++) {
this.pool[i] = new Worker(this.workerURL);
sendMessage(this.pool[i], 'init', {
id: i,
name: this.name,
count: this.count,
libURL: this.libURL
});
}
};
WebMP.prototype.forEach = function() {
var self = this;
var args = Array.prototype.slice.call(arguments);
var cb = args.pop();
var fn = args.pop();
var list = args.pop();
var context = args.pop() || {};
var iterFn = ['webMP._list.forEach(function(_listElem) {',
' webMP.elem = _listElem;',
extractFunction(fn),
'});'
].join('\n');
var splits = splitList(list, this.count);
var returned = 0;
var responses = [];
this.pool.forEach(function(worker, id) {
var iterReturned = splits[id].length;
var iterResponses = [];
context._list = splits[id];
runFunction(worker, context, iterFn, function(response, stop) {
iterReturned--;
iterResponses.push(response);
if (iterReturned === 0) {
stop();
returned++;
responses[id] = iterResponses;
if (returned == self.count) cb([].concat.apply([], responses));
}
});
});
};
WebMP.prototype.parallel = function() {
var self = this;
var args = Array.prototype.slice.call(arguments);
var cb = args.pop();
var fn = args.pop();
var context = args.pop() || {};
var returned = 0;
var responses = [];
this.pool.forEach(function(worker, id) {
runFunction(worker, context, extractFunction(fn), function(response, stop) {
stop();
returned++;
responses[id] = response;
if (returned == self.count) cb(responses);
});
});
};
global.WebMP = WebMP;
})(this);