I'm building store action. My store model looks like this:
{
entities: {[n:number]: Client},
ids: number[],
}
I fetch ids from backend that corresponds with given conditions.
Then I need to fetch from backend those entities that are not in store yet.
But I don't know how to pass fetched ids to withLatestFrom
function?
const params = {
conditions,
fields: ['id']
};
this.apiService.getList(params)
.pipe(
map(resp => {
const ids: number[] = [];
resp.map((item: Client) => {
ids.push(+item.id);
});
return ids;
}),
withLatestFrom(this.checkEntities()), // how to pass ids ?
tap(resp => {
patchState({
entities: resp[1],
ids: resp[0],
loading: false
});
})
);
private checkEntities(ids: number[]) {
const params: ApiWyszukiwarka = {
conditions: {id: ids},
fields: 'all'
};
return this.apiService.getList(params);
}
Or maybe I'm doing something wrong?
Just don't use withLatestFrom
and have a look at concatMap
instead. withLatestFrom
calls its callback on every emission from its source Observable while staying subscribed to all Observables passed as parameters but this is not what you need. You want to invoke this.checkEntities()
only after you get the list of ids and then create a new Observable and subscribe to that.
So with concatMap
you'll have something like this:
map(resp => {
...
return ids;
}),
concatMap(ids => this.checkEntities(ids))