Class: TCB::EventBus::SubscriberRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/tcb/event_bus/subscriber_registry.rb

Defined Under Namespace

Classes: Subscription

Instance Method Summary collapse

Constructor Details

#initializeSubscriberRegistry

Returns a new instance of SubscriberRegistry.



8
9
10
11
12
# File 'lib/tcb/event_bus/subscriber_registry.rb', line 8

def initialize
  @subscribers = Hash.new { |h, k| h[k] = Set.new }
  @metadata = {}
  @mutex = Mutex.new
end

Instance Method Details

#add(event_class, handler) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/tcb/event_bus/subscriber_registry.rb', line 14

def add(event_class, handler)
  subscription = Subscription.new(event_class: event_class, handler: handler)
  @mutex.synchronize do
    @subscribers[event_class].add(handler)
    @metadata[handler.object_id] = SubscriberMetadataExtractor.new(handler).extract
  end
  subscription
end

#clearObject



38
39
40
41
42
43
# File 'lib/tcb/event_bus/subscriber_registry.rb', line 38

def clear
  @mutex.synchronize do
    @subscribers.clear
    @metadata.clear
  end
end

#handlers_for(event_class) ⇒ Object



30
31
32
# File 'lib/tcb/event_bus/subscriber_registry.rb', line 30

def handlers_for(event_class)
  @mutex.synchronize { @subscribers[event_class].dup.freeze }
end

#metadata_for(subscription) ⇒ Object



34
35
36
# File 'lib/tcb/event_bus/subscriber_registry.rb', line 34

def (subscription)
  @mutex.synchronize { @metadata[subscription.handler.object_id] }
end

#remove(subscription) ⇒ Object



23
24
25
26
27
28
# File 'lib/tcb/event_bus/subscriber_registry.rb', line 23

def remove(subscription)
  @mutex.synchronize do
    @subscribers[subscription.event_class].delete(subscription.handler)
    @metadata.delete(subscription.handler.object_id)
  end
end