Class: Hokusai::Publisher

Inherits:
Object
  • Object
show all
Defined in:
ui/src/hokusai/publisher.rb

Overview

An event emitter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(listeners = []) ⇒ Publisher

Returns a new instance of Publisher.



6
7
8
# File 'ui/src/hokusai/publisher.rb', line 6

def initialize(listeners = [])
  @listeners = listeners
end

Instance Attribute Details

#listenersObject (readonly)

Returns the value of attribute listeners.



4
5
6
# File 'ui/src/hokusai/publisher.rb', line 4

def listeners
  @listeners
end

Instance Method Details

#add(listener) ⇒ Object

Adds a listener that subscribes to events emitted by this publisher

Parameters:



15
16
17
# File 'ui/src/hokusai/publisher.rb', line 15

def add(listener)
  listeners << listener
end

#notify(name, *args, **kwargs) ⇒ Object

emits ‘event` with `**args` to all subscribers

Parameters:

  • name (String)

    the event name

  • the (**args)

    args to emit

See Also:



24
25
26
27
28
29
30
# File 'ui/src/hokusai/publisher.rb', line 24

def notify(name, *args, **kwargs)
  listeners.each do |listener|
    raise Hokusai::Error.new("No target `##{name}` on #{listener.class}") unless listener.respond_to?(name)

    listener.public_send(name, *args, **kwargs)
  end
end