Class: Synapse::EventBus::SimpleEventBus
- Defined in:
- lib/synapse/event_bus/simple_event_bus.rb
Overview
Implementation of an event bus that notifies any subscribed event listeners in the calling thread. Listeners are expected to implement asynchronous handing themselves, if desired.
Instance Method Summary collapse
-
#initialize ⇒ SimpleEventBus
constructor
A new instance of SimpleEventBus.
- #publish(*events) ⇒ undefined
- #subscribe(listener) ⇒ undefined
- #unsubscribe(listener) ⇒ undefined
Constructor Details
#initialize ⇒ SimpleEventBus
Returns a new instance of SimpleEventBus.
6 7 8 9 |
# File 'lib/synapse/event_bus/simple_event_bus.rb', line 6 def initialize @listeners = Set.new @logger = Logging.logger[self.class] end |
Instance Method Details
#publish(*events) ⇒ undefined
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/synapse/event_bus/simple_event_bus.rb', line 13 def publish(*events) if @listeners.empty? return end events.flatten! events.each do |event| @listeners.each do |listener| if @logger.debug? listener_type = actual_type listener @logger.debug 'Dispatching event [%s] to listener [%s]' % [event.payload_type, listener_type] end listener.notify event end end end |
#subscribe(listener) ⇒ undefined
34 35 36 37 38 39 40 41 42 |
# File 'lib/synapse/event_bus/simple_event_bus.rb', line 34 def subscribe(listener) listener_type = actual_type listener if @listeners.add? listener @logger.debug 'Event listener [%s] subscribed' % listener_type else @logger.info 'Event listener [%s] not added, was already subscribed' % listener_type end end |
#unsubscribe(listener) ⇒ undefined
46 47 48 49 50 51 52 53 54 |
# File 'lib/synapse/event_bus/simple_event_bus.rb', line 46 def unsubscribe(listener) listener_type = actual_type listener if @listeners.delete? listener @logger.debug 'Event listener [%s] unsubscribed' % listener_type else @logger.info 'Event listener [%s] not removed, was not subscribed' % listener_type end end |