根据其他可观察条件返回可观察结果

时间:2019-07-11 09:11:51

标签: javascript angular rxjs observable store

我需要根据另一个可观察的条件返回可观察的数据,因此,如果存储中的ID不同,则如果不发送任何更新,则将更新的数据发送到外部可观察的对象,我需要此功能来支持多标签页,因此每个标签页都有自己的加载方式,不会影响其他标签页, 因此,此代码仅在search_id发生更改时才返回数据,如果没有更改,我们应该在外部可观察到的地方返回什么??

       this.data$ = this.getTopData$('book').pipe(
        map(data => {
               this.store
              .select(currentSearchSelector)
              .pipe(bufferCount(2))
             .subscribe(([previousSearch, currentSearch]) => {
             if ( previousSearch.id !== currentSearch.id ) {

               return data;
             }else
              {
               //what should I return here?
              }

             })
            })    
        ),

如果我按原样返回数据,则其他选项卡中的小部件会感觉到更改并在其他选项卡更改搜索时加载其自身的数据。

1 个答案:

答案 0 :(得分:0)

我对代码进行了一些整理,可以使用never()不向流发送任何内容,因此视图上没有更新。

this.getTopData$('book').pipe(
        switchMap(data =>this.store.select(currentSearchSelector).pipe(bufferCount(2))),
        switchMap(([previousSearch, currentSearch]) => {
             if ( previousSearch.id !== currentSearch.id ) {
               return of(data);
               }
              else return never()
       }})
       ).subscribe()
相关问题