Class: Conductor::Worker::Events::SyncEventDispatcher

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeSyncEventDispatcher

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

#clearself

Clear all listeners (primarily for testing)

Returns:

  • (self)


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

Parameters:

  • event_type (Class)

    Event class

Returns:

  • (Boolean)


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

Parameters:

  • event_type (Class)

    Event class

Returns:

  • (Integer)


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

Parameters:

Returns:

  • (self)


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.message}"
  end

  self
end

#register(event_type, listener) ⇒ self

Register a listener for an event type

Parameters:

  • event_type (Class)

    Event class to listen for

  • listener (Proc, #call)

    Callable to invoke when event is published

Returns:

  • (self)


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

Parameters:

  • event_type (Class)

    Event class

  • listener (Proc, #call)

    Listener to remove

Returns:

  • (self)


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