ExTermbox v1.0.2 ExTermbox.EventManager View Source
This module implements an event manager that notifies subscribers of keyboard, mouse and resize events received from the termbox API.
Internally, the event manager is managing a NIF-based polling routine and fanning out polled events to subscribers. It works likes this:
- The
ExTermbox.Bindings.start_polling/1
NIF is called with the event manager's pid. - The NIF creates a background thread for the blocking polling routine and immediately returns with a resource representing a handle for the thread.
- When the polling routine receives an event (e.g., a keypress), it sends a message to the event manager with the event data, and then continues polling for the next event.
- The event manager receives event data from the background thread and notifies all of its subscribers of the event. Steps 3 and 4 are repeated for each event.
- When the event manager is terminated,
ExTermbox.Bindings.stop_polling/0
is called to stop polling and terminate the background thread.
Example Usage:
def event_loop do
receive do
{:event, %Event{ch: ?q} = event} ->
Bindings.shutdown()
{:event, %Event{} = event} ->
# handle the event and wait for another...
event_loop()
end
end
{:ok, pid} = EventManager.start_link()
:ok = EventManager.subscribe(self())
event_loop()
Link to this section Summary
Functions
Returns a specification to start this module under a supervisor.
Starts an event manager process linked to the current process.
Subscribes the given subscriber pid to future event notifications.
Link to this section Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
Starts an event manager process linked to the current process.
Running multiple instances of the event manager process simultaneously is discouraged, as it could crash the NIF or cause unexpected behavior. By default, the process is registered with a fixed name to prevent this.
Subscribes the given subscriber pid to future event notifications.