class Rosegold::EventEmitter

Overview

Typed event subscriptions shared by Bot and Client.

Handlers run synchronously in the emitting fiber. Use spawn for work that waits for ticks or further packets so the reader can keep receiving events.

Direct Known Subclasses

Defined in:

rosegold/models/event_emitter.cr

Instance Method Summary

Instance Method Detail

def emit_event(event : Event) #

Calls handlers for this exact event type in registration order. Handler exceptions propagate to the caller.


[View source]
def event_handlers : Handlers #

Registered handlers, grouped by exact event type. Prefer #on and #off over modifying this registry directly.


[View source]
def off(event_type : T.class, id : UUID) forall T #

Removes the handler identified by id for event_type. Does nothing if the handler is already gone.


[View source]
def on(event_type : T.class, id : UUID = UUID.random, &block : T -> ) forall T #

Registers a handler and returns its UUID for removal with #off. Subscriptions match the exact event type, not its subclasses.

id = bot.on(Rosegold::Event::HealthChanged) { |event| puts event.health }
bot.off(Rosegold::Event::HealthChanged, id)

[View source]
def once(event_type : T.class, &block : T -> ) forall T #

Registers a handler for the next matching event and returns its UUID. Removes it before invoking the block, even if the block raises or emits again.


[View source]
def wait_for(event_type : T.class, timeout : Time::Span = 5.seconds) forall T #

Waits for the next matching event, or returns nil after timeout. Always removes its temporary listener before returning.


[View source]
def wait_for(event_type : T.class, timeout : Time::Span = 5.seconds, &) forall T #

Registers a listener, runs the block, then waits for the matching event. Returns the event; raises on timeout. The timeout includes time in the block, but cannot interrupt it. The listener is removed even if the block raises.

The block triggers the action; it is not an event filter. Any event of the requested type can satisfy the wait, including an unrelated server message.


[View source]