|
| 1 | +# NGXS State Management Overview |
| 2 | + |
| 3 | +The OSF Angular project uses [NGXS](https://www.ngxs.io/) as the state management library for Angular applications. NGXS provides a simple, powerful, and TypeScript-friendly framework for managing state across components and services. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Purpose |
| 8 | + |
| 9 | +The goal of using NGXS is to centralize and streamline the handling of application state, reduce boilerplate, and maintain a predictable flow of data and events throughout the OSF Angular app. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Core Concepts |
| 14 | + |
| 15 | +- **State**: Defines a slice of the application state and how it is modified in response to actions. |
| 16 | +- **Actions**: Dispatched to signal state changes or trigger effects (e.g., API calls). |
| 17 | +- **Selectors**: Functions that extract and transform data from the store. |
| 18 | +- **Store**: Centralized container that holds the application state. |
| 19 | +- **Effects** (via `@ngxs-labs/effects` or `@ngxs/store`): Side-effect handling such as HTTP requests, logging, etc. |
| 20 | + |
| 21 | +### Diagram |
| 22 | + |
| 23 | +[](./assets/osf-ngxs-diagram.png) |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## Directory Structure |
| 28 | + |
| 29 | +Typical NGXS-related files are organized as follows: |
| 30 | + |
| 31 | +``` |
| 32 | +src/app/shared/stores/ |
| 33 | + └── addons/ |
| 34 | + ├── addons.actions.ts # All action definitions |
| 35 | + ├── addons.models.ts # Interfaces & data models |
| 36 | + ├── addons.state.ts # State implementation |
| 37 | + ├── addons.selectors.ts # Reusable selectors |
| 38 | +``` |
| 39 | + |
| 40 | +``` |
| 41 | +src/app/shared/services/ |
| 42 | + └── addons/ |
| 43 | + ├── addons.service.ts # External API calls |
| 44 | +``` |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Tooling and Extensions |
| 49 | + |
| 50 | +- [Redux DevTools](https://github.com/zalmoxisus/redux-devtools-extension) is supported. Enable it in development via `NgxsReduxDevtoolsPluginModule`. |
| 51 | +- [NGXS Logger Plugin](https://www.ngxs.io/plugins/logger) can be used for debugging dispatched actions and state changes. |
| 52 | +- [NGXS Storage Plugin](https://www.ngxs.io/plugins/storage) allows selective persistence of state across reloads. |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## Testing |
| 57 | + |
| 58 | +- Mock `Store` using `jest.fn()` or test-specific modules for unit testing components and services. |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## Documentation |
| 63 | + |
| 64 | +Refer to the official NGXS documentation for full API details and advanced usage: |
| 65 | +[https://www.ngxs.io/docs](https://www.ngxs.io/docs) |
0 commit comments