Subscribe two times to one observable

first func:

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

second func:

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;
  }

in service i need to update data after request

but also in same time i need to clear current editItem

all of it must be done after i subscribe to one httpRequest

.share() - suport from rxjs-compat package (i want to remove this dep in closest time)

without .share() - work only 1 of 2 steps

current rxjs version is 6.3.3

Help who can...

Upvotes: 0

Views: 98

Answers (1)

JB Nizet
JB Nizet

Reputation: 691725

There is a pipeable share operator, that you would use the same way you use map() (i.e. inside pipe()) and thus doesn't need rxjs-compat.

But you don't need share() here. All you need is the 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))
      );
  }

Upvotes: 1

Related Questions