Class: Wpxf::EventEmitter
- Inherits:
-
Object
- Object
- Wpxf::EventEmitter
- Defined in:
- lib/wpxf/core/event_emitter.rb
Overview
An event emitter that allows one or more subscribers.
Instance Method Summary collapse
-
#emit(event) ⇒ Object
Emit an event to be handled by each subscriber.
-
#initialize ⇒ EventEmitter
constructor
A new instance of EventEmitter.
-
#subscribe(subscriber) ⇒ Object
Subscribe to the events emitted by this EventEmitter.
-
#unsubscribe(subscriber) ⇒ Object
Unsubscribe from the events emitted by this EventEmitter.
Constructor Details
#initialize ⇒ EventEmitter
Returns a new instance of EventEmitter
6 7 8 |
# File 'lib/wpxf/core/event_emitter.rb', line 6 def initialize @subscribers = [] end |
Instance Method Details
#emit(event) ⇒ Object
Emit an event to be handled by each subscriber.
24 25 26 27 28 |
# File 'lib/wpxf/core/event_emitter.rb', line 24 def emit(event) @subscribers.each do |s| s.on_event_emitted(event) if s.respond_to? 'on_event_emitted' end end |
#subscribe(subscriber) ⇒ Object
Subscribe to the events emitted by this Wpxf::EventEmitter.
12 13 14 |
# File 'lib/wpxf/core/event_emitter.rb', line 12 def subscribe(subscriber) @subscribers.push(subscriber) end |
#unsubscribe(subscriber) ⇒ Object
Unsubscribe from the events emitted by this Wpxf::EventEmitter.
18 19 20 |
# File 'lib/wpxf/core/event_emitter.rb', line 18 def unsubscribe(subscriber) @subscribers -= [subscriber] end |