Module: BubbleWrap::Reactor::Eventable

Included in:
PeriodicTimer, Timer
Defined in:
motion/reactor/eventable.rb

Overview

A simple mixin that adds events to your object.

Instance Method Summary collapse

Instance Method Details

#off(event, method = nil, &blk) ⇒ Object

When `event` is triggered, do not call the given block any more



17
18
19
20
21
22
23
24
25
26
27
# File 'motion/reactor/eventable.rb', line 17

def off(event, method = nil, &blk)
  events = _events_for_key(event)
  if method
    events.delete_if { |m| m.receiver == method.receiver and m.name == method.name }
  elsif blk
    events.delete_if { |b| b == blk }
  else
    __events__[event] = Array.new
  end
  blk
end

#on(event, method = nil, &blk) ⇒ Object

When `event` is triggered the block will execute and be passed the arguments that are passed to `trigger`.



9
10
11
12
13
# File 'motion/reactor/eventable.rb', line 9

def on(event, method = nil, &blk)
  events = _events_for_key(event)
  method_or_block = method ? method : blk
  events.push method_or_block
end

#trigger(event, *args) ⇒ Object

Trigger an event



30
31
32
33
34
35
# File 'motion/reactor/eventable.rb', line 30

def trigger(event, *args)
  blks = _events_for_key(event).clone
  blks.map do |blk|
    blk.call(*args)
  end
end