angularobservableangular-akita

Akita Observable OnCompleted Never Fires


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: ....

Solution

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

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