Thomas Vanhoutte
Hi I'm Thomas, a 26 years old frontend developer. I currently work at TVH and I develop in Angular.
A state management pattern
How does it work?
import {State} from '@ngxs/store';
import {Branch, Company, WsplItem} from '@psp/entities';
import {User} from '../modules/common/types/user';
export interface AppStateModel {
companyAndBranch: {
company: Company;
branch: Branch;
};
pspItems: Array<WsplItem>;
selectedWsplItem: WsplItem;
pspItemsFilter: any;
wsplScrollPosition: number;
profile: {user: User; loggedIn: boolean; language: {code: string; text: string};};
}
const initialState: AppStateModel = {
companyAndBranch: undefined,
pspItems: undefined,
selectedWsplItem: undefined,
pspItemsFilter: undefined,
wsplScrollPosition: undefined,
profile: undefined
};
@State<AppStateModel>({
name: 'app',
defaults: initialState
})
export class AppState {}
Updates
Streaming Data
Rendering
Actions (methods)
Interacting with other queries
import { ID } from '@datorama/akita';
export type Todo = {
id: ID;
title: string;
completed: boolean;
};
export function createTodo({ id, title }: Partial<Todo>) {
return {
id,
title,
completed: false
} as Todo;
}
import { Todo } from './todo.model';
import { EntityState, EntityStore } from '@datorama/akita';
export interface State extends EntityState<Todo> {}
@Injectable({
providedIn: 'root'
})
export class TodosStore extends EntityStore<State, Todo> {
constructor() {
super(initialState);
}
}
import { QueryEntity } from '@datorama/akita';
@Injectable({
providedIn: 'root'
})
export class TodosQuery extends QueryEntity<State, Todo> {
constructor(protected store: TodosStore) {
super(store);
}
}
ng new angular-akita-thomasvht
yarn add @datorama/akita
yarn add akita/schematics -D
yarn add @datorama/akita-ngdevtools -D
import { Action } from '@ngrx/store';
export enum ActionTypes {
Increment = '[Counter Component] Increment',
Decrement = '[Counter Component] Decrement',
Reset = '[Counter Component] Reset',
}
export class Increment implements Action {
readonly type = ActionTypes.Increment;
}
export class Decrement implements Action {
readonly type = ActionTypes.Decrement;
}
export class Reset implements Action {
readonly type = ActionTypes.Reset;
}
import { Action } from '@ngrx/store';
import { ActionTypes } from './counter.actions';
export const initialState = 0;
export function counterReducer(state = initialState, action: Action) {
switch (action.type) {
case ActionTypes.Increment:
return state + 1;
case ActionTypes.Decrement:
return state - 1;
case ActionTypes.Reset:
return 0;
default:
return state;
}
}
ng new angular-ngrx-thomasvht
yarn add @ngrx/platform
yarn add @ngrx/devtools -D
yarn add @ngrx/schematics -D
ng new angular-ngxs-thomasvht
yarn add @ngxs/store
yarn add @ngxs/devtools -D
yarn add @ngxs/schematics -D
Files generated
Total files
Boilerplate code
9
3
4
12
7
6
Heavy
Medium
Low
1
2
3
Community
1
2
3
1 (4779)
2 (2192)
3 (1290)
1 (186)
2 (105)
3 (24)
1 (753)
2 (1111)
3 (528)
Community
7.9
313
8.7
380.6
8.8
393.0
9
411.9
Size
Non-prod (Mb)
Prod (Kb)
By Thomas Vanhoutte
Hi I'm Thomas, a 26 years old frontend developer. I currently work at TVH and I develop in Angular.