Skip to content

Instantly share code, notes, and snippets.

@shanewholloway
Created July 12, 2018 18:08
Show Gist options
  • Save shanewholloway/0f8a56b3abb9aefc9a9ee38f2067e309 to your computer and use it in GitHub Desktop.
Save shanewholloway/0f8a56b3abb9aefc9a9ee38f2067e309 to your computer and use it in GitHub Desktop.
export default asWorkerFunction
export asWorkerFunction
export asBlobURLFunction
function asWorkerFunction(func) {
return new Worker(asBlobURLFunction(func))
}
function asBlobURLFunction(func) {
const rx_src = /(^.*=>|{)\s*([^]*?)(}\s*)?$/
const f_src = rx_src.exec(func)
const blob = new Blob([f_src[2]])
return URL.createObjectURL(blob)
}
import asWorkerFunction from './asWorkerFunction.js'
asWorkerFunction(() =>
self.postMessage({
hello: Date.now(),
loc: location.toString(),
loc_hash: location.hash })
).addEventListener('message', evt=>console.log('worker msg', evt.data), false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment