I am trying to read all emitted values of a replaySubject.
selectedPersons = new ReplaySubject();
sub = new Subscription;
--
ngOnInit() {
this.subscription = this.selectedPersons.subscribe((resp) => {
console.log(this.selectedPersons)
console.log(this.selectedPersons._events) <--- This one doesnt work because its private but i need this values
})
}
--
onSelect(value: Person) {
this.selectedPersons.next(value)
}
Data will be emitted in this Subject and i want to read all its values, not only the last one. What is the best approach to do ?
I want to display this data then in the html with | async
CodePudding user response:
I assume every time new event comes you want to get all the events that arrived before. to achieve it you can use scan
this.selectedPersons.pipe(scan((prev, evnt) => prev.concat(evnt), [])).subscribe((resp) => {
console.log(resp); // all events will be here
})
CodePudding user response:
The values that selectedPersons
emits can be seen in the lambda function you give to subscribe
.
this.selectedPersons.subscribe(resp => console.log(resp));
In this case, the emissions are called resp
. So that's what you would need to log.
You can call them whatever:
this.selectedPersons.subscribe(persons => console.log(persons));
If you need all previous emissions during each emission, you can do that like this:
this.selectedPersons.pipe(
scan((acc, v) => [...acc, v], [])
).subscribe(allPersons => console.log(allPersons));