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

[RxJS] Learn How To Use RxJS 5.5 Beta 2

时间:2017-09-30 21:07:01      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:logs   pipe   imp   count   import   build   use   hat   beta   

The main changes is about how you import rxjs opreators from now on. And introduce lettable opreator.

import { range } from ‘rxjs/observable/range‘;
import { map, filter, scan } from ‘rxjs/operators‘;

const source$ = range(0, 10);

source$.pipe(
  filter(x => x % 2 === 0),
  map(x => x + x),
  scan((acc, x) => acc + x, 0)
)
.subscribe(x => console.log(x))

 

Build own opreator:

import { interval } from ‘rxjs/observable/interval‘;
import { map, take, toArray } from ‘rxjs/operators‘;

/**
 * an operator that takes every Nth value
 */
const takeEveryNth = (n: number) => <T>(source: Observable<T>) =>
  new Observable(observer => {
    let count = 0;
    return source.subscribe({
      next(x) {
        if (count++ % n === 0) observer.next(x);
      },
      error(err) { observer.error(err); },
      complete() { observer.complete(); }
    })
  });


interval(1000).pipe(
  takeEveryNth(2),
  map(x => x + x),
  takeEveryNth(3),
  take(3),
  toArray()
)
.subscribe(x => console.log(x));
// [0, 12, 24]

 

[RxJS] Learn How To Use RxJS 5.5 Beta 2

标签:logs   pipe   imp   count   import   build   use   hat   beta   

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

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