scala-js / scala-js-macrotask-executor

Apache License 2.0
33 stars 7 forks source link

Behavior in browser extensions #38

Open AlexITC opened 2 years ago

AlexITC commented 2 years ago

I maintain a plugin and a template to create browser extensions with Scala.js.

I'm interested to push the macrotask-executor to these repos but I still don't understand the differences.

For example, in a browser extension, there are different contexts (like different applications composing the browser extension), some of them being:

The UI contexts behave just like any normal web page, hence, the macrotask-executor should be useful, on the other hand, I wonder if the macrotask executor is useful for the background context.

Thanks.

armanbilge commented 2 years ago

Thanks for raising this issue! Firstly, the MacrotaskExecutor should work in any JS runtime. If not, that's a bug :)

So the question at hand is what is the performance of MacrotaskExecutor when used in a browser extension. This depends on what APIs are available: so long as one of setImmediate, postMessage, or MessageChannel (for webworkers) are available, it should have optimal performance. If none of these are available, it falls back to setTimeout which is always available but much less performant.

From your description it sounds like the UI contexts provide APIs available in webpages and the background contexts provide APIs usually available in webworkers. The MacrotaskExecutor is already known to work well in those environments.

Lastly, do you have an easy way of testing browser extensions in CI? It would be a good addition to our test matrix and confirm everything is working as expected. We currently run tests in Node.js, JSDOM, and Chrome/Firefox webpages and webworkers.

AlexITC commented 2 years ago

Thanks for raising this issue! Firstly, the MacrotaskExecutor should work in any JS runtime. If not, that's a bug :)

Yes, it does.

it falls back to setTimeout which is always available but much less performant.

This is what I don't know, the background context might end up using this approach.

From your description it sounds like the UI contexts provide APIs available in webpages and the background contexts provide APIs usually available in webworkers. The MacrotaskExecutor is already known to work well in those environments.

Correct, the background context has differences from webworkers, I'm yet to investigate details about this.

Lastly, do you have an easy way of testing browser extensions in CI? It would be a good addition to our test matrix and confirm everything is working as expected. We currently run tests in Node.js, JSDOM, and Chrome/Firefox webpages and webworkers.

Yes, it is doable with Selenium, I'm involved in a private extension where we use that.

armanbilge commented 2 years ago

Thanks for the response!

Yes, it is doable with Selenium, I'm involved in a private extension where we use that.

Fantastic, do you think you could make a PR setting this up? That would be the best way to test this, and make sure we don't have any regressions for this environment in the future. Our test suite actually includes a test to verify one of the higher-performance polyfills is used. https://github.com/scala-js/scala-js-macrotask-executor/blob/7fbe5164500ed7a2ecaf3999be670e4a7a2cf1cb/core/src/test/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuite.scala#L43

AlexITC commented 2 years ago

I do remember the test, unfortunately I don't have too much free time right now but I can get someone from my team to add it, don't expected it too soon.