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?
Web workers lets you run CPU-intensive computations in a background thread, freeing the main thread to update the user interface.
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.
RxJS also has a fromWorker that takes the URL to your Web Worker: github.com/Reactive-Extensions/RxJS-DOM/blob/master/doc/…
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With