How do I append items to Observable
?
This is some code:
this.logEntries = this.controllerService.getLog(this.controller.remoteID, this.skip, this.max); this.logEntries.subscribe(a => { this.allLogEntries.add(this.logEntries) });
and here is their declarations:
logEntries: Observable<Log[]>; allLogEntries: Observable<Log[]> = Observable.empty<Log[]>();
I want to append items to allLogEntries as they are being fetched from the web service. How do I do this?
SubscribinglinkAn Observable instance begins publishing values only when someone subscribes to it. You subscribe by calling the subscribe() method of the instance, passing an observer object to receive the notifications.
Observables are not executed until a consumer subscribes. The subscribe() executes the defined behavior once, and it can be called again. Each subscription has its own computation. Resubscription causes recomputation of values.
Perhaps the scan
operator could interest you. Here is a sample:
obs.startWith([]) .scan((acc,value) => acc.concat(value)) .subscribe((data) => { console.log(data); });
See this question for more details:
Take a look at BehaviorSubject:
It's an Observable
where you can push a new item as containing object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With