Reputation: 187
I would like to wait for the dispatch of the store in ngrx to complete. Then, I would like to execute the next line of statement which is synchronous call.
I have tried as below which works as expected. The issue is that delay(2500) is not all the exact time. Sometime it may take more or less time to complete the store dispatch line of code. Is there a better way to handle it in order to execute return of(false) statement after the completion of this.store.dispatch...?
validateRules(isCompleteActivity : boolean) {
this.isSubmitProgress = true;
this.transmitSubscription = this.menuService.getRules(this.parameters,
statusTypeOptions.Error).pipe(
switchMap(x => {
if (x.ruleOutput.length > 0) {
return of(false);
} else {
return this.saveService.saveToGrid(null, null).pipe(
switchMap(successful=> {
if (successful) {
if (this.center == '002' && (this.activity == 'AAA' || this.activity == 'BBB')) {
this.store.dispatch(new fromActions.LoadDetails(this.parameters,
isCompleteActivity));
delay(2500);
return of(false);
}
else {
return this.flowService.completeAction();
}
}
return of(false);
})
);
}
})
).subscribe(isSuccess => {
this.isSubmitProgress = false;
if (isSuccess) {
this.bridgeService.closeWindow();
}
},
error => {
this.isSubmitProgress = false;
}
)
}
I am stuck in the above issue for couple of days and do not know how I would be able to escape through it. Really appreciate your help in advance. Thank you again!
Upvotes: 1
Views: 3434
Reputation: 46
Have you looked at effects?
With effects you get the dispatched actions and do something after they've been applied by the reducers. I would suggest re-writing the code to just dispatch the action, and implement an effect to handle what to do next based on the result of the original dispatch.
Upvotes: 1