2

This solution to use and reset shareReplay is from: RxjS shareReplay : how to reset its value?

private _refreshProfile$ = new BehaviorSubject<void>(undefined);

public profile$: Observable<Customer> = _refreshProfile$
  .pipe(
    switchMapTo(this.callWS()),
    shareReplay(1),
   );

public refreshProfile() {
  this._refreshProfile$.next();
}
João Portella
  • 357
  • 3
  • 9

1 Answers1

1

You can use switchMap. Use it as follows :

private _refreshProfile$ = new BehaviorSubject<number>(0);

public profile$: Observable<Customer> = _refreshProfile$
  .pipe(
    switchMap(userId => this.callWS(userId)),
    shareReplay(1),
   );

public refreshProfile(userId: number) {
    this._refreshProfile$.next(userId);
}

this.profile$.subscribe(console.log);
this.refreshProfile(20); // 20 will be passed as userId to callWS

StackBlitz reference: https://stackblitz.com/edit/angular-ivy-ezaxsk?file=src/app/hello.component.ts

Nikhil Londhe
  • 41
  • 2
  • 6