Event Loop API
Kraken TUI provides an animation-aware async event loop viacreateLoop() that automatically adjusts polling behavior based on whether animations are active.
createLoop()
Create an animation-aware async event loop.Configuration for the event loop
Loop controller with
start() and stop() methodsBasic Usage
Animation-Aware Behavior
The loop automatically adapts its polling strategy:-
When animations are active (~60fps mode):
- Non-blocking input poll (
readInput(0)) - Sleep for frame duration (~16ms)
- Ensures smooth animation updates
- Non-blocking input poll (
-
When idle (CPU-saving mode):
- Blocking input poll (
readInput(idleTimeout)) - No unnecessary rendering
- Saves CPU when nothing is happening
- Blocking input poll (
The loop checks
app.getPerfCounter(PERF_ACTIVE_ANIMATIONS) to detect active animations automatically.LoopOptions
Configuration interface forcreateLoop().
The Kraken application instance
Called for each event during drain. Fires before JSX handler dispatch.Use this for:
- Global keyboard shortcuts
- Analytics/logging
- Custom event routing
Called each tick after events are drained, before render.Use this for:
- Game logic updates
- State computations
- Custom animations
FPS target when animating. Frame duration is calculated as
1000 / fps milliseconds.Input poll timeout (ms) when idle. Higher values save more CPU but increase input latency when idle.
Disable automatic dispatch to JSX event handler props. Set to
true if using only imperative event handling.Loop mode:
"onChange"(default): Auto-detects animations and switches between ~60fps and idle modes"continuous": Forces fixed-fps rendering regardless of animation state
Advanced Example
dispatchToJsxHandlers()
Dispatch an event to JSX event handler props registered on the target widget.The event to dispatch
createLoop().
Usage with Custom Loop
The
createLoop() function calls dispatchToJsxHandlers() automatically unless disableJsxDispatch: true is set.Event Handler Mapping
Event types are mapped to JSX prop names:| Event Type | JSX Prop Name |
|---|---|
key | onKey |
mouse | onMouse |
focus | onFocus |
change | onChange |
submit | onSubmit |
accessibility | onAccessibility |
Performance Counters
PERF_ACTIVE_ANIMATIONS
Performance counter ID for active animation count.createLoop() to detect when to switch between animation and idle modes.
Loop Patterns
Pattern 1: Simple Event Loop
For basic applications with no special requirements:Pattern 2: Global Shortcuts
Handle keyboard shortcuts before JSX handlers:Pattern 3: Game Loop
For games or simulations with per-frame updates:Pattern 4: Custom Event Routing
Route events to specific handlers based on application state:Comparison with Kraken.run()
createLoop() is an alternative to Kraken.run():
| Feature | Kraken.run() | createLoop() |
|---|---|---|
| Return value | No return (blocking) | Loop object with stop() |
| Animation detection | Automatic | Automatic |
| JSX dispatch | Automatic | Automatic (configurable) |
| Custom event handling | Via onEvent callback | Via onEvent callback |
| Async/await support | No | Yes |
| Stop mechanism | Return false from onEvent | Call loop.stop() |
Use
Kraken.run() for simple applications. Use createLoop() when you need explicit control over loop lifecycle or async patterns.Source Reference
Seets/src/loop.ts for implementation details.
Related APIs
Kraken.run()
Alternative event loop API
Event Handling
Event handling patterns and examples
Animation
Animation system concepts
JSX Reconciler
JSX event handler registration