Created
June 25, 2023 17:11
-
-
Save alekstar79/042a211b62203f1439f2012ce6aa9f55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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