Observable and how to control results pace

喜夏-厌秋 提交于 2019-12-12 17:51:06

问题


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----]

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--------------]

Thanks for your help.


回答1:


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);

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.

See live demo: https://stackblitz.com/edit/rxjs6-test-scheduler?file=index.ts



来源:https://stackoverflow.com/questions/52063634/observable-and-how-to-control-results-pace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!