Class: NtiEventBus::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/nti_event_bus/registry.rb

Overview

Maps event names to the ordered, de-duplicated list of handler classes subscribed to them.

Built once during NtiEventBus.setup! and then deep-frozen via #finalize!, which makes reads (#handlers_for) lock-free and allocation-free on the hot path.

Constant Summary collapse

EMPTY =
[].freeze

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



11
12
13
# File 'lib/nti_event_bus/registry.rb', line 11

def initialize
  @handlers = Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#add(event_name, handler) ⇒ Object



15
16
17
18
# File 'lib/nti_event_bus/registry.rb', line 15

def add(event_name, handler)
  list = @handlers[event_name]
  list << handler unless list.include?(handler)
end

#finalize!Object



20
21
22
23
24
# File 'lib/nti_event_bus/registry.rb', line 20

def finalize!
  @handlers.each_value(&:freeze)
  @handlers.freeze
  self
end

#handlers_for(event_name) ⇒ Object



26
27
28
# File 'lib/nti_event_bus/registry.rb', line 26

def handlers_for(event_name)
  @handlers.fetch(event_name, EMPTY)
end