Global Event Bus
The DeployStack frontend implements a global event bus system using the mitt library to enable efficient cross-component communication. This system provides immediate updates across components without requiring direct parent-child relationships or complex state management.Overview
The event bus solves common frontend challenges such as:- Cross-component communication between unrelated components
- Immediate UI updates when data changes in different parts of the application
- Cache invalidation and data synchronization
- Decoupled architecture for better maintainability
Architecture
Event Bus Setup
The event bus is configured globally inmain.ts
using Vue 3’s provide/inject pattern:
Type Safety
All events are strictly typed using TypeScript interfaces:Storage Integration
The event bus includes built-in storage capabilities for persistent state management. When you use storage methods, they automatically emit events for reactive updates.Storage Events
The storage system emitsstorage-changed
events whenever data is modified:
📖 For detailed storage usage, see Frontend Storage System
Usage
Basic Implementation
1. Using the Composable
2. Component Lifecycle Management
Real-World Examples
Example 1: Team Management System
This example shows how the sidebar automatically updates when teams are created from the teams page.Emitting Events (Team Creation)
Listening for Events (Sidebar Updates)
Example 2: Notification System
Triggering Notifications
Example 3: MCP Server Deployment Status
Event Naming Convention
The event bus follows a consistent naming pattern to ensure clarity and maintainability across the application.Standard Pattern
Examples
Naming Guidelines
- Use kebab-case: All event names should use lowercase with hyphens
- Feature first: Start with the feature or domain name
- Action second: End with the specific action or state change
- Be specific: Avoid generic names like ‘update’ or ‘change’ without context
- Past tense for completed actions: Use ‘created’, ‘updated’, ‘deleted’ for completed operations
- Present tense for commands: Use ‘show’, ‘hide’, ‘clear’ for immediate actions
Best Practices
1. Event Type Definition
Always define event types in theEventBusEvents
interface for type safety:
2. Memory Management
Always clean up event listeners to prevent memory leaks:3. Error Handling
Wrap event handlers in try-catch blocks:4. Debugging Events
Add logging for development:Common Patterns
1. Data Synchronization
2. Cache Invalidation
3. UI State Updates
Performance Considerations
1. Event Frequency
Be mindful of high-frequency events:2. Event Data Size
Keep event payloads small:Testing
Unit Testing Events
Migration Guide
From Direct Component Communication
Before:Adding New Events
- Define the event type:
- Emit the event:
- Listen for the event:
Troubleshooting
Common Issues
- Events not firing: Check if the event name matches exactly
- Memory leaks: Ensure
eventBus.off()
is called inonUnmounted()
- TypeScript errors: Verify event types are defined in
EventBusEvents
- Handler not called: Check if the listener was registered before the event was emitted
Debugging Tips
Related Documentation
- Frontend Storage System - Persistent state management with automatic event emission