Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular 12 – Check a service all the time, is it feasible?

I have a component "topbar.component.ts" where I want to call a service that is checking if I have a running process, if I have a running process the response will be the next property:

"isExecution": true

In this case, the aforementioned component shows me an icon of the running process but if:

"isExecution": false

It shows me a icon that allow me to launch again a process.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Summarizing, a service in a component that is checking every second, for example, if I have a running process to show one icon or another, if the value of the service’s response is false, it shows me an icon that allows me to launch the process again by clicking on it. but if the response is true it will be a processing icon and nothing can be done about the icon, only to wait the finish for to recover status of icon initial that to allow to lunch a new process.

NOTE: I’m not sure if it would be detrimental or bad practice to have a service constantly checking the status of a run. Since it would be interesting to only activate it when a process is launched, which is done from another component called "execution.component.ts" they are launched from this component, but from "topbar.component.ts" it is only checked if this is in action. This would be ideal, but if it’s feasible to have a service doing the checks, it would save me hassle.

What would be the best way to do this in Angular? Thanks

>Solution :

You can do it by simply setInterval function, this would run after every x milliseconds. But as you have asked the Angular way to do it then I would suggest to use interval from rxjs like so;

import { interval, Subscription } from 'rxjs';

subscription: Subscription;

...

//emit value in sequence every 10 second
const source = interval(10000);
const isExecution: Boolean = true;
this.subscription = source.subscribe(val => this.opensnack(isExecution));

...

ngOnDestroy() {
  this.subscription.unsubscribe();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading