I am having an existing action that calls the api with list of IDs as querystring parameter and fetches the thumbnail response for the passed IDs. In some cases, count of IDs can be 150-200 records. Hence, I am working on making this APIs call batch based and run in parallel using forkJoin
and subscribe
methods of RxJs. I'm facing following 3 issues regarding the implementation.
I am able to get the batch based response in next method of subscribe. However, the action invoked in this function along with response is not getting invoked.
maxParallelQueries dont seems to be working as intended. All the fetchThumbnailObservables() are executed at once and not in the batches of 3 as set as constant/
How to handle return of observable so that mergeMap do not give syntax error .
Argument of type '({ payload }: IAction) => void' is not assignable to parameter of type '(value: IAction, index: number) => ObservableInput<any>'. Type 'void' is not assignable to type 'ObservableInput<any>'.ts(2345)
Here is my current code looks like. Any help would be great on this.
const getThumbnails: Epic<IAction, IAction, IStoreState> = (action$, state$) =>
action$.ofType(ActionTypes.AssetActions.DATA.FETCH_THUMBNAILS).pipe(
mergeMap( ({ payload }) => {
const assetIds = Object.keys(payload);
const meta = payload;
const batchSize = 10;
const maxParallelQueries = 3;
const fetchThumbnailObservables : Observable<IThumbnailResponse>[] = [];
for (let count = 0; count < assetIds.length; count += batchSize) {
fetchThumbnailObservables.push(AssetDataService.fetchThumbnailData(assetIds.slice(count, count + batchSize)));
}
forkJoin(fetchThumbnailObservables)
.pipe(mergeMap(fetchThumbnailObservable => fetchThumbnailObservable, maxParallelQueries)) // Issue 1 : maxParallelQueries limit is not working
.subscribe({
complete: () => { },
error: () => { },
next: (response) => {
// Issue 2 : below action dont seems to be getting invoked
actions.AssetActions.receivedThumbnailsAction(response, meta)
},
});
// Issue 3 : How to handle return of observable
// Added below code for prevennting error raised by mergeMap( ({ payload }) => {
return new Observable<any>();
}),
catchError((error) => of(actions.Common.errorOccured({ error })))
);
```