0

In general we need behavior subject functionality. But only on first subscription we should send subscribe to server in REST. And to send unsubscribe on the last unsubscribe, and all late observers subscribed will gwt the latest json recwived from the first. can i do it using rxjs operaTors and how? or shoul i use custom obserbale ?

currently the custom code for this is this:

public observable: Observable<TPattern> = new Observable((observer: Observer<TPattern>) => {
 this._observers.push(observer);
 if (this._observers.length === 1) {
  this._subscription = this.httpRequestStream$
    .pipe(
      map((jsonObj: any) => {
        this._pattern = jsonObj.Data;
        return this._pattern;
      })
    )
    .subscribe(
      (data) => this._observers.forEach((obs) => obs.next(data)),
      (error) => this._observers.forEach((obs) => obs.error(error)),
      () => this._observers.forEach((obs) => obs.complete())
    );
}
if (this._pattern !== null) {
  observer.next(this._pattern); // send last updated array
}
return () => {
  const index: number = this._observers.findIndex((element) => element === observer);
  this._observers.splice(index, 1);
  if (this._observers.length === 0) {
    this._subscription.unsubscribe();
    this._pattern = null; // clear pattern when unsubscribed
  }
};

});

Shaul Naim
  • 246
  • 3
  • 15

1 Answers1

1

Sounds like you need a shareReplay(1), it will share the latest response with all subscribes.

const stream$ = httpRequestStream$.pipe(
  shareReplay(1),
),

stream$.subscribe(); // sends the request and gets its result
stream$.subscribe(); // doesn't send it but gets cached result
stream$.subscribe(); // doesn't send it but gets cached result
stream$.subscribe(); // doesn't send it but gets cached result

satanTime
  • 12,631
  • 1
  • 25
  • 73
  • Thanks, sound like its the way to go ,but what will bet the result when httpRequestStream$ gets new result ( it getts from websocket) will all the subscribers gets the new one? and how the unsubscribe works in this implementation? also should strem and httpRequestStream$ should be behaviourSubjects? – Shaul Naim May 27 '20 at 08:15
  • in case if it emits everybody will be notified with the new value. when everybody unsubscribed then it closes subscription, if httpRequestStream$ is done then subscriptions are done too. and yes it can be BehaviourSubject or any other Observable. – satanTime May 27 '20 at 08:19
  • ahh thanks man. to be on the safe side i added the current code doing what i am trying to achieve, can shareReplay replace it? – Shaul Naim May 27 '20 at 08:44
  • that's exactly the case for it. wrap `this.httpRequestStream$` with `.pipe(shareReplay(1))` and you're full set to go. – satanTime May 27 '20 at 08:48
  • what do you mean by wrap this.httpRequestStream$? – Shaul Naim May 27 '20 at 09:03
  • to use `this.wrap$ = this.httpRequestStream$.pipe(shareReplay(1));` because if you call `this.httpRequestStream$.pipe(shareReplay(1))` several times it will create several replays instead of sharing one. – satanTime May 27 '20 at 09:05
  • and AFAYK this line ,this.wrap$ = this.httpRequestStream$.pipe(shareReplay(1));, is eaual to the code i posted? no need for any further logic? for unsubscribe for example etc... – Shaul Naim May 27 '20 at 09:09
  • Yep, if you did it in inside of httpRequestStream$ - then nothing should be done extra. – satanTime May 27 '20 at 09:14