Class: Conductor::Worker::Events::SyncEventDispatcher
- Inherits:
-
Object
- Object
- Conductor::Worker::Events::SyncEventDispatcher
- Defined in:
- lib/conductor/worker/events/sync_event_dispatcher.rb
Overview
Thread-safe synchronous event dispatcher Dispatches events to registered listeners in the calling thread Listener exceptions are isolated and logged, never propagating to callers
Instance Method Summary collapse
-
#clear ⇒ self
Clear all listeners (primarily for testing).
-
#has_listeners?(event_type) ⇒ Boolean
Check if there are listeners registered for an event type.
-
#initialize ⇒ SyncEventDispatcher
constructor
A new instance of SyncEventDispatcher.
-
#listener_count(event_type) ⇒ Integer
Get the number of listeners for an event type.
-
#publish(event) ⇒ self
Publish an event to all registered listeners Listeners are called synchronously in the calling thread Exceptions in listeners are caught and logged, not propagated.
-
#register(event_type, listener) ⇒ self
Register a listener for an event type.
-
#unregister(event_type, listener) ⇒ self
Unregister a listener for an event type.
Constructor Details
#initialize ⇒ SyncEventDispatcher
Returns a new instance of SyncEventDispatcher.
10 11 12 13 |
# File 'lib/conductor/worker/events/sync_event_dispatcher.rb', line 10 def initialize @listeners = Hash.new { |h, k| h[k] = [] } @mutex = Mutex.new end |
Instance Method Details
#clear ⇒ self
Clear all listeners (primarily for testing)
71 72 73 74 |
# File 'lib/conductor/worker/events/sync_event_dispatcher.rb', line 71 def clear @mutex.synchronize { @listeners.clear } self end |
#has_listeners?(event_type) ⇒ Boolean
Check if there are listeners registered for an event type
58 59 60 |
# File 'lib/conductor/worker/events/sync_event_dispatcher.rb', line 58 def has_listeners?(event_type) @mutex.synchronize { @listeners[event_type].any? } end |
#listener_count(event_type) ⇒ Integer
Get the number of listeners for an event type
65 66 67 |
# File 'lib/conductor/worker/events/sync_event_dispatcher.rb', line 65 def listener_count(event_type) @mutex.synchronize { @listeners[event_type].size } end |
#publish(event) ⇒ self
Publish an event to all registered listeners Listeners are called synchronously in the calling thread Exceptions in listeners are caught and logged, not propagated
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/conductor/worker/events/sync_event_dispatcher.rb', line 42 def publish(event) listeners = @mutex.synchronize { @listeners[event.class].dup } listeners.each do |listener| listener.call(event) rescue StandardError => e # Listener failure is isolated - never breaks the worker warn "[Conductor] Event listener error for #{event.class}: #{e.}" end self end |
#register(event_type, listener) ⇒ self
Register a listener for an event type
19 20 21 22 23 24 |
# File 'lib/conductor/worker/events/sync_event_dispatcher.rb', line 19 def register(event_type, listener) @mutex.synchronize do @listeners[event_type] << listener unless @listeners[event_type].include?(listener) end self end |
#unregister(event_type, listener) ⇒ self
Unregister a listener for an event type
30 31 32 33 34 35 |
# File 'lib/conductor/worker/events/sync_event_dispatcher.rb', line 30 def unregister(event_type, listener) @mutex.synchronize do @listeners[event_type].delete(listener) end self end |