angularrxjsngrxredux-observable

Get the result value from an observable stream


I am using an angular 12 project.

I have an observable:

     export class RegisterUserShellComponent implements OnInit {

       constructor(private store: Store<State>) { }

       user$: Observable<User>;
       isNull$: Observable<Boolean>; 

       ngOnInit(): void {
            this.user$ = this.store.select(getSelectedUser);
       }
      
     }

Here is the User class definition:

    export interface User {
        id: string | null;
        name: String;
        socialMedia: SocialMedia;
        companyName: string;
    }

I need to get the value from user$, check if it is null or not and assign it to isNull$.

My question is how can I read a property from the value user$ stream to process the value (to check if it is null or not) and assign the result to isNull$?


Solution

  • Use rxjs, there is no need to manually subscribe. This solution also keeps isNull$ as an observable.

    this.user$ = this.store.select(getSelectedUser);
    this.isNull$ = this.user$.pipe(map(user => user === null));
    

    Then (if relevant) use theasync pipe against each observable in your template.