操作符文档
api 列表
do -> tap
catch -> catchError
switch -> switchAll
finally -> finalize
map switchMap mergeMap
scan 和 reduce
// res: 12, 15
from([2, 3]).pipe(
scan((acc, item) => acc += item, 10))
.subscribe(v => console.log(v))
// res: 15
from([2, 3]).pipe(
reduce((acc, item) => acc += item, 10))
.subscribe(v => console.log(v))
filter 和 partition
from([2, 3, 4]).pipe(
filter(item => item <= 3))
.subscribe(v => console.log(v))
pairwise()
of(1, 2, 3)
.pipe(
pairwise()).subscribe(v => console.log(v))
[1,2]
[2,3]
min,max,count
from([1,2]).pipe(max()).subscribe(l) // 2
from([1,2]).pipe(min()).subscribe(l) // 1
from([1,2]).pipe(count()).subscribe(l) // 2
distinct(lambda) distinctUntilChanged([(Prev, current)=>{}]) 和 distinctUntilKeyChanged(key)
from([1, 2, 2, 3, 2])
.pipe(distinct())
.subscribe(l); // 1,2,3
elementAt
from([1, 2])
.pipe(elementAt(0, "default value"))
.subscribe(l);
ignoreElements()
skip(count: Number),skipLast(count: number),skipWhile(lambda)
take(count: number), takeLast(count: number),takeUntil(notifier: Observable),takeWhile(lambda)
throttle(lambda: Observable)和 throttleTime(number)
audit(lambda: Observable)和 auditTime(number)
interval(500)
.pipe(auditTime(1000))
.subscribe(l); // 1s后发出 2, 1被忽略
debounce(lambda: Observable) 和 debounceTime(number)
sample(Observable) 和 sampleTime(number)
find 和 findIndex
toPromise
click = async e => {
let res = await ajax('http://localhost:1995/a').pipe(map(res => res.response)).toPromise();
l(res)
}
buffer bufferCount bufferTime bufferToggle bufferWhen
const send$= fromEvent(document, 'click');
const interval = interval(1000);
const buffered = interval.pipe(buffer(send$));
buffered.subscribe(l);
defaultIfEmpty
const { of, from, empty } = require("rxjs");
const { mergeMap, defaultIfEmpty } = require("rxjs/operators");
from([1, 2, 2, 3, 2])
.pipe(
mergeMap(el => (el > 10 ? of(el) : empty())),
defaultIfEmpty("not data"),
)
.subscribe(l); // not data
delay delayWhen
endWith
from([1, 2])
.pipe(endWith("源观察完成后,附加一个发射,然后完成。"))
.subscribe(l); // 1, 2, "源观察完成后,附加一个发射,然后完成。"
不常用的
pluck(properties: ...string); 取一个对象的属性类似 obj.xxx.xxx
toArray(); 把流发出的值塞在Array后,返回Array