2

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.

leonsaysHi
  • 375
  • 2
  • 12
  • May I ask, is the Source Observable long-lived, i.e. does it emit again some time after the last emission went through the pipe? – ggradnig Aug 28 '18 at 18:55
  • Is this possible: [--A-BC--D-E--------------F-] ? – ggradnig Aug 28 '18 at 18:56
  • Yes. Maybe my knowledge of Observable/Observer is not good enough ... maybe it's something you should do on the observer side... – leonsaysHi Aug 28 '18 at 19:22
  • Okay, no this looks like a task for pipes. I just can't find anything built-in ;) – ggradnig Aug 28 '18 at 19:28
  • Yes, can't find anything that would replay slowly all values emitted since the observer subscribed to it. – leonsaysHi Aug 28 '18 at 19:44
  • 1
    You can take a look at this: https://stackoverflow.com/questions/21661391/separate-observable-values-by-specific-amount-of-time-in-rxjs But I'm afraid that would cause problems with long-lived observables as the zip operator will also queue the timer emissions. – ggradnig Aug 28 '18 at 19:48
  • Look at Answer 2 – ggradnig Aug 28 '18 at 19:48
  • @leonsaysHi so you want to make an equal delay between two emissions – martin Aug 29 '18 at 10:21

1 Answers1

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

martin
  • 93,354
  • 25
  • 191
  • 226