Class: Exa::Instrumentation::EventRegistry
- Inherits:
-
Object
- Object
- Exa::Instrumentation::EventRegistry
- Defined in:
- lib/exa/instrumentation.rb
Overview
Thread-safe event registry for subscribing to and emitting API events. Supports wildcard pattern matching (e.g., 'exa.request.*').
Instance Method Summary collapse
-
#clear_listeners ⇒ Object
Clear all listeners.
-
#initialize ⇒ EventRegistry
constructor
A new instance of EventRegistry.
-
#listener_count ⇒ Object
Returns the count of registered listeners.
-
#notify(event_name, payload) ⇒ Object
Emit an event to all matching subscribers.
-
#subscribe(pattern) {|event_name, payload| ... } ⇒ String
Subscribe to events matching a pattern.
-
#unsubscribe(subscription_id) ⇒ Object
Unsubscribe from events.
Constructor Details
#initialize ⇒ EventRegistry
Returns a new instance of EventRegistry.
10 11 12 13 |
# File 'lib/exa/instrumentation.rb', line 10 def initialize @listeners = {} @mutex = Mutex.new end |
Instance Method Details
#clear_listeners ⇒ Object
Clear all listeners.
42 43 44 45 46 |
# File 'lib/exa/instrumentation.rb', line 42 def clear_listeners @mutex.synchronize do @listeners.clear end end |
#listener_count ⇒ Object
Returns the count of registered listeners.
67 68 69 |
# File 'lib/exa/instrumentation.rb', line 67 def listener_count @mutex.synchronize { @listeners.size } end |
#notify(event_name, payload) ⇒ Object
Emit an event to all matching subscribers.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/exa/instrumentation.rb', line 51 def notify(event_name, payload) # Take a snapshot of current listeners to avoid holding the mutex during execution matching_listeners = @mutex.synchronize do @listeners.select do |_id, listener| pattern_matches?(listener[:pattern], event_name) end.dup end matching_listeners.each do |_id, listener| listener[:block].call(event_name, payload) rescue StandardError # Silently ignore listener errors to avoid breaking the main flow end end |
#subscribe(pattern) {|event_name, payload| ... } ⇒ String
Subscribe to events matching a pattern.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/exa/instrumentation.rb', line 19 def subscribe(pattern, &block) return unless block_given? subscription_id = SecureRandom.uuid @mutex.synchronize do @listeners[subscription_id] = { pattern: pattern, block: block } end subscription_id end |
#unsubscribe(subscription_id) ⇒ Object
Unsubscribe from events.
35 36 37 38 39 |
# File 'lib/exa/instrumentation.rb', line 35 def unsubscribe(subscription_id) @mutex.synchronize do @listeners.delete(subscription_id) end end |