Skip to content

Instantly share code, notes, and snippets.

@alekstar79
Created June 25, 2023 17:11
Show Gist options
  • Save alekstar79/042a211b62203f1439f2012ce6aa9f55 to your computer and use it in GitHub Desktop.
Save alekstar79/042a211b62203f1439f2012ce6aa9f55 to your computer and use it in GitHub Desktop.
/**
* @param {Function} job
* @return {function(*=): Promise<unknown>}
*/
export function workerInit(job)
{
const url = window.URL.createObjectURL(new Blob(
[`"use strict";\nself.onmessage = ${job.toString()}`],
{ type: 'text/javascript' }
))
return data => new Promise((resolve, reject) => {
const worker = new Worker(url, { type: 'module' })
worker.onerror = reject
worker.onmessage = e => {
resolve(e.data)
}
worker.postMessage(data)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment