Skip to content

Instantly share code, notes, and snippets.

@simon300000
Created September 14, 2019 00:41
Show Gist options
  • Save simon300000/6e5d1e6bcd254d5c16adf4efa2a82e65 to your computer and use it in GitHub Desktop.
Save simon300000/6e5d1e6bcd254d5c16adf4efa2a82e65 to your computer and use it in GitHub Desktop.
make function a WebWorker
// 刚刚说的感觉很有意思
// 我做了个Demo2333
// 不过只能弄很简单的Function( 可见Functional是未来( 误)
const testFunction = num => num * 2
const slowFunction = time => {
const now = Date.now()
let round = 0
while (now + time > Date.now()) {
round++
}
return round
}
const makeWorker = f => {
let pendingJobs = {}
const worker = new Worker(URL.createObjectURL(new Blob([
`onmessage = ({ data: { jobId, params } }) => {
const result = (${f.toString()})(...params)
postMessage({ jobId, result })
}`
])))
worker.onmessage = ({ data: { result, jobId } }) => {
pendingJobs[jobId](result)
delete pendingJobs[jobId]
}
return (...params) => new Promise(resolve => {
const jobId = String(Math.random())
pendingJobs[jobId] = resolve
worker.postMessage({ jobId, params })
})
}
const testWorker = makeWorker(testFunction)
const slowWorker = makeWorker(slowFunction)
testWorker(122).then(console.log)
slowWorker(1000).then(runs => console.log(`Runs: ${runs}`))
testWorker(233).then(console.log)
slowWorker(1000).then(runs => console.log(`Runs: ${runs}`))
testWorker(233).then(console.log)
@simon300000
Copy link
Author

paste in console
(GitHub does not work because of the secure policy)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment