Event Loops
Scarpeās event loop system is a core part of its architecture, handling both UI events and application logic.
Main Event Loop
The main event loop is responsible for:
- Processing user input
- Handling timer events
- Managing display updates
- Coordinating between components
Implementation Details
The event loop is implemented with these key features:
Event Queue
- Events are queued in order of arrival
- Priority system for critical events
- Efficient processing of batched events
Handler Registration
class EventLoop
def register_handler(event_type, &block)
handlers[event_type] << block
end
end
Event Processing
- Events are processed in a single thread
- Non-blocking operations where possible
- Careful handling of long-running tasks
Cooperative Multitasking
Scarpe uses cooperative multitasking where:
- Tasks yield control voluntarily
- Long operations are broken into smaller chunks
- The event loop maintains responsiveness
Best Practices
When working with the event loop:
- Keep handlers fast and focused
- Avoid blocking operations
- Use async operations for long-running tasks
- Handle errors appropriately
Integration with Display Service
The event loop coordinates closely with the display service:
- Synchronizes state updates
- Manages display refresh timing
- Handles animation frames
- Coordinates user input