I am working on an effect, that will be polling server.
What I want to achieve is as follows:
1) Send GET request to server
2) After response is received, wait 3 seconds
3) Send same GET request
4) After response is received, wait 3 seconds
5) Send same GET request
... and so on.
The code that I have now, doesn't quite work, as it polls server every 3 seconds, no matter if response was received or not:
@Effect()
pollEntries$ = this.actions$.pipe(
ofType(SubnetBrowserPageActions.SubnetBrowserPageActionTypes.StartPollingSubnetEntries),
switchMap(() => {
return timer(0, 3000);
}),
takeUntil(this.actions$.pipe(ofType(SubnetBrowserPageActions.SubnetBrowserPageActionTypes.StopPollingSubnetEntries))),
switchMap(() => {
return this.subnetBrowserService.getSubnetEntries();
}),
map((entries) => {
return new SubnetBrowserApiActions.LoadEntriesSucces({ entries });
}),
catchError((error) => {
return of(new SubnetBrowserApiActions.LoadEntriesFailure({ error }));
}),
);
Another thing that I am struggling with is how to stop polling. If I emit StopPollingSubnetEntries
action before request is sent to server, then it works fine - however if I emit it after request is sent, then I receive one more subsequent response, before polling stops.