I'm trying to figure out how to combineLatest of two sources, one of which may be undefined. Here's what I have:
const observable1 = interval(100);
const observable2 = this.ref?.observable; // a reference that may not exist until the future
combineLatest([observable1, observable2]).subscribe(...)
The issue is I want the output observable to subscribe to this.ref.observable as soon as this.ref is defined (but I can't predict when that will happen).
This is what I've attempted...
observable1.pipe(
startWith(of(true)),
switchMap((_) => interval(10)),
skipWhile((_) => !this.ref),
switchMap((_) => observable1),
combineLatestWith(this.ref?.observable ?? EMPTY),
)
.subscribe((val1, val2]) => {...})
However, I encountered TypeScript errors regarding the types returned from combineLatestWith and it feels like an overly complex solution for a seemingly simple problem!
I'm wondering if there's a simpler approach to receiving emissions from a source observable when the object the source observable relies on might be undefined at the time I aim to start observing?