Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when run in a WebWorker #1059

Closed
ligaz opened this issue Nov 17, 2016 · 7 comments
Closed

Exception when run in a WebWorker #1059

ligaz opened this issue Nov 17, 2016 · 7 comments

Comments

@ligaz
Copy link

ligaz commented Nov 17, 2016

Trying to execute Prism.highlight(...) in a WebWorker results in an error being throw from Prism.

It looks like the following code is the culprit. It assumes that if Prism is run in a Worker it will a be Worker created by itself using the async option and the message will have the Prism contract being a JSON with language, code and immediateClose fields.

@Golmote
Copy link
Contributor

Golmote commented Nov 19, 2016

Can you provide some example code to reproduce the issue? Prism should be able to be used in a Worker. I just tested the following code, that works:

    var worker = new Worker('prism.js');
    worker.onmessage = function(evt) {
        console.log(evt.data);
        // Displays <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span><span class="token punctuation">></span></span>
    };
    worker.postMessage(JSON.stringify({
        language: 'markup',
        code: '<span>'
    }));

@Rob--W
Copy link
Contributor

Rob--W commented Nov 20, 2016

@Golmote Look at the code linked from the report. It is quite obvious that it unconditionally handles any messages. That makes Prism.js in its current form unsuitable as a library in a Web worker (opposed to being the sole and only script running in the worker, as the code currently assumes).

I worked around the issue in my own project by temporarily clearing the addEventListener method because then prism.js will not attach a global message listener (ref in prism source): https://github.com/Rob--W/crxviewer/blob/ce1c9ec1bd5dc61f6d302ba835b76c16418d9ac0/src/lib/prettify/prism-source-extensions.js#L8-L14

Ideally there would be a global flag to disable this, e.g. _self.prismAsWorkerLib (if this is set, then Prism.js will not attach a message handler).

@Golmote
Copy link
Contributor

Golmote commented Nov 20, 2016

Could we add a public method to detach the message listener? Would this help?

@ligaz
Copy link
Author

ligaz commented Nov 21, 2016

Ideally the onmessage handler should be installed in case Prism.highlightElement is run with async option: https://github.com/PrismJS/prism/blob/gh-pages/components/prism-core.js#L212

@ligaz
Copy link
Author

ligaz commented Nov 21, 2016

You can also check the format of the incoming message and do nothing if it cannot be processed.

@Rob--W
Copy link
Contributor

Rob--W commented Nov 21, 2016

Ideally the onmessage handler should be installed in case Prism.highlightElement is run with async option: https://github.com/PrismJS/prism/blob/gh-pages/components/prism-core.js#L212

The onmessage handler must be registered in the worker, not in the main thread, so this does not work unless you plan on creating a dynamic worker script.

@ligaz
Copy link
Author

ligaz commented Nov 21, 2016

@Rob--W True that. There are ways to hack this around (like passing arguments via location) but they are not stable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants