1

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

mark
  • 129
  • 7

2 Answers2

2
const obs1 = fromEvent(window,'beforeunload')
const obs2 = this.store.select(aBooleanObservable)

const result: Observable<boolean>=  combineLatest(obs1, obs2).pipe(
filter((event, obvBoolean) => event && obvBoolean));
IdontwearString
  • 265
  • 1
  • 19
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 20 '21 at 16:39
0

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.

wscttc
  • 356
  • 1
  • 5