Overview
Happy Habitat uses Angular Signals as the primary state management solution. This modern, reactive approach provides fine-grained reactivity without the complexity of external state management libraries like NgRx or Redux.State Management Architecture
Why Signals?
The application uses Angular Signals (introduced in Angular 16+) for several reasons:- Fine-grained reactivity - Updates only affected components
- Simple API - Easy to learn and use
- Built-in - No external dependencies
- Type-safe - Full TypeScript support
- Performance - Efficient change detection
- Composable - Easy to create derived state with
computed()
State Distribution
State is distributed across services following the Single Responsibility Principle:Using Signals
Creating Signals
Signals are created in services using thesignal() function:
Reading Signals in Components
Computed Signals
Derived state usingcomputed():
Updating Signals
Direct Update:Service-Based State Management
AuthService State
Location:app/services/auth.service.ts:1
UsersService State
Location:app/services/users.service.ts:1
ErrorService State
Location:app/services/error.service.ts:1
LoggerService State
Location:app/services/logger.service.ts:1
State Synchronization
Cross-Service State Sync
The application synchronizes state between services when needed:Session Persistence
State is persisted to localStorage for session continuity:Patterns and Best Practices
1. Service-Based State
Keep state in services, not components:2. Computed for Derived State
Usecomputed() instead of manual updates:
3. Effect for Side Effects
Useeffect() for side effects that depend on signals:
4. Read-Only Signals
Expose read-only versions for external access:5. Signal Naming
Follow consistent naming conventions:Local Storage Strategy
Cache Implementation
Some services use localStorage for caching:Storage Keys Convention
hh_ prefix to avoid conflicts.
Migration from Other State Management
If you’re familiar with other state management solutions:From Redux/NgRx
| Redux/NgRx | Signals |
|---|---|
| Store | Service with signals |
| Selector | computed() |
| Action | Service method |
| Reducer | signal.update() |
| Effect | effect() or service method |
From RxJS BehaviorSubject
Performance Considerations
Change Detection
Signals enable precise change detection:- Components only update when their specific signals change
- No need for
ChangeDetectorRef.markForCheck() - No need for
OnPushstrategy with signals