有没有办法延迟某些值的onNext?,例如:
let subject = new Rx.BehaviorSubject({value: 100})
subject.onNext({ value: 123})
subject.onNext({ value: 999})
subject.onNext({ value: 120}) // could this be delayed with 500ms before being delivered to subscribers?
可能的选择:
答案 0 :(得分:-1)
我认为最简单的方法是使用flatMap observable并发出延迟的。
像这样:
const {Observable} = Rx;
const source = Observable.interval(500)
.flatMap((e) => {
if (e % 2 === 0) {
return Observable.of(e)
.delay(2000);
}
else {
return Observable.of(e);
}
});
source
.subscribe(e => console.log(e));
这会给你这样的结果:
"1, 3, 0, 5, 2, 7, 4, 9, 6"
更容易理解:
- = 250ms
-------- = 2000ms
interval(500)
0-1-2-3-4-5-6-7-8-9-(10)
.flatMap(...)
--------0---2---4---6
--1---3---5---7---9--
source (interval().flatMap())
--1---3-0-5-2-7-4-9-6