Skip to content

Instantly share code, notes, and snippets.

@kindziora
Last active November 18, 2016 00:55
Show Gist options
  • Save kindziora/140a3246d73af7e3045d772d323123d2 to your computer and use it in GitHub Desktop.
Save kindziora/140a3246d73af7e3045d772d323123d2 to your computer and use it in GitHub Desktop.
var wrapworker = function (_function, data) {
var wrapr = this;var wrapworker = function (_function, data) {
var wrapr = this;
var mtd = "self.onmessage = function() {postMessage(" + _function.toString().replace('(','du(') + "(" + JSON.stringify(data) + "));};";
blob = new Blob([mtd], { type: 'application/javascript' });
var work = new Worker(URL.createObjectURL(blob));
wrapr.ready = function (cb) { work.onmessage = cb; };
wrapr.error = function (cb) { work.onerror = cb; };
work.postMessage(true);
return wrapr;
};
var mtd = "self.onmessage = function() {postMessage(" + _function.toString().replace('(','du(') + "(" + JSON.stringify(data) + "));};";
blob = new Blob([mtd], { type: 'application/javascript' });
var work = new Worker(URL.createObjectURL(blob));
wrapr.ready = function (cb) { work.onmessage = cb; };
wrapr.error = function (cb) { work.onerror = cb; };
work.postMessage(true);
return wrapr;
};
/////////////////////////////////////////usage/////////////////////////////////
var extensiveMath = function (a) {
var e;
for (var i = 0; i < 100000; i++) {
for (var ah = 0; ah < 100040; ah++) {
e = i * Math.E / -i;
}
}
return e - a;
};
var core1 = wrapworker(extensiveMath, 1234567890)
.ready(function (result) {
console.log("core1 done", result);
});
var core2 = wrapworker(extensiveMath, 1234567890)
.ready(function (result) {
console.log("core2 done", result);
});
var core3 = wrapworker(extensiveMath, 1234567890)
.ready(function (result) {
console.log("core1 done", result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment