I developed a function to conduct polling on an API while incorporating pagination. For this purpose, I utilized a Subject Observable for pagination and implemented polling using the timer method (I attempted interval as well with similar outcomes).
Below is the code snippet:
getItems(pagination: Subject<Pagination>): Observable<ListResult<Item>> {
let params: URLSearchParams = new URLSearchParams();
return Observable
.timer(0, 5000)
.combineLatest(
pagination,
(timer, pagination) => pagination
)
.startWith({offset: 0, limit: 3})
.switchMap(pagination => {
params.set('skip', pagination.offset.toString());
params.set('limit', pagination.limit.toString());
return this.authHttp.get(`${environment.apiBase}/items`, {search: params})
})
.map(response => response.json() as ListResult<Item>)
.catch(this.handleError);
}
The intended functionality is as follows: An HTTP request will be triggered every 5 seconds AND when there is a change in page by the user.
This is what occurs: Initially, the first HTTP request is made, but subsequent requests are not sent to the server UNTIL pagination is applied. Once pagination is used for the first time, the polling mechanism starts functioning properly.
Due to being new to Observables, I might have overlooked something crucial despite my best efforts to identify any missed elements.
I also experimented with an alternative approach (perhaps lacking the timer counter in startWith), yet it did not yield any changes in behavior.
[...]
.combineLatest(
pagination
)
.startWith([0, {offset: 0, limit: 3}])
[...]