Published
Edited
Jan 18, 2020
Fork of Web Worker
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
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();
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more