Our applications state service uses a Firefly Semantics Slice Object Store ( OStore
) to store an authenticated User
instance.
export const OSK: OStoreStart = {
USER: { value: null },
};
export interface OSKI extends KeyObsValueReset {
USER: ObsValueReset;
}
const OS: OStore<OSKI> = new OStore<OSKI>(OSK);
The application will also use a reference an Observable<boolean>
that notifies whether the user is authenticated.
const isAuthenticated$: Observable<boolean>
We wish to derive this Observable
reference from the USER
state in our object store.
Approach
We will pipe the value emitted by the USER
obs
Observable
through map
where we map it to a boolean
value.
const isAuthenticated$: Observable<boolean> = OS.S.USER.obs.pipe(
map((r) => {
return !!r;
})
);
Demo
When we update the value of USER
to a non null
value, true
is logged. When USER
is set to null
false is logged: