首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >switchMap,startWith,switchMap?

switchMap,startWith,switchMap?
EN

Stack Overflow用户
提问于 2018-09-07 01:38:14
回答 2查看 773关注 0票数 0
代码语言:javascript
复制
this.timeline$ = app.selectedSites$.pipe(
  debounceTime(2000),
  switchMap(sites => this.interval.pipe(map(() => sites))),
  switchMap(sites =>
    analytics.timeline(sites, 60 * 24 * 2, 60).pipe(
      map(result => {
        const colors = getColors(result);

        return {
          labels: result[0].datapoints.map(pair => pair[1]),
          datasets: (<any[]>result).map((target, i) => ({
            pointRadius: 0,
            label: target.target,
            fill: false,
            backgroundColor: colors[i % colors.length],
            borderColor: colors[i % colors.length],
            data: target.datapoints.map(pair => ({
              y: pair[0],
              x: pair[1]
            }))
          }))
        };
      })
    )
  ),
  share()
);

我希望this.timeline$在选择first $ changes (取消)时发出null,并开始为新列表加载第一个数据,然后每30秒刷新一次,但在刷新之前不发出null .我不知道该把startWith(空)放哪儿.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-07 06:08:49

换句话说,您需要两个流:首先是所有运算符,另一个是发出null的流。

代码语言:javascript
复制
const source$ = app.selectedSites$
  .pipe(
    debounceTime(2000)
  )


this.timeline$ = source$.pipe(
  switchMap(sites => this.interval.pipe(map(() => sites))),
  switchMap(sites =>
    analytics.timeline(sites, 60 * 24 * 2, 60)
      .pipe(
        map(result => getField(result))
      )
  ),
  merge(source$.pipe(mapTo(null))),
  share()
);
票数 1
EN

Stack Overflow用户

发布于 2018-09-07 21:59:04

这就是我最后的结果..。

代码语言:javascript
复制
toChartData(selector: (sites: string[]) => Observable<any>) {
    return this.app.selectedSites$.pipe(
      debounceTime(2000),
      switchMap(sites =>
        this.interval.pipe(
          switchMap(() => selector(sites)),
          startWith(null)
        )
      ),
      share()
    );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52214165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档