Skip to content

Commit e063e2e

Browse files
committed
Catch thrown errors and fall back to a non-webworker solution.
This fixes parallel-js#8 and (for now) parallel-js#11, although the fix for parallel-js#11 should actually be a wrapper script.
1 parent 32e0027 commit e063e2e

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

parallel.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,34 @@ var Parallel = (function () {
6060

6161
var wrap = _.compose(wrapFunctions, wrapFiles, wrapMain);
6262

63-
var RemoteRef = function (fn) {
64-
var str = wrap(fn),
65-
blob = new Blob([str], { type: 'text/javascript' }),
66-
url = URL.createObjectURL(blob),
67-
worker = new Worker(url);
68-
69-
worker.onmessage = _.bind(this.onWorkerMsg, worker);
70-
71-
this.worker = worker;
72-
this.worker.ref = this;
63+
var RemoteRef = function (fn, args) {
64+
try {
65+
var str = wrap(fn),
66+
blob = new Blob([str], { type: 'text/javascript' }),
67+
url = URL.createObjectURL(blob),
68+
worker = new Worker(url);
69+
70+
worker.onmessage = _.bind(this.onWorkerMsg, this);
71+
72+
this.worker = worker;
73+
this.worker.ref = this;
74+
75+
if (isNode) {
76+
this.worker.postMessage(JSON.stringify([].concat(args)));
77+
} else {
78+
this.worker.postMessage([].concat(args));
79+
}
80+
} catch (e) {
81+
this.onWorkerMsg({data: fn.apply(window, args)});
82+
}
7383
};
7484

7585
RemoteRef.prototype.onWorkerMsg = function (e) {
7686
if (isNode) {
77-
this.ref.data = JSON.parse(e.data);
78-
this.ref.worker.terminate();
87+
this.data = JSON.parse(e.data);
88+
this.worker.terminate();
7989
} else {
80-
this.ref.data = e.data;
90+
this.data = e.data;
8191
}
8292
};
8393

@@ -97,12 +107,7 @@ var Parallel = (function () {
97107
};
98108

99109
return function (fn, args) {
100-
var r = new RemoteRef(fn);
101-
if (isNode) {
102-
r.worker.postMessage(JSON.stringify([].concat(args)));
103-
} else {
104-
r.worker.postMessage([].concat(args));
105-
}
110+
var r = new RemoteRef(fn, args);
106111

107112
return r;
108113
};

0 commit comments

Comments
 (0)