Ngrx dispatch action not working if request is still pending

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Ngrx dispatch action not working if request is still pending



I have a component that gets object details by subscription (no ngrx or api involved at this time).



After getting the information i'm dispatching an action and retrieve the data.



Works good expect when there are many calls and if a request is still pending then no dispatching action is happened.



Is there any way to cancel the request and to refer to the last call?



any other approach or solution?


this.service.currentMessage.subscribe((selected) =>
if(selected)
this.obj =
id: selected.id
;
// when there are many calls and a call is still in pending then
// this row actually not happened and no call made
this.store.dispatch(new FetchList(this.obj));

);



thanks





why do you trigger a dispatch action in a subscribe block ? You should run it in a your effect or in a guard or in a ngOnInit block.
– Whisher
Aug 12 at 18:41






I'm quite new to this - this code is part of the ngOnInit block
– eladr
Aug 12 at 18:48





In your State you should have a loaded prop so you can check for it
– Whisher
Aug 12 at 18:49





You should dispatch action in your init check for the loaded prop in your effect ofType(yourAction) run the service
– Whisher
Aug 12 at 18:51






Try to describe your flow so people can help you. What's trigger currentMessage ?
– Whisher
Aug 12 at 18:59




1 Answer
1



One of ways to solve this problem is to stop multiple request. So in your service where you get Data add RxJs ShareReplay operator like this:


this.http.get('your url')
.pipe(
shareReplay);



This means when you request many times , if request is already pending other requests will be ignored.





Any method to cancel the pending requests and to execute the last request?
– eladr
Aug 12 at 19:49





yes use switchMap operator. This will simply cancel previous request and execute latest. this.http.get('your url') .pipe(switchMap);
– Vugar Abdullayev
Aug 12 at 19:50






thanks i will try tomorrow...
– eladr
Aug 12 at 19:52





You are welcome ! Please do not forget to mark question as answer :)
– Vugar Abdullayev
Aug 12 at 19:53





in switch you include observable that you want cancel and renew. in this situation switchMap(this.http.get('your url')).
– Vugar Abdullayev
Aug 13 at 18:29






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard