Module: Events
- Included in:
- Channel, ComponentNode, ReactiveManager, ReactiveTemplate
- Defined in:
- lib/volt/reactive/events.rb
Instance Attribute Summary collapse
-
#scope ⇒ Object
Returns the value of attribute scope.
Instance Method Summary collapse
- #add_event_follower(follower) ⇒ Object
-
#add_event_to_chains(event) ⇒ Object
When events get added, we need to notify event chains so they can update and chain any new events.
- #add_following(object) ⇒ Object
-
#all_listeners_for(event) ⇒ Object
Return all listeners for an event on the current object and any event following objects.
- #event_chain ⇒ Object
-
#event_followers ⇒ Object
Track who’s following us.
-
#event_followings ⇒ Object
Track the current object that we’re following.
- #listeners ⇒ Object
-
#on(event, scope_provider = nil, &block) ⇒ Object
Add a listener for an event.
- #remove_event_follower(follower) ⇒ Object
-
#remove_event_from_chains(event) ⇒ Object
When events are removed, we need to notify any relevent chains so they can remove any chained events.
- #remove_following(object) ⇒ Object
-
#remove_listener(event, listener) ⇒ Object
Typically you would call .remove on the listener returned from the .on method.
- #trigger!(event, *args) ⇒ Object
-
#trigger_by_scope!(event, *args, &block) ⇒ Object
Takes a block, which passes in.
Instance Attribute Details
#scope ⇒ Object
Returns the value of attribute scope.
101 102 103 |
# File 'lib/volt/reactive/events.rb', line 101 def scope @scope end |
Instance Method Details
#add_event_follower(follower) ⇒ Object
223 224 225 226 227 228 |
# File 'lib/volt/reactive/events.rb', line 223 def add_event_follower(follower) @event_followers ||= [] @event_followers << follower follower.add_following(self) end |
#add_event_to_chains(event) ⇒ Object
When events get added, we need to notify event chains so they can update and chain any new events.
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/volt/reactive/events.rb', line 163 def add_event_to_chains(event) # First time this event is added, update any chains event_chain.add_event(event) # We need to keep the event chain's updated for any objects we're # following for events. event_followings.each {|ef| ef.event_chain.add_event(event) } if event != :changed && !@other_event_listener @other_event_listener = on('changed') { } end end |
#add_following(object) ⇒ Object
200 201 202 203 204 205 206 207 208 |
# File 'lib/volt/reactive/events.rb', line 200 def add_following(object) @event_followings ||= [] @event_followings << object # Take all of our listeners and add them to the listeners.keys.each do |event| object.event_chain.add_event(event) end end |
#all_listeners_for(event) ⇒ Object
Return all listeners for an event on the current object and any event following objects.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/volt/reactive/events.rb', line 240 def all_listeners_for(event) # TODO: We dup at the moment because some events unregister events, is there # a better solution than this? all_listeners = [] all_listeners += @listeners[event].dup if @listeners && @listeners[event] if @event_followers @event_followers.each do |event_follower| ef_listeners = event_follower.listeners all_listeners += ef_listeners[event].dup if ef_listeners[event] end end return all_listeners end |
#event_chain ⇒ Object
126 127 128 |
# File 'lib/volt/reactive/events.rb', line 126 def event_chain @event_chain ||= EventChain.new(self) end |
#event_followers ⇒ Object
Track who’s following us
219 220 221 |
# File 'lib/volt/reactive/events.rb', line 219 def event_followers @event_followers || [] end |
#event_followings ⇒ Object
Track the current object that we’re following.
196 197 198 |
# File 'lib/volt/reactive/events.rb', line 196 def event_followings @event_followings || [] end |
#listeners ⇒ Object
130 131 132 |
# File 'lib/volt/reactive/events.rb', line 130 def listeners @listeners || {} end |
#on(event, scope_provider = nil, &block) ⇒ Object
Add a listener for an event
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/volt/reactive/events.rb', line 103 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 add_event_to_chains(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_event_follower(follower) ⇒ Object
230 231 232 233 234 235 236 |
# File 'lib/volt/reactive/events.rb', line 230 def remove_event_follower(follower) if @event_followers @event_followers.delete(follower) follower.remove_following(self) end end |
#remove_event_from_chains(event) ⇒ Object
When events are removed, we need to notify any relevent chains so they can remove any chained events.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/volt/reactive/events.rb', line 178 def remove_event_from_chains(event) event_chain.remove_event(event) # We need to keep the event chain's updated for any objects we're # following for events. event_followings.each {|ef| ef.event_chain.remove_event(event) } if event != :changed # See if there are any remaining events that aren't changed if listeners.keys.reject {|k| k == :changed }.size == 0 @other_event_listener.remove @other_event_listener = nil end end end |
#remove_following(object) ⇒ Object
210 211 212 213 214 215 216 |
# File 'lib/volt/reactive/events.rb', line 210 def remove_following(object) @event_followings.delete(object) listeners.keys.each do |event| object.event_chain.remove_event(event) end 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
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/volt/reactive/events.rb', line 137 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 remove_event_from_chains(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, *args) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/volt/reactive/events.rb', line 256 def trigger!(event, *args) ObjectTracker.process_queue if !reactive? && !respond_to?(:skip_current_queue_flush) event = event.to_sym all_listeners_for(event).each do |listener| # Call the event on each listener listener.call(*args) end nil end |
#trigger_by_scope!(event, *args, &block) ⇒ Object
Takes a block, which passes in
270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/volt/reactive/events.rb', line 270 def trigger_by_scope!(event, *args, &block) ObjectTracker.process_queue if !reactive? && !respond_to?(:skip_current_queue_flush) event = event.to_sym all_listeners_for(event).each do |listener| # Call the block, pass in the scope if block.call(listener.scope) listener.call(*args) end end end |