可观察以及如何控制结果进度 [英] Observable and how to control results pace
问题描述
我正在寻找可以帮助我调整可观察对象发出的结果的运算符,它看起来像这样:
I am looking for an operator that would help me pace the results emitted from an observable, it would look like this :
[--A-BC--D-E----------------]
[--A----B----C----D----E----]
我尝试了AuditTime(),但是它不会重播间隔之间发出的结果,它会执行以下操作:
I tried AuditTime() but it does not replay the results that was emitted between intervals, it does something like this :
[--A-BC--D-E----------------]
[--A----C----E--------------]
感谢您的帮助.
推荐答案
我认为这应该可以满足您的需求:
I think this should do what you need:
const e1 = cold('--A-BC--D-E----------------|');
const expected = '--A----B----C----D----E----|';
const source = e1.pipe(
concatMap(val => of(false).pipe(
delay(5, scheduler),
takeWhile(Boolean),
startWith(val),
)),
);
expectObservable(source).toBe(expected);
这里的窍门是我使用concatMap
总是等到上一个Observable完成.内部Observable发射该值,然后推迟其自身的完成,因此concatMap
强制两次发射之间的延迟.
The trick here is that I'm using concatMap
to always wait until the previous Observable completes. The inner Observable emits the value and then postpones its own completion and thus concatMap
enforces the delay between two emissions.
观看现场演示: https://stackblitz.com/edit /rxjs6-test-scheduler?file=index.ts
这篇关于可观察以及如何控制结果进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!