I am using the NGRX platform in an angular 4 project. I'm using RXJS +5.
On init of a component, I am selecting an array of objects from my store. I want this observable to emit a single array every time data changes in my store.
I am unpacking the array just fine, but packaging the stream of observables back into a single array that emits once is causing me trouble.
I would like to do the following, but this never emits a value because the subscription never completes. To my knowledge, toArray
only emits on completion.
this.addedProducts$ = this._store.select(s=>s.order.selectedProducts)
.flatMap(prods=>prods) //emits an observable for each product in store
.map(prod=>prod._id) //returns the id for each product
.toArray(); // I would like this to be an Observable<string[]>
Instead, I have done something like this:
this.addedProducts$ = this._store.select(s=>s.order.selectedProducts)
.flatMap(prods=>prods)
.map(prod=>prod._id)
.scan((acc, value) => {
acc.push(value);
return acc;
}, [])
But this emits an new array every single iteration of adding an _id to the array. I would like this observable to emit a single array with all the values in it.
How can I achieve this?