Answer the question
In order to leave comments, you need to log in
Why does ReplaySubject return all values?
I have a service:
private publishSubscribeSubject_: ReplaySubject<any> = new ReplaySubject();
private emiter_: Observable<any>;
public publish(channel: EventsChannels, event: any) {
this.publishSubscribeSubject_.next({ channel: channel, event: event });
}
public subscribe(channel: EventsChannels[] | EventsChannels , handler: (value: any) => void) {
return this.emiter_
.filter(emission => emission.channel === channel)
.map(emission => emission.event)
.subscribe(handler);
}
ngOnInit() {
this.objectDetailsSubscription = this.eventService.subscribe(
EventsChannels.OBJECT_DETAILS,
semantic => {
console.log(semantic);
}
);
}
Answer the question
In order to leave comments, you need to log in
because this is how ReplaySubject works )))
if you want to see not all values, but only a limited number - pass this to the first argument, for example: new ReplaySubject(10) will store only the last 10 values. by default, this value is Number.POSITIVE_INFINITY - that is, positive infinity, which means that until js reaches the limit of a familiar number, it will save new and new values and output them accordingly when subscribing.
if the task is to store only the last value with the ability to subscribe to it at any time, then BehaviorSubject is more suitable here
if the question is to show values filtered according to some principle, then try filtering inside the map operator - the filter operator does not transform the value in any way, but only checks whether it is emitted or not emitted.
if your case differs from those described, please describe in the comments what you would like to achieve.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question