I'm trying to generate generic reducers and actions of ngrx in Angular. I've already managed to do it but I have to apply the singelton pattern manually to generate the actions and reducers.
class CertificateGroupReducer extends CommonReducer<CertificateGroup, CertificateGroupState> {
private static instance: CertificateGroupReducer;
private constructor() {
super('certificate-groups', certificateGroupActions, initialState, otherReducers);
}
public static getInstance(): CertificateGroupReducer {
if (!CertificateGroupReducer.instance) {
CertificateGroup.instance = new CertificateGroup();
}
return CertificateGroup.instance;
}
}
export const certificateGroupReducer: CertificateGroup = CertificateGroup.getInstance();
This way I can import the instance and use it in the module to use it as param of StoreModule.forFeature()
import { certificateGroupReducer } from '../../certificate-groups/state/certificate-group.reducer';
@NgModule({
declarations: [],
imports: [
CommonModule,
EffectsModule.forFeature([CertificateGroupEffect]),
StoreModule.forFeature(
'certificate-groups',
certificateGroupReducer.reducer, // <-- HERE!!
),
],
})
export class CertificateGroupsStateModule {}
Now I'm trying to use Angular's dependency pattern to handle object creation itself.
I've tried with the new function "inject()" but it doesn't seem to be usable in that context.
@NgModule({
declarations: [],
imports: [
CommonModule,
EffectsModule.forFeature([CertificateGroupEffect]),
StoreModule.forFeature('certificate-groups', inject(CertificateGroupReducer).reducer), // <- HERE!!
],
})
export class CertificateGroupsStateModule {}
Module using Inetable() instead of manual Singelton
@Injectable({
providedIn: 'root',
})
export class CertificateGroupReducer extends CommonReducer<CertificateGroup, CertificateGroupState> {
constructor(private certificateGroupActions: CertificateGroupActions) {
super('certificate-groups', certificateGroupActions, initialState, otherReducers);
}
}
Versions
- Angular: 15.0.0
- NGRX: 15.4.0
Is there a way to fix this, or as angular is thinking is impossible to do?