Timeouts and Handlers
This document explains how Scarpe manages timeouts and event handlers within its cooperative evented approach.
Timeout Management
Scarpe implements timeouts with these key considerations:
- Cooperative Nature
- Timeouts don’t interrupt execution
- They’re processed when the event loop is ready
- Timing is approximate, not exact
- Implementation
def after(milliseconds, &block)
register_timeout(Time.now + (milliseconds / 1000.0), block)
end
Event Handlers
Event handlers in Scarpe follow these principles:
- Registration
- Handlers are registered with specific events
- Multiple handlers can exist per event
- Order of execution is preserved
- Execution
- Handlers run in the main event loop
- They should be non-blocking when possible
- Long operations should be broken up
Restrictions
The cooperative approach has some important restrictions:
- No Preemption
- Long-running handlers block other events
- Timeouts may be delayed
- UI updates wait for handlers to complete
- Best Practices
- Keep handlers short
- Use async for long operations
- Break up complex tasks
Error Handling
Handlers include error management:
- Exception Catching
- Errors don’t crash the event loop
- Exceptions are logged appropriately
- Error handlers can be registered
- Recovery
- System can recover from handler errors
- State remains consistent
- Other handlers continue to work