[RxJS] Transformation operator: buffer, bufferCount, bufferTime

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[RxJS] Transformation operator: buffer, bufferCount, bufferTime相关的知识,希望对你有一定的参考价值。

This lesson will teach you about another horizontal combination operator: buffer and its variants. Buffer groups consecutive values together, emitting the output as an array. The buffer variants and their arguments allow to specify when to close the buffers.

 

buffer(close observable): According to another observalbe to group items.

var foo = Rx.Observable.of(‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘)
  .zip(Rx.Observable.interval(600).take(5), (x,y) => x);
var bar = Rx.Observable.interval(900).take(3);

/*
-----h-----e-----l-----l-----o|       (foo)
--------0--------1--------2|          (bar)

        buffer(bar)

--------h--------e--------ll|
*/

var result = foo.buffer(bar);

result.subscribe(
  function (x) { console.log(‘next ‘ + x); },
  function (err) { console.log(‘error ‘ + err); },
  function () { console.log(‘done‘); },
);
  
  /*

"next h"
"next e"
"next l,l"
"done"
  
  */

 

bufferTime(number): 

var foo = Rx.Observable.of(‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘)
  .zip(Rx.Observable.interval(600).take(5), (x,y) => x);

/*
-----h-----e-----l-----l-----o|       (foo)
--------x--------x--------x|          (900ms)

        bufferTime(900)

--------h--------e--------ll|
*/

var result = foo.bufferTime(900);

result.subscribe(
  function (x) { console.log(‘next ‘ + x); },
  function (err) { console.log(‘error ‘ + err); },
  function () { console.log(‘done‘); },
);
  
  /*

"next h"
"next e"
"next l,l"
"done"
  
  */

 

bufferCount(number):

var foo = Rx.Observable.of(‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘)
  .zip(Rx.Observable.interval(600).take(5), (x,y) => x);

/*
-----h-----e-----l-----l-----o|       (foo)

        bufferCount(2)

----------([h,e])------([l,l])([o|])l
*/

var result = foo.bufferCount(2);

result.subscribe(
  function (x) { console.log(‘next ‘ + x); },
  function (err) { console.log(‘error ‘ + err); },
  function () { console.log(‘done‘); },
);
  
  /*

"next h,e"
"next l,l"
"next o"
"done"
  
  */

 

以上是关于[RxJS] Transformation operator: buffer, bufferCount, bufferTime的主要内容,如果未能解决你的问题,请参考以下文章

[RxJS] Transformation operator: buffer, bufferCount, bufferTime

[RxJS] Transformation operator: map and mapTo

[RxJS] Transformation operators: delay and delayWhen

[RxJS] Transformation operator: repeat

kettle中跑transformation和job的区别

CG中的仿射(affine transformation)和法线变换