RxJS - FlatMap watcher called multiple times

I am trying to understand how flatMap works . I understand this is a way to handle an Observable <Observed <T →.

Anyway, I tested his behavior and got stuck in this:

let plusDom = document.querySelector('#plus');
let minusDom = document.querySelector('#minus');

let minusSource = Rx
              .Observable
              .fromEvent(minusDom, 'click');

let plusSource = Rx.Observable
  .fromEvent(plusDom, 'click');

plusSource
  .flatMap(function(c){
    console.log('Flatmap called');
    return minusSource;
  })
  .subscribe(function(e){
  console.log('done');
})

Here's jsbin: https://jsbin.com/sefoyowuqe/edit?html,js,console,output

I do not understand this behavior:

3 clicks on plusDom prints:
Flatmap called
Flatmap called
Flatmap called

1 click on print minus Dom:

done
done
done

Why, when I click the minusDom button, it repeats events as many times as we click on plusDom?

+4
source share
1 answer

flatMap . , switchMap, , , .

, SwitchMap .

:

Flatmap "" :

flatMap Marble Diagram

Switchmap "" .

switchMap Marble Diagram

+1

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


All Articles