[英]Implement pooling with rxjs - wait for correct response and do it with timeout and delay
在我的情况下,我必须实施池化,但我在正确实施我的所有条件时遇到了一些问题。
所以首先,我必须调用一个端点,在它返回成功后,调用另一个端点,直到它返回正确的响应(它总是返回成功/200/,但对我来说最重要的是响应,所以如果响应将是 {state : 'ready'} 或者如果时间过去了(20 秒),我应该停止调用 api。
executeTest$(testCode: string, name: string): Observable<Test> {
let requestDelay = 500;
return this.testService.start({
body: {
code: {value: testCode},
name
}
}).pipe(
switchMap(body => {
return this.testStatus(body.name).pipe(
delay(500),
// and here I have problem to implement logic:
repeat this http until condition is met, so until response will be {state: 'ready'}
I see that repeat when is deprecated and retry when is suitable for errors.
timeout(20000)
);
})
);
}
private testStatus(testId: string): Observable<Test> {
return this.http.get(apiUrl)
}
您必须将内部可观察到的switchMap
更改为下面。 takeUntil
和repeat
应该在这里有所帮助。
switchMap(body => {
const subject: Subject<boolean> = new Subject();
const completeSubject = () => {
subject.next()
subject.complete();
}
return this.testStatus(body.name).pipe(
delay(500),
timeout(20000),
tap(res => {
if (res.status === ready) {
completeSubject();
}
}),
takeUntil(subject)
repeat(),
finalize(() => completeSubject())
);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.