Module: RubyPi::Agent::EventEmitter
- Included in:
- Core
- Defined in:
- lib/ruby_pi/agent/events.rb
Overview
Mixin that adds event subscription and emission to any class. Include this module and call ‘on`, `emit`, and `off` to wire up event-driven communication between components.
Instance Method Summary collapse
-
#emit(event, data = {}) ⇒ void
Fires all handlers registered for the given event type.
-
#off(event, &block) ⇒ Proc?
Removes a specific handler from an event’s subscriber list.
-
#on(event, &block) ⇒ Proc
Subscribes a handler block to a specific event type.
Instance Method Details
#emit(event, data = {}) ⇒ void
This method returns an undefined value.
Fires all handlers registered for the given event type. Each handler receives the ‘data` hash as its argument. Handlers that raise are rescued individually — one failing handler does not prevent others from executing.
If a handler raises during a non-error event, the error is re-emitted as an :error event so subscribers can observe it. To prevent infinite recursion, errors raised inside :error event handlers are silently swallowed — they are not re-emitted. This ensures that a broken error handler cannot crash the agent loop.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ruby_pi/agent/events.rb', line 84 def emit(event, data = {}) validate_event!(event) event_handlers[event].each do |handler| handler.call(data) rescue StandardError => e # Guard against infinite recursion: if we are already emitting an # :error event and the error handler itself raises, we must not # re-emit — that would cause unbounded recursion. Silently discard # the secondary error instead. if event != :error emit(:error, error: e, source: :event_handler, event: event) end end end |
#off(event, &block) ⇒ Proc?
Removes a specific handler from an event’s subscriber list. If the handler is not found, this is a no-op. Pass the same block reference that was given to ‘on`.
106 107 108 109 |
# File 'lib/ruby_pi/agent/events.rb', line 106 def off(event, &block) validate_event!(event) event_handlers[event].delete(block) end |
#on(event, &block) ⇒ Proc
Subscribes a handler block to a specific event type. The block will be called every time ‘emit` fires for that event. Multiple handlers can be registered for the same event — they are invoked in the order they were registered.
64 65 66 67 68 |
# File 'lib/ruby_pi/agent/events.rb', line 64 def on(event, &block) validate_event!(event) event_handlers[event] << block block end |