码迷,mamicode.com
首页 > Web开发 > 详细

[RxJS] Transformation operator: bufferToggle, bufferWhen

时间:2016-05-28 06:37:04      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5536682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!