I am currently facing a weird bug with ngrx state management and I think it might be due to me misinterpretating ngrx's createFeature method, possibly only missinterpretating @ngrx/entity
Here is my code:
import {User} from "../../../models/dto/User";
import {createEntityAdapter, EntityAdapter, EntityState} from "@ngrx/entity";
export interface UserState extends EntityState<User> {
selectedUser: User | null
}
export const userAdapter: EntityAdapter<User> =
createEntityAdapter<User>({
selectId: (user) => user.uuid,
})
export const initialUsersState: UserState = userAdapter.getInitialState({
selectedUser: null // taken from database once the selectedUser is requested the first time
})
I could not do
selectedUser?: User
since the createFeature method would complain with lots of errors, including one, which according to the docs mentions "'optional properties are not allowed in the feature state'". But I think this should be possible in a store, for a property to be undefined, no?
Now the problem that this null-setting is causing me, is that somehow there is a race-condition between the initial state being set to null and the selectedUser being retrieved from the database. The null always wins, since once it is assigned, it stops the action that retrieves the user from the database.
All in all I suspect this unexpected and undefined behaviour to be caused by either ngrx in general or by ngrx/entity, combined with my use of null for a property that is undefined at the initial state.