angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Feat: Run angular in web worker mode

Open k3nsei opened this issue 10 years ago • 65 comments

In current setup there is no possibility to bootstrap angular in web worker mode. Please add this killer functionality.

k3nsei avatar Sep 23 '16 09:09 k3nsei

I came across web workers recently and early thoughts went into the direction of @angular/platform-browser:

  • abstract the DOM APIs away,
  • thus, code would run in a worker...

I just flipped the idea the other way around:

  • DONT ABSTRACT the DOM APIs away,
  • Extract the supposed to run in a worker-code,
  • Wrap it in a if no worker-fallback.

You would end up with a style of code like this:

// All worker-compatible code goes to @my/worker
import { WorkerFacade, workerCompatibleCode } from '@my/worker';

// WorkerFacade would implement the "if no worker" fallback and messaging from UI to worker
const myWorker: WorkerFacade = /* ... */

const runnableInWorker = myWorker
  // You would need to map messages to function calls in the worker (not shown here)
  .postToWorker({ run: 'workerCompatibleCode', data: { foo: 'bar' } })
  // workerCompatibleCode() would be run in the UI were web worker isn't available
  .orElse(() => workerCompatibleCode());

runnableInWorker.subscribe(result=> {
 console.log("result was returned either from a web worker or from UI process");
});

dherges avatar Mar 06 '20 16:03 dherges

Running Angular inside a web worker has been deprecated for a while now, as it just isn't typically beneficial for performance: https://angular.io/guide/deprecations#webworker-apps. As a result, the CLI doesn't support this use case anymore.

Applications can still add web workers to run their own CPU-intensive tasks, just not running Angular itself. See https://angular.io/guide/web-worker for more on that.

dgp1130 avatar Jun 24 '20 17:06 dgp1130

I'd really like to use angular's dependency injection system in my web workers. I have a bunch of heavily modularized Angular services that rely heavily on the Angular DI system. I'd like to use some of those services in a web worker (even if it meant instantiating a secondary service tree that lived on the web worker side.) Does anyone have any suggestions on how to replicate that kind of approach in a web worker? Ideally reusing the actual service code, if possible?

pshields avatar Jun 24 '20 18:06 pshields

@pshields I assume using Angular DI system directly into a webworker would be hard but I remember that Minko extracted the DI part into a separate project which is browser agnostic (can work on Node) :tada:

https://github.com/mgechev/injection-js Maybe this will help you

maxime1992 avatar Jun 25 '20 06:06 maxime1992

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.