Module: CultomePlayer::Events

Included in:
CultomePlayer
Defined in:
lib/cultome_player/events.rb

Instance Method Summary collapse

Instance Method Details

#emit_event(event, *data) ⇒ Object

Broadcast an event to all the registered listeners.

Parameters:

  • event (Symbol)

    The event name.

  • data (Array)

    The information sended to the listeners.



25
26
27
# File 'lib/cultome_player/events.rb', line 25

def emit_event(event, *data)
  listeners[event].collect{|l| l.call(*data) }
end

#listenersHash

Lazy getter of registered event listeners.

Returns:

  • (Hash)

    With event names as the keys and values are the listeners registered to that event.



7
8
9
# File 'lib/cultome_player/events.rb', line 7

def listeners
  @listeners ||= Hash.new{|h,k| h[k] = [] }
end

#register_listener(event, &callback) ⇒ Object

Register a callback to an event.

Parameters:

  • event (Symbol)

    The event name.

  • callback (Object)

    Implements a callback with the name on_<event name>.

Returns:

  • (Object)

    The registered callback.



16
17
18
19
# File 'lib/cultome_player/events.rb', line 16

def register_listener(event, &callback)
  listeners[event] << callback
  return callback
end