Class: Volt::Listener

Inherits:
Object show all
Defined in:
lib/volt/reactive/eventable.rb

Overview

Listeners are returned from #on on a class with Eventable included. Listeners can be stopped by calling #remove

Instance Method Summary collapse

Constructor Details

#initialize(klass, events, callback) ⇒ Listener

Returns a new instance of Listener.



5
6
7
8
9
# File 'lib/volt/reactive/eventable.rb', line 5

def initialize(klass, events, callback)
  @klass    = klass
  @events    = events
  @callback = callback
end

Instance Method Details

#call(*args) ⇒ Object



11
12
13
# File 'lib/volt/reactive/eventable.rb', line 11

def call(*args)
  @callback.call(*args) unless @removed
end

#inspectObject



33
34
35
# File 'lib/volt/reactive/eventable.rb', line 33

def inspect
  "<Listener:#{object_id} events=#{@events}>"
end

#instance_call(instance, *args) ⇒ Object

Call the callback with self set to instance



16
17
18
# File 'lib/volt/reactive/eventable.rb', line 16

def instance_call(instance, *args)
  instance.instance_exec(*args, &@callback)
end

#removeObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/volt/reactive/eventable.rb', line 20

def remove
  raise "Listener has already been removed" if @removed
  @removed = true

  @events.each do |event|
    @klass.remove_listener(event, self)
  end

  # Make things easier on the GC
  @klass    = nil
  @callback = nil
end