Search code examples
angularrxjspipebehaviorsubject

How to return a value using the previous and the current value of a BehaviorSubject?


In my angular app, I have a user service, an activeUser$ behaviorSubject and a loggedIn$ one which will be true or false based(piped(mapped)) on the value of activeUser$. When loggedIn$’s next value is true and then false, that would mean that the user has logged out. There is a function which I want to trigger only when it is true and then false. A loggedOut$ observable as a result would be awesome! Any idea?


Solution

  • Just found out the answer I needed from here

    I have implemented it like this:

    loggedOut$ = this.loggedIn$.pipe(
        distinctUntilChanged(),
        map((current) => !current)
    );