Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IE10 WebWorkers #16

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions eval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
self.onmessage = function (e) {
eval(e.data);
};
34 changes: 23 additions & 11 deletions parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,29 @@ var Parallel = (function () {

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

var RemoteRef = function (fn, args) {
var RemoteRef = function (fn, args, evaljs) {
var str, blob, url, worker;
try {
var str = wrap(fn),
blob = new Blob([str], { type: 'text/javascript' }),
url = URL.createObjectURL(blob),
str = wrap(fn),
blob = new Blob([str], { type: 'text/javascript' }),
url = URL.createObjectURL(blob);

try {
worker = new Worker(url);

} catch (e) {
if (e.code === 18 && evaljs) { // cross-origin error
worker = new Worker(evaljs);
worker.postMessage(str);
} else {
throw e;
}
}

worker.onmessage = _.bind(this.onWorkerMsg, this);

this.worker = worker;
this.worker.ref = this;

if (isNode) {
this.worker.postMessage(JSON.stringify([].concat(args)));
} else {
Expand All @@ -81,8 +92,7 @@ var Parallel = (function () {
if (console && console.error) {
console.error(e);
}

this.onWorkerMsg({ data: fn.apply(window, args) });
this.onWorkerMsg({data: fn.apply(window, args)});
}
};

Expand Down Expand Up @@ -110,8 +120,10 @@ var Parallel = (function () {
this.worker.terminate();
};

return function (fn, args) {
var r = new RemoteRef(fn, args);
return function (fn, args, evaljs) {
if (!evaljs) evaljs = '/eval.js';

var r = new RemoteRef(fn, args, evaljs);

return r;
};
Expand Down