Can't pass callback to worker functions
See original GitHub issueThe docs are saying that we can pass functions between worker and main thread, but looks like this is only true on one side. I can pass a callback from Worker to Main Thread but I can not pass it from Main Thread to Worker.
Here is the code I used to test (disregard the timeout. I used it only to prevent race conditions)
const testScript = `
window.analytics = [];
window.analytics.track = (title, cb) => {
console.log('Logging analytics.track arguments', {title, cb});
cb();
};
window.addEventListener('click', () => {
console.log('Worker > Main Thread callback')
});
`;
<script
dangerouslySetInnerHTML={{
__html: `
setTimeout(() => {
window.analytics.track('My event', () => {
console.log('Main Thread > Worker callback')
});
}, 3000);
partytown = {
forward: ['analytics.track'],
};
`,
}}
/>
<script type="text/partytown" dangerouslySetInnerHTML={{__html: testScript}}/>
And this are the logs I see in the browser after reloading the page and clicking
As we can see the Main Thread > Worker callback never shows and we can see that the callback is an empty function inside the worker.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Is it possible to pass a callback to a webworker?
So No. You can't pass functions about. My issue is that I send the worker a message to start the rendering at the...
Read more >How to pass function to Web Workers - DEV Community
Passing serialized functions around may be not secure. It's better to define all needed functions inside the worker. ... I generally agree.
Read more >JavaScript Callbacks Variable Scope Problem - Pluralsight
In this free JavaScript guide, you'll learn how to fix the common JavaScript callbacks variable scope problem. This step-by-step JavaScript ...
Read more >JavaScript | Passing parameters to a callback function
Callback Function : Passing a function to another function or passing a function inside another function is known as a Callback Function.
Read more >Hooks FAQ - React
Hooks won't work if you forget to update, for example, React DOM. ... If computing the initial state is expensive, you can pass...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Very cool. If you’re game to take a stab at it and send a PR that passes build and adds an additional test for this, that would be great
I need something like this.