天天看点

Angular rxjs operators 笔记

toArray

/*
  toArray把结果都塞到数组里去
*/
const source = interval(1000);
const example = source.pipe(
    take(10),
    toArray()
);

example.subscribe(val => console.log(val));
// output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
           

toArray

/*
  pairwise把相邻的两个流组成数组
*/
const clicks = fromEvent(document, 'click');
const pairs = clicks.pipe(pairwise());
const distance = pairs.pipe(
  map(pair => {
    return [pair[0].clientX, pair[1].clientX];
  }),
);
distance.subscribe(x => console.log(x));
/*
 [409, 678]
 [678, 772]
 [772, 926]
 [926, 1120]
*/
           

Buffer

/*
  Buffer接受一个Observable作为参数,
  当参数(clicks)发射值时,会把intervalEvents之前发出的值保存在数组里发出来
*/
const clicks = fromEvent(document, 'click');
const intervalEvents = interval(1000);
const buffered = interva