英文:
Angular 12 - Check a service all the time, is it feasible?
问题
I have a component called "topbar.component.ts" where I want to call a service that checks if I have a running process. If there is a running process, the service will respond with the "isExecution" property set to true. In this case, the component displays an icon indicating the running process. However, if "isExecution" is false, it shows an icon that allows me to launch a new process.
To summarize, I need a service in the component that constantly checks if there's a running process and displays different icons based on the service's response. If the response is false, it allows me to initiate a new process by clicking the icon. If the response is true, it shows a processing icon, and I have to wait for the process to finish to revert to the initial icon that allows launching a new process.
Please note that it might not be ideal to have a service constantly checking the process status. It would be better to activate this check only when a process is launched, as opposed to continuously polling. This can be achieved by triggering the check from another component called "execution.component.ts" when processes are launched from there, and "topbar.component.ts" can check if a process is in action.
The best way to implement this in Angular would depend on your specific use case and requirements. You may consider using observables and event handling to trigger and respond to process status changes.
英文:
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.
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
答案1
得分: 1
你可以简单地使用 setInterval 函数来实现,这会在每个 x 毫秒后运行。但是,如果你想要以 Angular
的方式实现,我建议使用 rxjs
中的 interval,代码如下:
import { interval, Subscription } from 'rxjs';
subscription: Subscription;
...
// 每隔 10 秒发出一个值
const source = interval(10000);
const isExecution: Boolean = true;
this.subscription = source.subscribe(val => this.opensnack(isExecution));
...
ngOnDestroy() {
this.subscription.unsubscribe();
}
英文:
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论