Class: Kitsune::Kit::Events::Bus
- Inherits:
-
Object
- Object
- Kitsune::Kit::Events::Bus
- Defined in:
- lib/kitsune/kit/events.rb
Instance Method Summary collapse
-
#initialize ⇒ Bus
constructor
A new instance of Bus.
- #publish(event) ⇒ Object
- #subscribe(subscriber = nil, &block) ⇒ Object
Constructor Details
#initialize ⇒ Bus
Returns a new instance of Bus.
28 29 30 31 |
# File 'lib/kitsune/kit/events.rb', line 28 def initialize @subscribers = [] @mutex = Mutex.new end |
Instance Method Details
#publish(event) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/kitsune/kit/events.rb', line 41 def publish(event) @mutex.synchronize { @subscribers.dup }.each do |subscriber| subscriber.respond_to?(:call) ? subscriber.call(event) : subscriber.handle(event) end event end |
#subscribe(subscriber = nil, &block) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/kitsune/kit/events.rb', line 33 def subscribe(subscriber = nil, &block) target = subscriber || block raise ArgumentError, "subscriber or block is required" unless target @mutex.synchronize { @subscribers << target } target end |