One of the properties in my Issue object is an array of Tool objects, with both Issues and Tools stored in separate tables in my database.
In my issues-log.component, I am successfully dispatching an action to search for matching issues based on a query.
Now, my goal is to retrieve the list of tools associated with each issue by passing the issue id to my tools service. To achieve this, I created another effect that listens for the ISSUE.SEARCH.COMPLETE action. However, looping through the array of issues in my tools service to call the API for each issue id feels inefficient. It slows down the loading time for large lists of issues and limits the re-usability of my tools service.
I don't want to wait for all the issues to load before fetching the associated tools. Is there a way to optimize my code so that I can start adding the tools as the list of issues is being constructed within the issuesSearch$ effect?
Component:
@Component({
selector: issue-log,
template: `
<issue-search (search)="search($event)></issue-search>
<issue-list [issues]=$issues | async></issue-list>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class IssueLogComponent {
issues$: Observable<Issue[]>;
constructor(private store: Store<fromRoot.State>) {
this.issues$ = store.select(fromRoot.getIssueSearchResults);
}
search(query) {
this.store.dispatch(new issue.IssueSearch(query));
}
}
Effect:
@Effect() issueSearch$: Observable<Action> = this.actions$
.ofType(issue.ISSUE_SEARCH)
.debounceTime(300)
.map(toPayload)
.switchMap(query => {
if (query === '') {
return empty();
}
const nextSearch$ = this.actions$.ofType(issue.ISSUE_SEARCH).skip(1);
return this.issueService.getIssuesFromQuery(query) //calls API service
.takeUntil(nextSearch$)
.mergeMap((res: Issue[]) => {
// How do I make another API call here, passing data from each element of res array?
return Observable.from([
new issue.IssueSearchComplete(res)
])
})
.catch(() => of(new issue.IssueSearchComplete([])));
});
I also attempted calling my tools service from within my issues service, but it didn't feel like the right solution.