RxJ的不确定的可观察调用

时间:2019-02-21 13:40:08

标签: angular typescript rxjs angular2-observables

我需要从服务器检索无限量的数据。应该以以下方式进行:

  1. 发送初始请求
  2. 检索部分数据,并告诉服务器一切正常,我可以获取更多数据
  3. 重复第2步和第3步,直到收到一个特定值,表示没有更多数据

如何使用可观察对象做到这一点?

就目前而言,我只能想到带有类似功能的递归可观察调用。

const send = execSend() {
    this.send(message).subscribe(resp => {
        if (resp === 'end') {
            subscriber.next(byteArr.join(''));
            console.log('finished');
            subscriber.complete();
        } else {
            byteArr.push(resp);
            execSend();
        }
    });
}();

3 个答案:

答案 0 :(得分:1)

以下内容应复制您的想法:完成之前发出1个事件。

for i in color:
    if i == " ":
        color = color.split()
        x = color[0]
        y = color[1]
        z = color[2]
    else: # If HEX color
        if color[0] == "#": # if it starts with "#"
            color = color[1:]
            decX = color[0:1]
            decY = color[2:3]
            decZ = color[4:5]
            x = int(decX, 16)
            y = int(decY, 16)
            z = int(decZ, 16)
            print(color, x, y, z)
        else: # if it's without "#"
            decX = color[0:1]
            decY = color[2:3]
            decZ = color[4:5]
            x = int(decX, 16)
            y = int(decY, 16)
            z = int(decZ, 16)
            print(color, x, y, z)

您可以看到它在this blitz

中工作

答案 1 :(得分:0)

未经测试,但您可以尝试此模式

exec=()=>http.get(....) 

exec().pipe(
  expand((resp)=>exec()),
  takeWhile(resp=>resp !== 'end'),
  scan((acc,curr)=>acc.concat(curr),[])
).subscribe()

答案 2 :(得分:-1)

类似这样的东西:

let todo = true;
interval(100).pipe(
    takeWhile(()=>todo),
    concatMap(()=>getStuff())
).subscribe(data => {
  todo = !isFinished(data);
});
相关问题