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, event, callback) ⇒ Listener

Returns a new instance of Listener.



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

def initialize(klass, event, callback)
  @klass    = klass
  @event    = event
  @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



25
26
27
# File 'lib/volt/reactive/eventable.rb', line 25

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

#removeObject



15
16
17
18
19
20
21
22
23
# File 'lib/volt/reactive/eventable.rb', line 15

def remove
  @removed = true

  @klass.remove_listener(@event, self)

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