How to create selector with parameters like in NGRX signalStore like `createSelector` in NGXS?
I'm refactoring the code and I started to moving code to the signalStore, but I was stuck while creating custom selector with some property to make it like signal computed value.
NGXS Code:
interface Permission {
name: string;
}
interface UserStateModel {
permissions: Permission[];
}
const DEFAULT_STATE: UserStateModel= {
permissions: []
};
@State<UserStateModel>({
name: 'user_state',
defaults: DEFAULT_STATE,
})
@Injectable()
export class UserState {
@Selector()
static permissions(state: UserStateModel): Permission[] {
return state.permissions;
}
static permission(spermissionName: string) {
return createSelector(
[UserState.permissions],
(permissions) => permissions.find(x => x.name === permissionName)
})
}
}
As you can see, I can use UserState.permission('myPermissionName') and do any action if this position change.
export type UserStoreModel = {
permissions: Permission[;
};
const DEFAULT_STATE: UserStoreModel = {
permissions: [],
};
export const UserStore = signalStore(
withState(DEFAULT_STATE),
withComputed(store => {
return {
permissions: computed(() => store.permissions() ?? []),
};
}),
);
It should be a method which return Signals? Maybe there is something like createSelector
which I can use here with Signals?
Thanks for help.
I did a research but I can't find any valid solution for this.
I'm expecting I will be able to make selector with Signals using ngrx signalStore.
Example usage of this:
/// My view
const hasAccessPermissions = computed(() => {
const result = this.userStore.hasAccess('MyPermission'); //<--- this should be reactive
return result;
});
private readonly userStore = inject(UserStore);