Event System (EventBus)
Overview
The EventBus is FreeWorld OS's central event system, similar to OS message queues. It enables completely decoupled communication between components, allowing them to work together without direct dependencies.
Architecture: The EventBus follows a publish-subscribe pattern, similar to how Windows uses message queues and Linux uses event loops.
Key Features
- Decoupled Communication: Components communicate via events, not direct calls
- Event Types: Structured event types (e.g.,
input.mousedown,drag.start) - Priority System: Events can have priorities for ordered processing
- Async Processing: Events are queued and processed asynchronously
- Event Cancellation: Events can be cancelled or propagation stopped
- Global Listeners: Listen to all events for debugging/monitoring
Event Types
Input Events
input.mousedown- Mouse button pressedinput.mouseup- Mouse button releasedinput.mousemove- Mouse movedinput.click- Mouse clickedinput.dblclick- Mouse double-clickedinput.contextmenu- Right-click context menuinput.keydown- Key pressedinput.keyup- Key released
Drag & Drop Events
drag.start- Drag operation starteddrag.begin- Drag threshold crossed (actual drag)drag.move- Element being draggeddrag.enter- Entered drop targetdrag.leave- Left drop targetdrag.drop- Dropped on targetdrag.end- Drag operation ended
Window Events
window.create- Window createdwindow.close- Window closedwindow.focus- Window focusedwindow.blur- Window lost focus
Usage
Initialization
const eventBus = new EventBus();
window.freeworldEventBus = eventBus; // Make globally available
Subscribing to Events
// Subscribe to an event type
const unsubscribe = eventBus.on('input.mousedown', (event) => {
console.log('Mouse down at:', event.data.x, event.data.y);
});
// Subscribe with options
eventBus.on('drag.start', (event) => {
// Handle drag start
}, {
priority: 10, // Higher priority = processed first
once: false // Remove after first call
});
// Unsubscribe
unsubscribe();
Emitting Events
// Emit an event
eventBus.emit('input.mousedown', {
x: 100,
y: 200,
button: 'left',
target: element
}, {
source: 'InputManager',
cancelable: true
});
Global Listeners
// Listen to all events (for debugging)
eventBus.onAll((event) => {
console.log('Event:', event.type, event.data);
});
Integration with Components
InputManager
The InputManager captures all mouse/keyboard input and emits standardized events to the EventBus.
DragDropSystem
The DragDropSystem subscribes to input events and emits drag/drop events, completely decoupled from other components.
GUI Server
The GUI server subscribes to events it needs (drag events, context menu events) and responds accordingly.
Benefits
- Modularity: Components can be added/removed without breaking others
- Testability: Components can be tested in isolation
- Debugging: All events flow through one system, easy to monitor
- Extensibility: New components can subscribe to existing events
- OS-like Architecture: Similar to how real OSes handle messages/events