繁体   English   中英

链接主题-RxJS / Angular2

[英]Chaining Subjects - RxJS / Angular2

我对可观测对象非常陌生,我正在努力使自己的想法围绕链接主题的好方法。 从本质上讲,我试图采取观测,将所有发出相同类型和它们连起来,这样,当我打电话的阵列next的第一个主题,每一个主题以后(假设没有error发生在之间)都有机会取值,对其进行操作,然后将其传递给下一个主题,直到到达最后一个主题为止,最后一个主题将发出最终结果。

我已经处理了自己的类来处理这个问题,但是似乎这种特殊情况将一直伴随着可观察对象的出现 ,因此我想知道是否有人知道RxJS或Angular2中已经内置了可以做到这一点的东西。

另外,我是否要强迫受试者做他们不应该做的事情? 是否有更好的方法将算法链接在一起,这样每个函数都有机会在最终返回输入之前依次对输入进行操作,如果需要的话,可能会出错? 在这种情况下,什么是“最佳实践”?

编辑

为了更清楚我在说什么,这类似于我正在寻找的东西:

var wrapper = Subject.chain(subject1, subject2, subject3)

// Subscriptions happen here

/** 
 * This calls subject1.next("HI"), 
 * which then calls subject2.next() with the result of subject1's manipulation of "HI",
 * which then calls subject3.next() with the result of subject2's manipulation of the subject1's manipulation of "HI",
 * which then emits the result of subject3's manipulation of subject2's manipulation of subject1's manipulation of "HI"
 */
wrapper.next("HI"); 

替代方法

万一将来有人发现此问题,这是我在Array.reduce函数的帮助下使用的解决方法。 这不是完美的,但会为我做:

chain<T>(source: Observable<T>, destination: Subject<T>): Observable<T>
{
    let processed = false;
    return source.catch(
        err => {
            let ret = new ReplaySubject<T>(1);
            destination.first().subscribe(ret);
            processed = true;
            destination.error(err);
            return ret;
        }
    ).finally(
        () => {
            // TODO: Allow sources to not propagate complete status
            !processed && destination.complete();
            processed = true;
        }
    ).flatMap(
        (next: T) => {
            let ret = destination;
            if(!processed)
            {
                ret = new ReplaySubject<T>(1);
                destination.first().subscribe(ret);
                destination.next(next);
            }
            processed = false;
            return ret;
        }
    );
}

要链接可观察对象,应考虑使用flatMapswitchMap类的运算符。

这是HTTP的示例:

this.http.get('...')
    .map(res => res.json())
    .flatMap(data => {
      // receive the result of the first request
      // use this result to execute another one
      return this.http.get('...')
              .map(res => res.json());
    }).subscribe(data => {
      // receive the result of the second request
    });

如果您对此有一个很好的教程感兴趣,可以看看这个:

您可以使用订阅来链接主题。

subject1
    .subscribe( subject2 );

subject2
    .subscribe( subject3 );

subject3
    .subscribe( x=> {
        // do you want to do
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM