gpt4 book ai didi

angular - Effect 中的可取消 NGRX 服务轮询

转载 作者:行者123 更新时间:2023-12-03 21:25:06 24 4
gpt4 key购买 nike

如何轮询具有停止轮询直到服务的返回值满足条件或总持续时间超过超时阈值的效果的服务?

例如:后端系统正在生成资源,前端应用程序可以通过调用返回 bool 值的 REST api 调用来检查该资源是否可用。在我的 NGRX 应用程序中,我想每 200 毫秒轮询一次此 api 调用,直到此 api 调用返回 bool 值 true 或总轮询持续时间超过 10000 毫秒的阈值。

以下代码示例显示了一种轮询机制,但是,该轮询无法取消,也没有超时。这是怎么做的?

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/timer';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';

@Effect()
pollEffect$: Observable<Action> = this.actions$
.ofType(tasksActions.ActionTypes.START_POLLING)
.switchMap(() => Observable.timer(0, 200)
.switchMap(() => this.myBackendService.getAvailability().map(response =>
return taskActions.ActionTypes.UpdateValue(response))
)
);

最佳答案

有完全相同的问题,我无法从互联网上找到答案。我花了一些时间让它工作。这是我所做的。我注意到我把 'takeUntil(this.pollingUntil$)' 放在哪里很重要,我把它放在最上面,因为我需要用最新的数据更新商店。如果我把它放在底部(注释行),则轮询被取消,我无法用最新数据更新商店。

private readonly pollingIntervalMs = 5000;
private readonly maxPollingMs = 600000; // 10 sec

private pollingUntil$: Subject<boolean> = new Subject<boolean>();

@Effect()
pollDb$: Observable<Action> = this.actions$.pipe(
ofType(infraActions.InfraActionTypes.PollDb),
switchMap(pollAction => interval(this.pollingIntervalMs).pipe(
takeUntil(timer(this.maxPollingMs)),
takeUntil(this.pollingUntil$),
mapTo(pollAction),
switchMap(
(action: infraActions.PollDb) => this.dbService.get().pipe(
map((dbInfo: DbInfo) => {
if (meet condition) {
this.pollingUntil$.next(true);
}
return dbInfo;
})
)
),
// takeUntil(this.pollingUntil$),
map((dbInfo: DbInfo) => {
return new infraActions.PollDbSuccess(dbInfo);
}),
catchError(err => of(new infraActions.LoadDbFail(err)))
)),
);

关于angular - Effect 中的可取消 NGRX 服务轮询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48151924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com