OverlayPlugin icon indicating copy to clipboard operation
OverlayPlugin copied to clipboard

WebWorker compatible common file(?)

Open Vassi opened this issue 3 years ago • 1 comments

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.

Vassi avatar May 09 '21 16:05 Vassi

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.

valarnin avatar May 19 '21 01:05 valarnin