标签: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