wokwi / avr8js

Arduino (8-bit AVR) simulator, written in JavaScript and runs in the browser / Node.js
https://blog.wokwi.com/avr8js-simulate-arduino-in-javascript/
MIT License
481 stars 78 forks source link

Emittery emitter #53

Closed elliottkember closed 4 years ago

elliottkember commented 4 years ago

I noticed that when using a Web Worker, I couldn't use window.postMessage because the web worker doesn't have access to the window object. Here's a version using the emittery library.

urish commented 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?

elliottkember commented 4 years ago

@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!

urish commented 4 years ago

So self. didn't work inside the web worker?

elliottkember commented 4 years ago

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 ;)

urish commented 4 years ago

Perhaps it's worth then to have a separate repo with a complete example of how to do the web worker setup?

urish commented 4 years ago

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.

urish commented 3 years ago

Update: I've reimplemented TaskScheduler and it should now also support running in Web Worker context!

commit: 0ce082e4d621bef5083f05cebaeba8b06e4eac2b