Ionic/Angular and Web Workers

I’m torn on whether to use web workers, and currently coming down on the side of No, but I’m curious what others think. Here’s my current position.

  1. Angular style recommends all heavy lifting appear in a provider, not a page.

  2. If you know a provider needs time to think, your page can set its values to initial placeholder values that get updated via Observable when the provider finishes thinking.

  3. Using a web worker, by contrast, encourages the programmer to put heavy code in the page itself, and discourages the use of Observables and reactive solutions.

Thoughts? Anyone used web workers in a provider? Any benefit to this?

I currently rely on (1), and it means my page displays almost immediately, and values arrive when they are available. That seems like a better user experience than waiting for a web worker, even if a web worker executes faster than the same code not in a web worker.

Edit to add: I started a thread on this topic on the Angular repo.

Isn’t the main advantage of a Web Worker that it runs in a background thread, so you can do things you normally wouldn’t do on the main thread in fear of blocking UI? What exactly are you thinking about using the Web Worker for?

I think the main reason why there are no best practices yet for Web Workers and Angular (and Ionic) is that they just weren’t a (stable) thing until recently.

That’s my understanding too. And I have providers with a lot of pure functions, some very simple, but some like “sort this array and make a new copy of it” that seem like natural candidates for web workers. So if it makes sense to use a web worker inside a provider that gets injected into a page, then I’ll probably do it. But I don’t understand enough about Angular’s DI to understand if there’s a benefit. I already have no heavy logic in my pages – it’s all being injected and listened to using things like BehaviorSubject.take(1).asObservable.

I just noticed:

Features include only loading images which are visible, using web workers for HTTP requests, preventing jank while scrolling and in-memory caching.

https://ionicframework.com/docs/api/components/img/Img/

Maybe this concept is already part of Ionic in more places?

1 Like

Unfortunately not, at least not if we believe the docs: https://www.google.de/search?q=site%3Aionicframework.com%2Fdocs+“web+workers”