Class: Kitsune::Kit::Events::Bus

Inherits:
Object
  • Object
show all
Defined in:
lib/kitsune/kit/events.rb

Instance Method Summary collapse

Constructor Details

#initializeBus

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

Raises:

  • (ArgumentError)


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