嵌套深度未知的嵌套 Observable [英] Nested Observable with unknown nesting depth

查看:49
本文介绍了嵌套深度未知的嵌套 Observable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当向 Sharepoint URI 发送 Get 请求时(例如 http://company.com.au/teams/Telstra/_api/web/Lists/getbytitle('${listTitle}')/items),它返回有限数量的项目(例如 30 个)并提供查询链接以检索接下来的 30 个项目,如下所示:

When sending a Get request to Sharepoint URI (e.g. http://company.com.au/teams/Telstra/_api/web/Lists/getbytitle('${listTitle}')/items), it returns a finite number of items (e.g. 30) and provides a link to query to retrieve the next 30 items like so:

我正在编写一个 Angular 应用程序来检索所有项目.伪代码如下:

I am writing an Angular app to retrieve all items. The pseudo code is like so:

public GetByDomainOwner(listTitle: string, domainNameOwner: string): Observable<any[]> {

    let dataToReturn = new Array<any>();

    let URI = `http://company.com.au/teams/Telstra/_api/web/Lists/getbytitle('${listTitle}')/items?$filter=DomainOwnerText eq '${domainNameOwner}'`;

    var headers: HttpHeaders = new HttpHeaders();
    headers.append('Accept', 'application/json;odata=verbose');

    while(URI !== undefined){
        const records = this.http.get(URI, {
            headers: headers,
            withCredentials: true
        });

        records.subscribe(r => {
            let data = <SharepointItem> r;

            dataToReturn.push(data.d.results);

            if(data.d.__next){
                URI = data.d.__next;
            } else{
                URI = undefined;
            }
        });
    }       

    console.log("Escaped while loop");

    return new Observable<any[]>(dataToReturn); // This is invalid obviously, but it describes how I want to make one Observable call to retrieve all data.
}

我想进行上述单个 Observable 调用,它将读取并订阅 d.__next 以检索下一个有限数量的项目

I want to make the above one single Observable call where it will read and subscribe to d.__next to retrieve the next finite number of items

使用 Observable flatmap 对于嵌套深度已知的情况似乎很有希望(例如 C 订阅 B 的流,其中 B 订阅 A 的流).我想制作一个 flatMap(对其他建议开放),可以在不指定深度的情况下重复检查和订阅更多流.

Using Observable flatmap seems to be quite promising for situations where the depth of nesting is known (e.g. C subscribes to B's stream, of which B subscribes to A's stream). I want to make a flatMap (open to other suggestions) that can repeatedly check and subscribe to more streams without specifying a depth.

推荐答案

正如@KiraAG 评论的那样,expand 可能就是你想要的.

As @KiraAG commented, expand is probably what you want.

这篇博文应该包含您想要的详细信息:https://ncjamieson.com/understanding-expand/

This blog post should have the details you are after: https://ncjamieson.com/understanding-expand/

关键部分是:

import { empty } from "rxjs/observable/empty";
import { concatMap, expand } from "rxjs/operators";
import { get } from "./get";

const url = "https://api.github.com/users/sindresorhus/repos";
const repos = get(url).pipe(
  expand(({ next }) => next ? get(next) : empty()),
  concatMap(({ content }) => content)
);
repos.subscribe(repo => console.log(repo));

其中 get(url) 返回一个带有 ajax 结果 (content) 和next"的 observableurl(获取下一个结果 - 在您的示例中保存在 __next 中)

where get(url) returns an observable with the ajax results (content) and the "next" url (to get the next results - held in __next in your example)

这篇关于嵌套深度未知的嵌套 Observable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆