0

In Angular, ReplaySubject returns the last value received, if receiver is late to subscribing.

The ReplaySubject is comparable to the BehaviorSubject in the way that it can send “old” values to new subscribers.

Is there anyways, someone can clear out the cache, and reset, so ReplaySubject has no history at all?

Here is my ReplayService method

Would like to add to the following method, to have a clearhistory method, etc

export class BasicReplayService {

  private messageSource = new ReplaySubject();
  currentMessage = this.messageSource.asObservable();

  constructor() { }

  changeMessage(currentMessage) {
    this.messageSource.next(currentMessage);
  }
}
  • If you don't need history of values, use a simple Subject. If you subscribe afterwards, it will still subscribe to next emitted values. – Sanket Nov 12 '19 at 05:17
  • Why can't you just re-initialize it? – cs95 Nov 12 '19 at 05:36
  • hi @cs95 how would I do that? thanks –  Nov 12 '19 at 05:50
  • `reset() { this.messageSource = new ReplaySubject(); }` Now new subscribers will only see the new observable with no history. – cs95 Nov 12 '19 at 05:54
  • hi @cs95 what will happen to existing subscribers? are they unsuscribed? I was testing my code, seems like it, I want existing subscribers to stay, thanks –  Nov 12 '19 at 06:02
  • That's fair, it seems like you'd need to ship your own implementation, this might be what you're looking for: https://stackoverflow.com/questions/51145664/resetting-replaysubject-in-rxjs-6 – cs95 Nov 12 '19 at 06:06

0 Answers0