Class: Hokusai::Publisher
- Inherits:
-
Object
- Object
- Hokusai::Publisher
- Defined in:
- ui/src/hokusai/publisher.rb
Overview
An event emitter
Instance Attribute Summary collapse
-
#listeners ⇒ Object
readonly
Returns the value of attribute listeners.
Instance Method Summary collapse
-
#add(listener) ⇒ Object
Adds a listener that subscribes to events emitted by this publisher.
-
#initialize(listeners = []) ⇒ Publisher
constructor
A new instance of Publisher.
-
#notify(name, *args, **kwargs) ⇒ Object
emits ‘event` with `**args` to all subscribers.
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
#listeners ⇒ Object (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
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
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 |