Created
February 5, 2021 16:40
-
-
Save westwick/48b0f7169dc8f5fadd71dd53f7fa57f7 to your computer and use it in GitHub Desktop.
webworker example
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
// example.worker.js | |
onmessage = e => { | |
const data = JSON.parse(e.data); | |
switch (data.type) { | |
case 'init': | |
// set up data or something | |
break; | |
case 'filter': | |
// some long running task possibly | |
const result = doSomethingAsyncOrWhatever(...); | |
postMessage(result); | |
break; | |
} | |
} | |
// then in the component using it, following best practices from article | |
exampleWorker.postMessage(JSON.stringify({type: 'filter', message: 'whatever'}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment