Class: Listener
Overview
A listener gets returned when adding an ‘on’ event listener. It can be used to clear the event listener.
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#scope_provider ⇒ Object
readonly
Returns the value of attribute scope_provider.
Instance Method Summary collapse
- #call(*args) ⇒ Object
-
#initialize(klass, event, scope_provider, callback) ⇒ Listener
constructor
A new instance of Listener.
- #inspect ⇒ Object
- #internal? ⇒ Boolean
-
#remove ⇒ Object
Removes the listener from where ever it was created.
- #scope ⇒ Object
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
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
8 9 10 |
# File 'lib/volt/reactive/events.rb', line 8 def klass @klass end |
#scope_provider ⇒ Object (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 |
#inspect ⇒ Object
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
40 41 42 |
# File 'lib/volt/reactive/events.rb', line 40 def internal? @internal end |
#remove ⇒ Object
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 |
#scope ⇒ Object
44 45 46 |
# File 'lib/volt/reactive/events.rb', line 44 def scope @scope_provider && scope_provider.respond_to?(:scope) && @scope_provider.scope end |