question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disable automatically created WebWorker message handler

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
tvooocommented, Mar 22, 2018

I managed to achieve this the following way:

global.Prism = { disableWorkerMessageHandler: true };
const Prism = require('prismjs');

Then, I load the worker using webpack’s worker-loader like this:

const Worker = require('worker-loader?inline=true!./worker');

Hope that helps 😃

EDIT: It’s not necessary to import Prism as PrismLib or something else. Prism works just fine.

0reactions
jakearchibaldcommented, Sep 20, 2021

If you need to use imports, you can do this:

main.js

import './prism-config.js';
import Prism from 'prismjs';

// Use prism

./prism-config.js

self.Prism = { disableWorkerMessageHandler: true };
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found