I have an observable (onAuthStateChanged) from the Firebase client that:
- emits
nullimmediately if the user is not signed in, and - emits
nulland then auser objecta few moments later if the user is signed in.
const observable = new Observable((obs) => {
return app.auth().onAuthStateChanged(
obs.next,
obs.error,
obs.complete
)
})
What I want is to:
- ignore any emitted
nullvalues for the first 1000ms of the app lifecycle (nullcoming after 1000ms is accepted) - always emit
user objectregardless of what time it comes - if no
user objectcomes in the first 1000ms, then emitnullat the 1000ms mark
Here is what I've done (and it seems to work). However, I'm reluctant to use this code as it doesn't seem that concise:
const o1 = observable.pipe(skipUntil(timer(1000)))
const o2 = observable.pipe(
takeUntil(o1),
filter((user) => user !== null)
)
const o3 = timer(1000).pipe(takeUntil(o2), mapTo(null))
merge(o1, o2, o3).subscribe({
next: setUser,
error: console.log,
complete: () => console.log("error: obs completed, this shouldn't happen"),
})
Is there a way to do this without merge? I tried going through the docs but I'm quite lost.
Thanks for your help!