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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, events, callback) ⇒ Listener

Returns a new instance of Listener.



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

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

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



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

def events
  @events
end

Instance Method Details

#call(*args) ⇒ Object



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

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

#inspectObject



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

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

#instance_call(instance, *args) ⇒ Object

Call the callback with self set to instance



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

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

#removeObject



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

def remove
  fail '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