Closed elliottkember closed 4 years ago
Thank you Elliot!
How about simply replacing window.
with self.
? I think it should do the trick and work both on browser and inside a Web worker, without introducing a new 3rd party dependency?
@urish I tried that, but I couldn't figure out how to get it working. It seemed to have a different syntax between window
and WorkerGlobalScope
. I also tried using a basic EventEmitter and couldn't make that work either. I tried for an hour or so, and ended up using this library, which worked extremely well. Performance is hugely improved by using a web worker!
So self.
didn't work inside the web worker?
Not the way I was using it. I was also trying to keep it usable between both web worker and browser context. I'm sorry I didn't take any notes as to why, I just gave up in frustration and turned to this library which worked perfectly ;)
Perhaps it's worth then to have a separate repo with a complete example of how to do the web worker setup?
Closing for now, as the goal of the demo project in this repo to be minimal. That being said, I'd love seeing a repo with an example project that utilizes a web worker.
Update: I've reimplemented TaskScheduler and it should now also support running in Web Worker context!
commit: 0ce082e4d621bef5083f05cebaeba8b06e4eac2b
I noticed that when using a Web Worker, I couldn't use
window.postMessage
because the web worker doesn't have access to thewindow
object. Here's a version using theemittery
library.