`ngControl` is an intuitive and easy-to-use state manager for Angular applications. It provides a simple interface for managing application state, integrates with Redux DevTools.
npm install ng-controlngControl is an intuitive and easy-to-use state manager for Angular applications.
It provides a simple interface for managing application state, integrates with Redux DevTools.
npm i ng-control
- Simplicity: ngControl offers an intuitive and simple API for managing application state.
- Modularity: ngControl is modular and can be easily extended.
- Integration with Redux DevTools: ngControl supports Redux DevTools, making state debugging easier.
- Type-safe with TypeScript: ngControl is written in TypeScript, providing type safety and better integration with the Angular ecosystem.
Store
Methods
- getState(): Signal
- setState(newState: Partial
- selectOn
- select
Store to you Angular module and configure the initial state.import {NG_CONTROL_STATE, Store} from "../state/store";
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
{ provide: NG_CONTROL_STATE, useValue: { / initial state can be anything or empty Object / } },
Store
]
};
interface myStoreInterface {
firstName: string,
lastName: string,
}
export class AppComponent implements OnInit{
public storeData: myStoreInterface;
constructor(private store: Store
this.storeData = this.store.getState();
}
}
Actions are objects that describe the type of change to be made to the application's state. Each action should have a unique name that clearly indicates the type of operation being performed.
- Track State Changes: Actions allow you to precisely track state changes, which is particularly useful when debugging and using tools like Redux DevTools.
- Better Code Organization: Using actions helps in organizing your code better and separating business logic from presentation logic.
- Readability and Clarity: Well-named actions make it easier to understand what is happening in the application.
- Action Names: Action names should be descriptive and clearly indicate what they do. A good pattern is to use the format FEATURE_ACTION, where FEATURE is the part of the application the action pertains to, and ACTION describes the operation.
- Constants: Use constants to define action names to avoid typos and facilitate refactoring.
public setFirstName(userName: string): void {
this.store.setState({ firstName: userName },' SET_FIRST_NAME');
}
public changeFirstName(newName: string): void {
this.store.setState({ firstName: newName }, 'UPDATE_FIRST_NAME');
}
public setUserData(user: myStoreInterface): void {
this.store.setState ({
firstName: 'Mateusz',
lastName: 'Jaracz',
},'SET_USER_DATA')
}
this.store.select(state => state.firstName);
//returns Mateusz
## selectOn()
this.store.selectOn('firstName');
//returns Mateusz
Parameters
url: The URL to which the HTTP request is sent.
action: A string describing the action.
method: The HTTP method (GET, POST, PUT, DELETE).
body: (Optional) The request payload.
headers: (Optional) The request headers.
## Example
public loadData(): void {
const headers = new HttpHeaders({
'Authorization': 'Bearer your-token'
});
const data: Signal<{ data: T } | null> = this.store.effect<{ data: T }>('https://api.example.com/data', 'LOAD_DATA', 'GET', null, headers);
effect(() => {
console.log(data());
// handle data or update component state
});
}
Parameters
url: The URL to which the HTTP request is sent.
action: A string describing the action.
method: The HTTP method (GET, POST, PUT, DELETE).
body: (Optional) The request payload.
headers: (Optional) The request headers.
## Example
public loadData(): void {
const headers = new HttpHeaders({
'Authorization': 'Bearer your-token'
});
this.store.effectObservable<{ data: T }>('https://api.example.com/data', 'LOAD_DATA', 'GET', null, headers)
.subscribe(response => {
console.log(response);
// handle response or update component state
});
}
ngControl provides automatic state saving and loading functionality when the browser window is closed.
To enable automatic state persistence, configure the provider as follows:
{ provide: 'HYDRATION_KEY', useValue: 'your_key' }.
Once this key is set, ngControl will automatically start saving the specified state values.
For example, if your state structure looks like this:
{ user: UserInterface, data: DataInterface }
and you want to automatically save the latest state of the user in the core part of the application,
you simply call
this.store.hydrationData(['user']).
This will ensure that the user state is automatically saved and restored when the application is reopened.
# Manual clear hydration data
this.store.clearHydrationData();
[License
ngControl is available under the MIT License. See the LICENSE file for more information.]()