Module: Events

Included in:
ComponentNode, ReactiveManager, ReactiveTemplate
Defined in:
lib/volt/reactive/events.rb

Instance Method Summary collapse

Instance Method Details

#event_chainObject



128
129
130
# File 'lib/volt/reactive/events.rb', line 128

def event_chain
  @event_chain ||= EventChain.new(self)
end

#listenersObject



132
133
134
# File 'lib/volt/reactive/events.rb', line 132

def listeners
  @listeners || {}
end

#on(event, scope_provider = nil, &block) ⇒ Object

Add a listener for an event



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/volt/reactive/events.rb', line 102

def on(event, scope_provider=nil, &block) 
  # puts "Register: #{event} on #{self.inspect}"   
  event = event.to_sym
  
  new_listener = Listener.new(self, event, scope_provider, block)
  
  @listeners ||= {}
  @listeners[event] ||= []
  @listeners[event] << new_listener

  first = @listeners[event].size == 1
  
  # When events get added, we need to notify event chains so they
  # can update and chain any new events.
  event_chain.add_event(event) if first

  # Let the included class know that an event was registered. (if it cares)
  if self.respond_to?(:event_added)
    # call event added passing the event, the scope, and a boolean if it
    # is the first time this event has been added.
    self.event_added(event, scope_provider, first)
  end

  return new_listener
end

#remove_listener(event, listener) ⇒ Object

Typically you would call .remove on the listener returned from the .on method. However, here you can also pass in the original proc to remove a listener



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/volt/reactive/events.rb', line 139

def remove_listener(event, listener)
  event = event.to_sym
  
  raise "Unable to delete #{event} from #{self.inspect}" unless @listeners && @listeners[event]
  
  # if @listeners && @listeners[event]
    @listeners[event].delete(listener)

    no_more_events = @listeners[event].size == 0
    if no_more_events
      # When events are removed, we need to notify any relevent chains so they
      # can remove any chained events.
      event_chain.remove_event(event)

      # No registered listeners now on this event
      @listeners.delete(event)
    end

    # Let the class we're included on know that we removed a listener (if it cares)
    if self.respond_to?(:event_removed)
      # Pass in the event and a boolean indicating if it is the last event
      self.event_removed(event, no_more_events)
    end
  # end
end

#trigger!(event, filter = nil, *args) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/volt/reactive/events.rb', line 165

def trigger!(event, filter=nil, *args)
  are_reactive = reactive?
  # puts "TRIGGER FOR: #{event} on #{self.inspect}" if !reactive?
  ObjectTracker.process_queue if !are_reactive# && !respond_to?(:skip_current_queue_flush)
  
  event = event.to_sym
  
  if @listeners && @listeners[event]
    # puts "LISTENERS FOR #{event} on #{self.inspect} - #{@listeners[event].inspect}"
    # TODO: We have to dup here because one trigger might remove another
    @listeners[event].dup.each do |listener|
      # Call the event on each listener
      # If there is no listener, that means another event trigger removed it.
      # If there is no filter, call
      # if we aren't reactive, we should pass to all of our reactive listeners, since they
      # just proxy us.
      # If the filter exists, check it
      # puts "CHECK #{listener.inspect} : #{self.inspect} -- #{listener.klass.inspect}"   
      if (!filter || (!are_reactive && listener.scope_provider.reactive?) || filter.call(listener.scope))
        listener.call(filter, *args)
      end
    end
  end

  nil
end

#trigger_by_scope!(event, *args, &block) ⇒ Object

Takes a block, which passes in



193
194
195
# File 'lib/volt/reactive/events.rb', line 193

def trigger_by_scope!(event, *args, &block)
  trigger!(event, block, *args)
end