【发布时间】:2018-08-01 18:56:08
【问题描述】:
我使用的 API 只返回有限数量的结果,比如 100 个。
我想重复查询,直到返回结果集为 < 100,这意味着我已经得到了最后一个结果。
所以它会是这样的:
- 查询
- 结果集是否小于限制?如果是这样,请再次执行并附加结果。
- 一旦结果集小于限制,就发出最终结果。
【问题讨论】:
我使用的 API 只返回有限数量的结果,比如 100 个。
我想重复查询,直到返回结果集为 < 100,这意味着我已经得到了最后一个结果。
所以它会是这样的:
【问题讨论】:
我们还可以使用repeatWhen 运算符(类似于retryWhen,但在完成而不是错误时起作用):
this.queryData().pipe(
repeatWhen(obs => obs),
filter(data => this.checkLimit()),
take(1)
).subscribe(result => console.log(result));
注意:take(1) 是停止 repeatWhen 循环所必需的。
如果我们需要重试之间的延迟,我们可以这样做:
repeatWhen(obs => obs.pipe(delay(1000)))
【讨论】:
find(checkLimit)。和filter()和take(1)加在一起基本一样。
stop$: Subject = new Subject();
query$.pipe(takeUntil(this.stop$)).subscribe( result => {
if(result < limit)
this.stop$.next();
this.stop$.complete();
}
else {
process result, i.e append somewhere...
}
});
注意他是 RxJs 6 语法。为了便于阅读,您可以在方法中提取订阅逻辑。
在这种情况下,使用 takeWhile 可能更容易:
doQuery: boolean = true;
query$.pipe(takeWhile(()=> this.doQuery).subscribe( result => {
if(result < limit)
this.doQuery = false;
}
else {
process result, i.e append somewhere...
}
});
【讨论】:
您可以将 expand 运算符用于简单的“条件重复”行为。
仅作为示例,我更改了查询以返回一个数字,而不是结果集。以下不断查询,直到检索到的数字小于100
const { defer, empty } = rxjs;
const { expand, toArray} = rxjs.operators;
const query$ = defer(async () => Math.floor(Math.random()*1000));
query$
.pipe(
expand(result => result < 100 ? empty() : query$),
toArray()
)
.subscribe(console.log);
<script src="https://unpkg.com/rxjs/bundles/rxjs.umd.min.js"></script>
【讨论】:
You can also use RXJS Interval with TakeWhile operator. Here is the sample code
In Component:
return Observable
.interval(250)
.flatMap(() => this.getQueryData())
.takeWhile(data => this.checklimit(data))
.subscribe(result => console.log(result);
getQueryData(){
// HTTTP API call
}
checklimit(){
// return true/false
}
【讨论】: