Patterns
Architectural patterns and best practices for building with Commiq.
Patterns
These patterns address recurring architectural decisions when building with Commiq. Each one is prescriptive — it reflects how the library is designed to be used and what works well in practice.
Use them as starting points. Copy the code, adapt the names, and apply the structure that fits your project.
Store File Structure
How to organize store files for clarity, testability, and scale.
Typed Command Factories
Replace magic strings with typed factory functions for type-safe command dispatch.
Domain Hooks
Encapsulate store interactions in a clean React hook API.
Async Loading States
A standard approach to async commands with loading and error handling.
Event-Driven Side Effects
Trigger notifications, navigation, and analytics from store events without coupling logic to views.
Real-Time Transports
Integrate WebSockets, Socket.IO, and Server-Sent Events with Commiq stores.
Testing Stores and Hooks
Unit test command handlers, event flows, React hooks, and effects.
Multi-Store Coordination
Coordinate independent stores through an event bus without coupling them directly.
Effects and Cancellation
Structured side effects with lifecycle control using the effects plugin and interruptable commands.
Error Handling and Recovery
Handle command errors, detect invalid commands, and implement retry patterns.
Optimistic Updates
Update state immediately for responsiveness and reconcile on async completion.
State Normalization
Structure store state for collections to simplify lookups, selectors, and updates.
Composing Plugins
Wire multiple plugins on the same store with correct initialization order and cleanup.
Command Validation
Validate command data with guards for business rules and assertions for developer invariants.
Dependency Injection
Provide typed dependencies to command handlers for testability and environment flexibility.
Deferred Cleanup
Guarantee resource cleanup in command handlers using ctx.defer, regardless of success or failure.