Class: Listener

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

Overview

A listener gets returned when adding an ‘on’ event listener. It can be used to clear the event listener.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, event, scope_provider, callback) ⇒ Listener

Returns a new instance of Listener.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/volt/reactive/events.rb', line 10

def initialize(klass, event, scope_provider, callback)
  @klass = klass
  @event = event
  @scope_provider = scope_provider
  @callback = callback

  if DEBUG && RUBY_PLATFORM == 'opal'
    # puts "e: #{event} on #{klass.inspect}"
    @@all_events ||= []
    @@all_events << self

    # counts = {}
    # @@all_events.each do |ev|
    #   scope = (ev.scope_provider && ev.scope_provider.scope) || nil
    #
    #   # puts `typeof(scope)`
    #   if `typeof(scope) !== 'undefined'`
    #     counts[scope] ||= 0
    #     counts[scope] += 1
    #   end
    # end
    #
    # puts counts.inspect

    `window.total_listeners = window.total_listeners || 0;`
    `window.total_listeners += 1;`
    `console.log(window.total_listeners);`
  end
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/volt/reactive/events.rb', line 8

def klass
  @klass
end

#scope_providerObject (readonly)

Returns the value of attribute scope_provider.



8
9
10
# File 'lib/volt/reactive/events.rb', line 8

def scope_provider
  @scope_provider
end

Instance Method Details

#call(*args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/volt/reactive/events.rb', line 48

def call(*args)
  if @removed
    # puts "Triggered on a removed event: #{@event}"
    return
  end

  if @klass.reactive?
    # Update the reactive value's current value to let it know it is being
    # followed.
    @klass.update_followers if @klass.respond_to?(:update_followers)
  end

  @callback.call(*args)
end

#inspectObject



91
92
93
# File 'lib/volt/reactive/events.rb', line 91

def inspect
  "<Listener:#{object_id} event=#{@event} scope=#{scope.inspect}#{' internal' if internal?}>"
end

#internal?Boolean

Returns:



40
41
42
# File 'lib/volt/reactive/events.rb', line 40

def internal?
  @internal
end

#removeObject

Removes the listener from where ever it was created.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/volt/reactive/events.rb', line 64

def remove
  if @removed
    # raise "event #{@event} already removed"
    puts "event #{@event} already removed"
    return
  end

  if DEBUG && RUBY_PLATFORM == 'opal'
    @@all_events.delete(self) if @@all_events

    `window.total_listeners -= 1;`
    `console.log("Rem", window.total_listeners);`
  end


  @removed = true
  @klass.remove_listener(@event, self)

  # We need to clear these references to free the memory
  @scope_provider = nil
  @callback = nil
  # @klass2 = @klass
  @klass = nil
  # @event = nil

end

#scopeObject



44
45
46
# File 'lib/volt/reactive/events.rb', line 44

def scope
  @scope_provider && scope_provider.respond_to?(:scope) && @scope_provider.scope
end