Disable automatically created WebWorker message handler
See original GitHub issueI have a web worker which looks like this:
import Prism from 'prismjs';
self.addEventListener('message', function(event) {
let { language, code } = event.data;
// Do some stuff
code = Prism.highlight(code, Prism.languages[language]);
self.postMessage(code);
});
It works, however the mere act of opening Prism inside a web worker causes it to create it’s own “message” handler. One which expects a String
as input. So I’m constantly getting errors in my console like:
JSON.parse: unexpected character
I could just use the automatically created “message” handler, but that prevents me from doing the extra processing that I wanted to happen in my web worker.
Is there a way of disabling the automatic creation of this message handler? I can see multiple references to disableWorkerMessageHandler
in the code, but I can’t figure out a way of leveraging it. Maybe that’s not even possible?
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
event auto unbind when Web worker terminate - Stack Overflow
No, the message event is still bound to the worker object. You can remove the event using the removeEventListener() property of the worker ......
Read more >Introducing WebSockets - Bringing Sockets to the Web
When postMessage() is called from the main page, our worker handles that message by defining an onmessage handler for the message event. The ......
Read more >Using Web Workers - Web APIs - MDN Web Docs
Once created, a worker can send messages to the JavaScript code that ... when the message is received by writing an event handler...
Read more >Introduction to Web Workers in JavaScript - Listen and Send ...
The Worker.onmessage event handler lets you listen for messages between the threads. The signature of this Worker event handler property is as ...
Read more >Web workers, React, and TypeScript - LogRocket Blog
If set up correctly, a web worker can send and receive messages to the ... Using the count web worker in TypeScript; Creating...
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 Free
Top 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
I managed to achieve this the following way:
Then, I load the worker using webpack’s
worker-loader
like this:Hope that helps 😃
EDIT: It’s not necessary to import Prism as
PrismLib
or something else.Prism
works just fine.If you need to use imports, you can do this:
main.js
./prism-config.js