2

In my Angular 8 application, I make calls to an Api Service, then write the data to an Akita Entity Store to be used for follow up requests. I do this by using a mergeMap to send the requests in a synchronous manner.

I have some additional requests I wish to do in a concurrent manner with a forkJoin with this function, however, I found the following problem with the akita selectAll function does not fire the onCompleted function/event. It returns data but onCompleted never fire. Leaving the Observable in an incomplete state.

If I remove the call to the Akita Entity store. I can see the Api Call to getTeams Observable the function completes. I added debugging code to the akita entity store call and see the the onCompleted never fires. I am not sure why but perhaps I am doing something incorrectly.

Function:

   getTeams(): Observable<Team[]> {
     return this.teamApiService.getTeams().pipe(
      mergeMap((teams) => {
        this.teamStore.set(teams);
        return this.teamQuery.selectAll({ sortBy: 'name' });
      })
     );
   }

Invoking Function:


   this.teamStateService.getTeams().subscribe(
      data => console.log('GOT getTeams:', data),
      err => console.log('Error:', err),
      () => console.log('getTeams: Completed')
    );

I should see the following results:

GOT getTeams: .... 
getTeams: Completed

However, I just get

GOT getTeams: ....
thxmike
  • 614
  • 1
  • 7
  • 25
  • what makes you think that you _should_ see those results? – Jota.Toledo Jun 26 '19 at 19:36
  • @Jota.Toledo - If I remove the call to the Akita observable selectAll function, I get the desired results. I would assume that if the call to the observable api function getTeams completes and then the observable selectAll completes the getTeams would be marked as complete. However, perhaps, I am thinking about this too much like promises. I am just not quite sure how to approach it. – thxmike Jun 26 '19 at 19:41
  • You arent overthinking, indeed your logic is right. The only thing is that you are making a wrong assumption; the observable returned by the store selector **does not complete**. A store is a source of events that is "never" closed, so any stream that is created from it is infinite. – Jota.Toledo Jun 26 '19 at 20:27
  • Interesting. I did not think the entry and exit of EntitiyStore was used in this manner. If I were to use something that the does parallel requests such as a forkJoin, I would not be able to use it with akita, since it is looking for a "completed" state for each observable. I will have to look for another way. Thanks for the info. – thxmike Jun 27 '19 at 10:47

1 Answers1

3

You can force it to complete. You can add take(1):

return this.teamQuery.selectAll({ sortBy: 'name' }).pipe(take(1));

undefined
  • 6,366
  • 12
  • 46
  • 90