-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel.min.js
178 lines (136 loc) · 5.25 KB
/
parallel.min.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
/*
* Library: parallel.js
* Author: Adam Savitzky
* License: Creative Commons 3.0
*/
(function (isNode, _) {
var Worker = isNode ? require('./worker') : window.Worker,
URL = isNode ? require('./url') : window.URL,
Blob = isNode ? require('./blob') : window.Blob;
var Parallel = (function () {
var _require = (function () {
var state = {
files: [],
funcs: []
};
var makeUrl = function (fileName) {
var link = document.createElement("link");
link.href = fileName;
return link.href;
};
var setter = function () {
var args = _.toArray(arguments);
state.funcs = _.filter(args, _.isFunction);
state.files = _.chain(args).filter(_.isString).map(makeUrl).value();
};
setter.state = state;
return setter;
})();
var spawn = (function () {
var wrapMain = function (fn) {
var op = fn.toString();
return isNode ?
'process.on("message", function (m) { process.send({ data : JSON.stringify((' + op + ').apply(process, JSON.parse(m))) }); });' :
'self.onmessage = function (e) { self.postMessage((' + op + ').apply(self, e.data)); };';
};
var wrapFiles = function (str) {
return isNode ?
(_require.state.files.length ? _.map(_require.state.files, function (f) { return 'require(' + f + ');'; }).join('') : '') + str :
(_require.state.files.length ? 'importScripts("' + _require.state.files.join('","') + '");' : '') + str;
};
var wrapFunctions = function (str) {
return str + (_require.state.funcs.length ? _.invoke(_require.state.funcs, 'toString').join(';') + ';' : '');
};
var wrap = _.compose(wrapFunctions, wrapFiles, wrapMain);
var RemoteRef = function (fn, args) {
try {
var str = wrap(fn),
blob = new Blob([str], { type: 'text/javascript' }),
url = URL.createObjectURL(blob),
worker = new Worker(url);
worker.onmessage = _.bind(this.onWorkerMsg, this);
this.worker = worker;
this.worker.ref = this;
if (isNode) {
this.worker.postMessage(JSON.stringify([].concat(args)));
} else {
this.worker.postMessage([].concat(args));
}
} catch (e) {
if (console && console.error) {
console.error(e);
}
this.onWorkerMsg({ data: fn.apply(window, args) });
}
};
RemoteRef.prototype.onWorkerMsg = function (e) {
if (isNode) {
this.data = JSON.parse(e.data);
this.worker.terminate();
} else {
this.data = e.data;
}
};
RemoteRef.prototype.data = undefined;
RemoteRef.prototype.fetch = function (cb) {
if (this.data === '___terminated') {
return;
}
return this.data ? (cb ? cb(this.data): this.data) : (setTimeout(_.bind(this.fetch, this, cb), 0) && undefined);
};
RemoteRef.prototype.terminate = function () {
this.data = '___terminated';
this.worker.terminate();
};
return function (fn, args) {
var r = new RemoteRef(fn, args);
return r;
};
})();
var mapreduce = (function () {
var DistributedProcess = function (mapper, reducer, chunks) {
this.mapper = mapper;
this.reducer = reducer;
this.chunks = chunks;
this.refs = _.map(chunks, function (chunk) {
return spawn(mapper, [].concat(chunk));
});
};
DistributedProcess.prototype.fetch = function (cb) {
var results = this.fetchRefs(),
that = this;
if (_.isEqual(results, _.without(results, undefined))) {
return cb ? cb(_.reduce(results, this.reducer)) : _.reduce(results, this.reducer);
}
setTimeout(function () {
that.fetch(cb);
}, 100);
};
DistributedProcess.prototype.fetchRefs = function (cb) {
return _.map(this.refs, function (ref) {
return ref.fetch(cb || undefined);
}, this);
};
DistributedProcess.prototype.terminate = function (n) {
n !== undefined ? this.refs[n].terminate() : _.invoke(this.refs, 'terminate');
};
return function (mapper, reducer, chunks, cb) {
var d = new DistributedProcess(mapper, reducer, chunks);
d.fetch(cb);
return d;
}
})();
return {
mapreduce: mapreduce,
spawn: spawn,
require: _require
};
})();
if (isNode) {
this.exports = Parallel;
} else {
this.Parallel = Parallel;
}
}).call(typeof module !== 'undefined' && module.exports ? module : window, // context
typeof module !== 'undefined' && module.exports, // isNode
typeof module !== 'undefined' && module.exports ? require('underscore') : _); // underscore