Module: Tinker::Evented

Included in:
Context, Reactor
Defined in:
lib/tinker/evented.rb

Instance Method Summary collapse

Instance Method Details

#dispatch(event) ⇒ Object

Internal: Disatches an event

event - The event to dispatch to the listeners

Examples

@room.dispatch(Event.new("event_name", nil, :param1 => "value1"))


26
27
28
29
30
# File 'lib/tinker/evented.rb', line 26

def dispatch(event)
  _dispatchers[event.name].each do |callback|
    event_callback event, callback
  end
end

#every(time, *callbacks, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/tinker/evented.rb', line 8

def every(time, *callbacks, &block)
  callbacks << block if block
  EventMachine.add_periodic_timer(time) do
    env = Tinker::Event::Environment.new(nil, self)
    event = Tinker::Event.new("meta.timer.tick", env)
    callbacks.each do |callback|
      event_callback(event, callback)
    end
  end
end

#on(event_name, *callbacks, &block) ⇒ Object

Raises:

  • (ArgumentError)


2
3
4
5
6
# File 'lib/tinker/evented.rb', line 2

def on(event_name, *callbacks, &block)
  raise ArgumentError, 'No callbacks given' unless callbacks.any? || block
  _dispatchers[event_name].push(*callbacks) if callbacks.any?
  _dispatchers[event_name].push(&block) if block
end