jschuss
jschuss

Reputation: 655

Combine RXJS observables, but wait for first one to begin emitting values

I have a stream of video player events that fire as certain events (like pause or play) is pressed.The stream is coming from two observables using combineLatest, to grab the current video position whenever an event is emitted.

I need to emit another event every 10 seconds and throw it into the mix. Everything is working fine, but the ping$ observable starts emitting events before the first events$ value is emitted, which would be the event that marks that the player has been loaded, and all other events may be recorded.

How can I combine ping$ and events$ into one stream, but only let ping$ begin once events$ has started emitting values?

const events$ = mediaStreams.events$;
const currTime$ = mediaStreams.currentTime$;

const intervalSource$ = interval(2000);
const ping$ = intervalSource$.pipe(map(() => "ping"));

const concatEvents$ = merge(events$, ping$);


const combined = concatEvents$.pipe(
 withLatestFrom(currTime$),
   map(([first, second]) => {
    return {
       event: first,
       position: second
     }
  })
)


combined.subscribe(val => console.log('COMBINED', val));

Thank you

Upvotes: 2

Views: 403

Answers (1)

Antediluvian
Antediluvian

Reputation: 733

ping$.pipe(
    skipUntil(events$)
);

ping$ will be skipped until events$ emits.

Upvotes: 5

Related Questions