Skip to content

Instantly share code, notes, and snippets.

@yuchenQ
Created April 28, 2021 05:29
Show Gist options
  • Save yuchenQ/535c0956cfb3dc06de73edffc298744f to your computer and use it in GitHub Desktop.
Save yuchenQ/535c0956cfb3dc06de73edffc298744f to your computer and use it in GitHub Desktop.
initWorker
const initWorker = (workerFn) => {
const iife = `(${workerFn.toString()})()`;
const blob = new Blob([iife], { type: 'text/javascript' });
const fileURL = URL.createObjectURL(blob);
const worker = new Worker(fileURL);
worker.cleanup = () => {
URL.revokeObjectURL(fileURL);
worker.terminate();
};
return worker;
};
export default initWorker;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment