I'm learning NGXS
with the Emitters
plugin in Angular
, and I have some trouble understanding how to declare my state files in a managable way.
I've had success declaring a simple .state file like this:
export type PageStatusCases = 'LOADING' | 'IDLE' | 'ERROR' | 'INITIALIZING';
export interface AppStateModel {
pageStatus: PageStatusCases;
}
@State<AppStateModel>({
name: 'AppState',
defaults: {
pageStatus: 'INITIALIZING'
}
})
export class AppState {
@Selector()
static pageStatus(state: AppStateModel): PageStatusCases {
return state.pageStatus;
}
@Receiver({type: '[Global] Page status'})
static setPageStatus({patchState}: StateContext<AppStateModel>, {payload}: EmitterAction<PageStatusCases>): void {
patchState({pageStatus: payload});
}
}
Now I am trying a more complex example converting my Service
in a State
In my service I have a lot of BehaviorSubject
s keeping track of my UI's state.
title: BehaviorSubject<string> = new BehaviorSubject('');
backClick$: EventEmitter<void> = new EventEmitter<void>();
primaryClick$: EventEmitter<void> = new EventEmitter<void>();
toolbarVisible$: BehaviorSubject<boolean> = new BehaviorSubject(true);
primaryVisible$: BehaviorSubject<boolean> = new BehaviorSubject(false);
primaryDisabled$: BehaviorSubject<boolean> = new BehaviorSubject(false);
primaryAutoDisabled$: BehaviorSubject<boolean> = new BehaviorSubject(false);
primaryIcon$: BehaviorSubject<ToolbarPrimaryIcon> = new BehaviorSubject(ToolbarPrimaryIcon.ADD);
I've started to convert my BehaviorSubject
s to parts of the state, but I realized that I need to create a lot of boilerplate code.
For every BehaviorSubject
I need to:
- Declare it in the state's model
Interface
- Declare it's default state in the
State
- Declare it's
@Selector
in the state "manifest" - Declare it's
@Receiver
(Action
) in the state - Declare it's
@Select
in every component that needs it - Declare it's
@Emitter
in every component that needs it, for when I want to update it
My current situation is 100+ lines of code for correctly state-managing only 7 variables, so I think I am missing something. I'm not even complaining about the amount of visual noise added.
I would like to know what am I missing and what would be a better way of declaring state in a situation like this.
I am using NGXS with the emitters
plugin because I through it was going to reduce the amount of boilerplate but I am not gaining much at the moment.