Patterns
This section contains comprehensive code patterns and implementation guides for the Context-Action framework.
Core Framework Patterns
Action Patterns
- Action Patterns - Pure action dispatching with memory management and performance optimization
- Basic Usage - Fundamental Action Only pattern implementation
- Register Patterns - Advanced handler registration and memory management
- Register Delegation - Advanced pattern for modular handler organization
Store Patterns
- Store Patterns - Type-safe state management (Recommended)
- Basic Usage - Fundamental Store Only pattern with type inference
- HOC Pattern - Higher-Order Component pattern for automatic Provider wrapping
- Advanced Config - Performance optimization and custom comparison strategies
Ref Patterns
- Ref Patterns - Direct DOM manipulation with zero re-renders and context singleton management
- Basic Usage - Fundamental RefContext pattern with type-safe ref management
- Context Singleton Handling - Managing context singletons and external resources with lazy evaluation
- Multi-Context - Multiple RefContext composition for complex applications
- Performance - Hardware acceleration and performance optimization
Architecture Patterns
- Architecture Patterns - System architecture and design patterns
- MVVM Pattern - Model-View-ViewModel architecture with perfect layer separation
- Domain Context Pattern - Document-centric domain separation for multi-domain apps
- Composition Strategies - Advanced pattern composition for complex applications
- Context Splitting Patterns - Managing and splitting large contexts for scalability
Async Patterns
- Async Patterns - Asynchronous operation patterns and control flow
- Real-time State Access - Avoiding closure traps with store.getValue()
- Wait-Then-Execute - Safe DOM operations after element availability
- Conditional Await - Smart waiting based on conditions
- Timeout Protection - Preventing infinite waits with fallback strategies
Performance Patterns
- Performance Patterns - Performance optimization techniques and strategies
- Optimization Techniques - Store optimization, memoization, and RefContext performance
- Handler memory management and limits (see Action Register Patterns)
Debug Patterns
- Debug Patterns - Production debugging and troubleshooting patterns
- Production Debugging - Critical issues, state monitoring, error recovery, and stress testing
Quick Start Guide
| Pattern | Use Case | Import | Best For |
|---|---|---|---|
| 🎯 Action Only | Action dispatching with memory management | createActionContext | Event systems, command patterns, large applications |
| 🏪 Store Only | State management without actions | createStoreContext | Pure state management, data layers |
| 🔧 Ref Context (Advanced) | Direct DOM manipulation and singleton object management | createRefContext | High-performance UI, animations, external services |
Note: For complex applications, compose patterns together for maximum flexibility and separation of concerns.
Usage Guidelines
Each pattern includes:
- ✅ Best practices with working examples
- ❌ Common pitfalls to avoid
- 🎯 Use cases for when to apply the pattern
- ⚡ Performance considerations and optimization tips
Architecture Decision Guide
Single Domain Applications
- Simple Apps: Start with Store Only Pattern
- Interactive Apps: Add Action Only Pattern for business logic
- High-Performance Apps: Add RefContext Pattern (Advanced) for animations
- Complex Apps: Use MVVM Architecture for perfect layer separation
Multi-Domain Applications
- Team Boundaries: Use Domain Context Architecture for business separation
- Combined Approach: Apply MVVM Architecture within each business domain
- Enterprise Scale: Combine all patterns with proper domain isolation
Pattern Integration
These patterns can be combined for complex scenarios:
- Action Only + Store Only for complete business logic separation
- RefContext + Store Only for high-performance state-driven animations
- Core + Advanced Patterns + Domain Architecture for enterprise applications
- MVVM Architecture for perfect architectural layer separation