Module: Events

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

Instance Method Summary collapse

Instance Method Details

#event_chainObject



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

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

#has_listeners?Boolean

Returns:



135
136
137
# File 'lib/volt/reactive/events.rb', line 135

def has_listeners?
  @has_listeners
end

#listenersObject



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

def listeners
  @listeners || {}
end

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

Add a listener for an event



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

def on(event, scope_provider=nil, &block)

  event = event.to_sym

  @has_listeners = true

  new_listener = Listener.new(self, event, scope_provider, block)

  @listeners ||= {}
  @listeners[event] ||= []
  @listeners[event] << new_listener

  first_for_event = @listeners[event].size == 1
  first = first_for_event && @listeners.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_for_event

  # 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, first_for_event)
  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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/volt/reactive/events.rb', line 142

def remove_listener(event, listener)
  event = event.to_sym

  raise "Unable to delete #{event} from #{self.inspect}" unless @listeners && @listeners[event]

  @listeners[event].delete(listener)

  last_for_event = @listeners[event].size == 0

  if last_for_event
    # 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

  last = last_for_event && @listeners.size == 0

  # 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, last, last_for_event)
  end

  if last
    @has_listeners = nil
  end
end

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/volt/reactive/events.rb', line 173

def trigger!(event, filter=nil, *args)
  # puts "TRIGGER: #{event} on #{self.inspect}" if event == :added
  are_reactive = reactive?

  event = event.to_sym

  if @listeners && @listeners[event]
    # 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
      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



198
199
200
# File 'lib/volt/reactive/events.rb', line 198

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

#trigger_for_methods!(event, *method_names) ⇒ Object

Takes an event and a list of method names, and triggers the event for each listener coming off of those methods.



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/volt/reactive/events.rb', line 204

def trigger_for_methods!(event, *method_names)
  trigger_by_scope!(event, [], nil) do |scope|
    if scope
      method_name = scope.first

      method_names.include?(method_name)
    else
      false
    end
  end
end