{
function W(func) {
function job(func) {}
function workerMessageHandler(e, handler) {
console.log(`Worker: ${e}`);
postMessage(handler());
}
job.prototype.blob = function() {
const code = `
${func.toString()}
const handler = ${func.name}
${workerMessageHandler.toString()}
this.onmessage = (e) => {
workerMessageHandler(e.data, handler)
}
`;
return code;
};
job.prototype.worker = function() {
const blob = new Blob([this.blob()], { type: 'text/javascript' });
const url = URL.createObjectURL(blob);
this.w = new Worker(url);
return this.w;
};
return new job(func);
}
function sample() {
return `Worker`;
}
const job = W(sample);
const w = job.worker();
w.onmessage = m => {
console.log(`Main: [${m.data}]`);
};
w.postMessage('hello');
return job.blob();
}