RxJS Observed with theme, timer polling and combLatest not working

I wrote a polling function in the API that can also perform pagination. Here, pagination is done using the Observable object, and polling is done using the timer method (I also tried the interval with the same result).

Here is my code:

  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);
  }

Expected Behavior: An HTTP request is run every 5 seconds. And when the user changes the page.

Here's what happens: The first HTTP request is launched, but then another server is not sent to the server. UNTIL pagination is used. After the pagination is used for the first time, the survey also starts to work.

This is the first time I use Observables, so I'm sure I missed something, but I can’t understand what it might be.

(, startWith), .

[...]
  .combineLatest(
    pagination
  )
  .startWith([0, {offset: 0, limit: 3}])
[...]
+4
2

combineLatest() , Observables .

, .startWith(). combineLatest() , pagination Subject , , - .

, .startWith():

.combineLatest(
  pagination.startWith({offset: 0, limit: 3}),
  (timer, pagination) => pagination
)

, , , , timer(), pagination. , , merge(), . timer() .

Observable
  .timer(0, 1000)
  .map(i => { return {offset: i*3, limit: 3}})
  .merge(pagination.startWith({offset: 0, limit: 3}))
  .switchMap(pagination => {
    return Observable.of(pagination).delay(200)
  })
  .subscribe(val => console.log(val));
+2

, .

,

, observable.next(value);

behourSubject

, Subject, , .

private val: Subject = YourObservable;
private uiValue: string;

val.sunscribe((val) => {
   uiVal = val;
});

uiVal , .

, console.log, ,

-1

Source: https://habr.com/ru/post/1670465/


All Articles