Last active
June 8, 2017 12:30
-
-
Save jeromeetienne/2221b0e273f0dd78035c4c0431ff7915 to your computer and use it in GitHub Desktop.
Webworkers pool - Minimal standalone implementation
This file contains 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
self.addEventListener('message', function(event) { | |
console.log('in worker - received event', event) | |
self.postMessage('started') | |
}, false); |
This file contains 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
<body><script> | |
var workerURL = 'worker-pool-worker.js' | |
// create all workers | |
var workers = [] | |
workers.push( new Worker(workerURL) ) | |
workers.push( new Worker(workerURL) ) | |
workers.push( new Worker(workerURL) ) | |
workers.push( new Worker(workerURL) ) | |
var nWorkers = workers.length | |
var worker = workers[1] | |
worker.postMessage('start') | |
worker.onmessage = function(event) { | |
console.log('in mainthread - received event', event) | |
if( event.data === 'started' ){ | |
console.log('in mainthread - worker started', event) | |
} | |
} | |
</script></body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment