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

WebWorker compatible common file(?) #203

Open
Vassi opened this issue May 9, 2021 · 1 comment
Open

WebWorker compatible common file(?) #203

Vassi opened this issue May 9, 2021 · 1 comment

Comments

@Vassi
Copy link

Vassi commented May 9, 2021

So this is out of left field,

I spent the morning kind of messing around with setting up a new overlay and I was wondering if it would be possible to run the log parsing part in a web worker and then just post the interesting, processed, messages to my overlay to do stuff with.

Unfortunately, common.js doesn't really work in a web-worker, for various reasons I imagine. For one, it poops whenever you try to use window which is not available in a worker, and for another I'm not sure if whatever magi-globals the plugin puts in the window exist in the worker space.

Websockets work great, though, so I managed to get it working by modifying the contents of common. It's not ideal since it relies on implementation details that could change so I wonder if we could get like a common.worker.js in dist (or some other way cooler solution)?

To get it to work I had to:

  1. Change wsUrl to not come from the location and instead be a param of connectWs
  2. Make an onmessage handler to initiate connectWs from the main process (the main process sends the wsUrl)
  3. Delete the 'else' part of the websocket/local statement so it always tries to use websocket.
  4. Change anything that assigned to window.variable into a const variable = declaration

Sorry if this is weird, it's not really an issue, it's a feature request so feel free to close it if it doesn't fit your scope! I would be willing to do a PR for the first version but I wanted to see if it's even interesting to you.

Thanks for making this amazing bit of tech.

@valarnin
Copy link

You're correct that window isn't available from the WebWorker's scope, however you can simulate the needed global-scope properties pretty easily for the purposes that common.js needs, something like this should be sufficient:

window = window || {};

You'd then make sure to run that before loading common.js or make it its own separate file that can be included/required depending on how you're doing things.

As for the location property, that exists within the scope of a webworker already, it just uses the webworker's location to begin with, so if you append ?OVERLAY_WS=... to the webworker's location when creating it, that should be sufficient to pass it through.

sandtechnology pushed a commit to sandtechnology/OverlayPlugin that referenced this issue May 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants