I have 2 observables. One returns an event and the other boolean. I want to create a new observable that returns the boolean or event while the value in the store is true. Here is the code. This obs1 should wait until the obs2 returns true.
const obs1 = fromEvent(window,'beforeunload')
const obs2 = this.store.select(aBooleanObservable)
const result: Observable<boolean>= ???
senario 1 = Event fired, value in the store true => result true
senario 2 = Event fired, value in the store false => result noting emit until the value is chaneged to true
CodePudding user response:
I am not sure if this would work, the following answer assumes obs1 only triggers once. you could try
const obs1 = fromEvent(window,'beforeunload')
const obs2 = this.store.select(aBooleanObservable)
const result: Observable<boolean>= obs1
.pipe(
switchMap(()=> obs2 ),
filter((aBooleanObservableRes)=>aBooleanObservableRes)
)
Your result will only be returned when obs2 has a "true" boolean value.
CodePudding user response:
const obs1 = fromEvent(window,'beforeunload')
const obs2 = this.store.select(aBooleanObservable)
const result: Observable<boolean>= combineLatest(obs1, obs2).pipe(
filter((event, obvBoolean) => event && obvBoolean));