Home > Net >  Tap to dispatch an action as a side effect is not working
Tap to dispatch an action as a side effect is not working

Time:11-29

I am trying to poll for a process status. I want to keep polling until getting from the server a status that is not IN_PROGRESS. While in the mean time I want to keep updating the status of the process in my store. This is how I am trying to do so:

pollForImportProcessStatusEffect = createEffect(() =>
      this.actions$.pipe(
        ofType(MigrationActions.importInProcess),
        switchMap((payload) =>
          this.pollForProcessStatus(payload.importedFileId).pipe(
            map(data => MigrationActions.importProcessFinished(data.status)),
            catchError(() => of(MigrationActions.importInProcess(payload.importedFileId)))
          )
        )
      )
  );

private pollForProcessStatus(importedFileId: number): Observable<ImportFileProcessStatus> {
    return timer(0, 2000).pipe(
      concatMap(() => this.apiClient.getImportProcessStatus(importedFileId)),
      tap((importFileProcessStatus: ImportFileProcessStatus) =>
    // This action won't be dispatched:
        MigrationActions.importStatusUpdated(importFileProcessStatus)
      ),
      takeWhile((importFileProcessStatus: ImportFileProcessStatus) =>
        importFileProcessStatus.status === FileStatus.IN_PROGRESS, true
      ),
      takeLast(1)
    );
  }

I noticed that the importStatusUpdated action is not being dispatched. I wonder if I am doing something wrong here.

CodePudding user response:

By just creating a action you are not dispatching it. You will need to do something like:

this.store.dispatch(MigrationActions.importStatusUpdated(importFileProcessStatus))           
  • Related