Skip to content

Commit d00a051

Browse files
committed
Provide a fallback if webworkers do not exist
Fixes #30.
1 parent 02b7092 commit d00a051

File tree

1 file changed

+51
-19
lines changed

1 file changed

+51
-19
lines changed

lib/parallel.js

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
var defaults = {
7070
ie10shim: !isNode,
7171
path: (isNode ? __dirname + '/' : 'lib/') + 'eval.js',
72-
maxWorkers: isNode ? require('os').cpus().length : 4
72+
maxWorkers: isNode ? require('os').cpus().length : 4,
73+
synchronous: true
7374
};
7475

7576
function Parallel(data, options) {
@@ -120,6 +121,10 @@
120121
wrk = new Worker(this.options.path);
121122
wrk.postMessage(src);
122123
} else {
124+
if (Worker === undefined) {
125+
return undefined;
126+
}
127+
123128
try {
124129
if (this.requiredScripts.length !== 0) {
125130
if (this.options.ie10shim) {
@@ -152,12 +157,21 @@
152157
var newOp = new Operation();
153158
this.operation.then(function () {
154159
var wrk = that._spawnWorker(cb);
155-
wrk.onmessage = function (msg) {
156-
wrk.terminate();
157-
that.data = msg.data;
158-
newOp.resolve(null, that.data);
159-
};
160-
wrk.postMessage(that.data);
160+
if (wrk !== undefined) {
161+
wrk.onmessage = function (msg) {
162+
wrk.terminate();
163+
that.data = msg.data;
164+
newOp.resolve(null, that.data);
165+
};
166+
wrk.postMessage(that.data);
167+
} else if (that.options.synchronous) {
168+
setImmediate(function () {
169+
that.data = cb(that.data);
170+
newOp.resolve(null, that.data);
171+
});
172+
} else {
173+
throw new Error('Workers do not exist and synchronous operation not allowed!');
174+
}
161175
});
162176
this.operation = newOp;
163177
return this;
@@ -166,12 +180,21 @@
166180
Parallel.prototype._spawnMapWorker = function (i, cb, done) {
167181
var that = this;
168182
var wrk = that._spawnWorker(cb);
169-
wrk.onmessage = function (msg) {
170-
wrk.terminate();
171-
that.data[i] = msg.data;
172-
done();
173-
};
174-
wrk.postMessage(that.data[i]);
183+
if (wrk !== undefined) {
184+
wrk.onmessage = function (msg) {
185+
wrk.terminate();
186+
that.data[i] = msg.data;
187+
done();
188+
};
189+
wrk.postMessage(that.data[i]);
190+
} else if (that.options.synchronous) {
191+
setImmediate(function () {
192+
that.data[i] = cb(that.data[i]);
193+
done();
194+
});
195+
} else {
196+
throw new Error('Workers do not exist and synchronous operation not allowed!');
197+
}
175198
};
176199

177200
Parallel.prototype.map = function (cb) {
@@ -203,12 +226,21 @@
203226
Parallel.prototype._spawnReduceWorker = function (data, cb, done) {
204227
var that = this;
205228
var wrk = that._spawnWorker(cb);
206-
wrk.onmessage = function (msg) {
207-
wrk.terminate();
208-
that.data[that.data.length] = msg.data;
209-
done();
210-
};
211-
wrk.postMessage(data);
229+
if (wrk !== undefined) {
230+
wrk.onmessage = function (msg) {
231+
wrk.terminate();
232+
that.data[that.data.length] = msg.data;
233+
done();
234+
};
235+
wrk.postMessage(data);
236+
} else if (that.options.synchronous) {
237+
setImmediate(function () {
238+
that.data[that.data.length] = cb(data);
239+
done();
240+
});
241+
} else {
242+
throw new Error('Workers do not exist and synchronous operation not allowed!');
243+
}
212244
};
213245

214246
Parallel.prototype.reduce = function (cb) {

0 commit comments

Comments
 (0)