Class: Wpxf::EventEmitter

Inherits:
Object
  • Object
show all
Defined in:
lib/wpxf/core/event_emitter.rb

Overview

An event emitter that allows one or more subscribers.

Instance Method Summary collapse

Constructor Details

#initializeEventEmitter

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.

Parameters:

  • event (Object)

    the event object to emit.



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.

Parameters:

  • subscriber (Object)

    the event subscriber.



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.

Parameters:

  • subscriber (Object)

    the event subscriber.



18
19
20
# File 'lib/wpxf/core/event_emitter.rb', line 18

def unsubscribe(subscriber)
  @subscribers -= [subscriber]
end