Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Worker in Angular Library

I'm developing an angular library that perform strong computation operations. So I was interested in implementing the web worker in order to have a better user experience and let the complex operations run on the background.

My problem is that when I run the command ng g web-worker with Angular CLI 8, the output is Web Worker requires a project type of "application". Does anyone has suggestion on how to proceed for implementing web worker in my library?

like image 387
Davide Bulbarelli Avatar asked Jul 17 '19 09:07

Davide Bulbarelli


People also ask

What is web worker in angular?

Web workers lets you run CPU-intensive computations in a background thread, freeing the main thread to update the user interface.

What is a web worker used for?

Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface.

Does RxJS use web workers?

RxJS also has a fromWorker that takes the URL to your Web Worker: github.com/Reactive-Extensions/RxJS-DOM/blob/master/doc/…

What is the difference between service worker and web worker?

What is a Service Worker? Unlike Web Workers, Service Workers have a dedicated role of being a proxy between the network and the browser and/or cache. Similar to Web Workers, they are registered in the primary JavaScript file as a dedicated worker.


1 Answers

Per se a library should just provide its users with functionalities. It is up to the developer to decide how to use them: in a web page main context, in a web worker or in a server side rendering pipeline. So there is no direct easy way to do what you ask. The correct way would be to create your libraries and then a separate project with for example

ng new --create-application false workername

and then configure webpack directly in order to obtain a minified bundle which uses your libraries. If you really want to bundle the webworker inside your library, you may try to inline it (see). It also will require you to mess with webpack in order to create the minified result and embed it as a Blob in your bundle.

Have a look at the following links, useful in both cases:

https://github.com/webpack-contrib/worker-loader

https://github.com/GoogleChromeLabs/worker-plugin

https://www.npmjs.com/package/webworkify-webpack?activeTab=readme

like image 110
Gianmaria Canossa Avatar answered Sep 27 '22 17:09

Gianmaria Canossa