订阅两次到一个可观察

时间:2019-06-04 11:15:08

标签: rxjs

第一个功能:

updateMark(item: MarkDTO) {
    this.service
      .put(item, this.resource)
      .subscribe(() => this.markEdit = null);
  }

第二个功能:

put(item: MarkDTO, rcc: string): Observable<MarkDTO> {
    const rdto = new MarkRDTO(item);
    const url = `${this.getUrl('base')}${rcc}/marks/${rdto.rid}`;
    const obs = this.http.put<MarkDTO>(url, rdto, { withCredentials: true })
      .pipe(map((r: MarkDTO) => new MarkDTO(r)))
      .share();
    obs.subscribe(newMark => this.storage.update(newMark, rcc));
    return obs;
  }

在使用中,我需要在请求后更新数据

但同时我需要清除当前的editItem

所有这些必须在我订阅一个httpRequest之后完成

.share()-支持rxjs-compat包(我想在最短的时间内删除此dep)

没有.share()-仅工作2个步骤之一

当前rxjs版本为6.3.3

帮助谁可以...

1 个答案:

答案 0 :(得分:1)

有一个share operator管道,您可以使用与map()相同的方式(即在pipe()内),因此不需要rxjs-compat。

但是您在这里不需要share()。您只需要tap() operator

put(item: MarkDTO, rcc: string): Observable<MarkDTO> {
    const rdto = new MarkRDTO(item);
    const url = `${this.getUrl('base')}${rcc}/marks/${rdto.rid}`;
    return this.http.put<MarkDTO>(url, rdto, { withCredentials: true })
      .pipe(
         map(r => new MarkDTO(r)),
         tap(newMark => this.storage.update(newMark, rcc))
      );
  }
相关问题