[RxJS] Transformation operator: bufferToggle, bufferWhen

bufferToggle(open: Observable, () => close: Observalbe : Observalbe<T[]>)

bufferToggle take two args, first is opening observable, seconde is a function which return an observable for closing.

The closeing observalbe only execute after opening emit value.

const source$ = Rx.Observable.interval(500);
const open$ = Rx.Observable.interval(1500);
const close$ = Rx.Observable.interval(1000);

/**

---0---1---2---3---4---5---6---7---8---9----....    (source)

-----------1-----------2-----------3--------...      (open)

           --- ---x    --- ---x    --- ---x...      (close)
        bufferToggle(open$, () => close$)
        
------------------([2,3])-----([5.6])-----([8,9])--...
*/


const foo$ = source$.bufferToggle(open$, () => {
  return close$;
});

foo$.subscribe(
  (x) => console.debug("Next: " + x),
  (err) => console.error(err),
  () => console.info("DONE")
)

/*

"Next: 2,3"
"Next: 5,6"
"Next: 8,9"
"Next: 11,12"
...
*/

 

bufferWhen( () => Observable):

bufferWhen takes a function which return observable.

const source$ = Rx.Observable.interval(500);
const close$ = Rx.Observable.interval(1000);

/**

---0---1---2---3---4---5---6---7---8---9----....    (source)

-------0-------1-------2-------3-------4---....     (close)

        bufferWhen(()=>close$)
        
-------(0)-----([1,2])-([3,4])-([5,6])--......  
*/


const foo$ = source$.bufferWhen(() => close$);

foo$.subscribe(
  (x) => console.debug("Next: " + x),
  (err) => console.error(err),
  () => console.info("DONE")
)

/*

"Next: 0"
"Next: 1,2"
"Next: 3,4"
"Next: 5,6"
"Next: 7,8"
...
*/

 

你可能感兴趣的:([RxJS] Transformation operator: bufferToggle, bufferWhen)