Ziyaddin Sadygly
Ziyaddin Sadygly

Reputation: 9686

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.

Upvotes: 0

Views: 805

Answers (2)

Krzysztof Skowronek
Krzysztof Skowronek

Reputation: 2946

I would use combineLatest with a selector networkStateSubject && authStateSubject, and then filter the true ones

Upvotes: 1

Andrei Tătar
Andrei Tătar

Reputation: 8295

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?
   );

Upvotes: 2

Related Questions