rxjsobservablesubjectsubject-observer

RxJS: Emit new value whenever all subjects emit true


I have two subjects: networkStateSubject and authStateSubject.

networkStateSubject emits boolean value:

    true if network connectivity is enabled,

    false if network connectivity is disabled.

authStateSubject emits boolean value:

    true if user logged in,

    false if user logged out.

I need to combine these two subjects, so that whenever both of their values are true, new value (void) is emitted from combined observable.


Solution

  • You can use combineLatest and filter to check for both values.

    const connectedAndAuth$ = combineLatest([networkStateSubject, authStateSubject])
       .pipe(
          filter(([isConnected, isAuth]) => isConnected && isAuth),
          map(_ => 1) //what do you mean emit a void value?
       );