Class: Vedeu::Events::Event Private
- Inherits:
-
Object
- Object
- Vedeu::Events::Event
- Extended by:
- Forwardable
- Includes:
- Repositories::Model
- Defined in:
- lib/vedeu/events/event.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Contains all the logic of an event. Handles debouncing and throttling.
Vedeu provides an event mechanism to facilitate the functionality of your application. The events are either Vedeu defined, ie. system events or user defined, ie. user events. User events allow you to orchestrate behaviour within your application, ie. the user presses a specific key, you trigger an event to make something happen. Eg. pressing ‘p’ instructs the player to play.
Events described here assume that you have bound the events within your classes:
class SomeClassInYourApplication
Vedeu.bind(:event_name) do |arg1, arg2|
# Things that should happen when the event is triggered;
# these can be method calls or the triggering of another
# event or events.
end
Vedeu.bind(:event_name) do
# Not all events you define will have arguments; like
# methods.
:do_stuff
end
Instance Attribute Summary collapse
- #closure ⇒ String readonly protected private
-
#name ⇒ NilClass|Symbol|String
readonly
protected
private
The name of the model, the target model or the name of the associated model.
Attributes included from Repositories::Model
Class Method Summary collapse
-
.bind(name, options = {}, &block) ⇒ Boolean
private
Register an event by name with optional delay (throttling) which when triggered will execute the code contained within the passed block.
-
.bound?(name) ⇒ Boolean
private
-
.event ⇒ Boolean
private
Register an event by name with optional delay (throttling) which when triggered will execute the code contained within the passed block.
-
.register ⇒ Boolean
private
Register an event by name with optional delay (throttling) which when triggered will execute the code contained within the passed block.
-
.unbind(name) ⇒ Boolean
private
Instance Method Summary collapse
- #bind ⇒ Object private
-
#deadline? ⇒ Boolean
private
private
Returns a boolean indicating whether this event has a deadline.
-
#debounce ⇒ Fixnum|Float
private
private
Return the amount of time in seconds to debounce the event by.
-
#debounce_expired? ⇒ Boolean
private
private
Returns a boolean indicating whether the debounce has expired.
-
#debouncing? ⇒ Boolean
private
private
Returns a boolean indicating whether debouncing is required for this event.
-
#defaults ⇒ Hash<Symbol => void>
private
private
The default options/attributes for a new instance of this class.
-
#delay ⇒ Fixnum|Float
private
private
Return the amount of time in seconds to throttle the event by.
-
#execute(*args) ⇒ void
private
private
Execute the code stored in the event closure.
-
#initialize(name, closure, options = {}) ⇒ Vedeu::Events::Event
constructor
private
Returns a new instance of Vedeu::Events::Event.
-
#options ⇒ Hash<Symbol => void>
private
private
Combines the options provided at instantiation with the default values.
-
#throttle_expired? ⇒ Boolean
private
private
Returns a boolean indicating whether the throttle has expired.
-
#throttling? ⇒ Boolean
private
private
Returns a boolean indicating whether throttling is required for this event.
-
#trigger(*args) ⇒ void
private
Triggers the event based on debouncing and throttling conditions.
Methods included from Repositories::Model
Constructor Details
#initialize(name, closure, options = {}) ⇒ Vedeu::Events::Event
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Vedeu::Events::Event.
128 129 130 131 132 133 134 135 136 |
# File 'lib/vedeu/events/event.rb', line 128 def initialize(name, closure, = {}) @name = name @options = @closure = closure @deadline = 0 @executed_at = 0 @now = 0 @repository = Vedeu.events end |
Instance Attribute Details
#closure ⇒ String (readonly, protected)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
170 171 172 |
# File 'lib/vedeu/events/event.rb', line 170 def closure @closure end |
#name ⇒ NilClass|Symbol|String (readonly, protected)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The name of the model, the target model or the name of the associated model.
174 175 176 |
# File 'lib/vedeu/events/event.rb', line 174 def name @name end |
Class Method Details
.bind(name, options = {}, &block) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Register an event by name with optional delay (throttling) which when triggered will execute the code contained within the passed block.
87 88 89 90 91 |
# File 'lib/vedeu/events/event.rb', line 87 def bind(name, = {}, &block) Vedeu.log(type: :event, message: "Binding: '#{name.inspect}'") new(name, block, ).bind end |
.bound?(name) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
98 99 100 101 |
# File 'lib/vedeu/events/event.rb', line 98 def bound?(name) Vedeu.events.registered?(name) || Vedeu::Events::Aliases.registered?(name) end |
.event ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Register an event by name with optional delay (throttling) which when triggered will execute the code contained within the passed block.
92 93 94 95 96 |
# File 'lib/vedeu/events/event.rb', line 92 def bind(name, = {}, &block) Vedeu.log(type: :event, message: "Binding: '#{name.inspect}'") new(name, block, ).bind end |
.register ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Register an event by name with optional delay (throttling) which when triggered will execute the code contained within the passed block.
93 94 95 96 97 |
# File 'lib/vedeu/events/event.rb', line 93 def bind(name, = {}, &block) Vedeu.log(type: :event, message: "Binding: '#{name.inspect}'") new(name, block, ).bind end |
.unbind(name) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/vedeu/events/event.rb', line 106 def unbind(name) return false unless Vedeu.bound?(name) Vedeu.log(type: :event, message: "Unbinding: '#{name.inspect}'") Vedeu.events.remove(name) true end |
Instance Method Details
#bind ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/vedeu/events/event.rb', line 139 def bind new_collection = if repository.registered?(name) repository.find(name).add(self) else Vedeu::Events::Events.new([self], nil, name) end repository.store(new_collection) true end |
#deadline? ⇒ Boolean (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether this event has a deadline.
243 244 245 |
# File 'lib/vedeu/events/event.rb', line 243 def deadline? @deadline > 0 end |
#debounce ⇒ Fixnum|Float (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the amount of time in seconds to debounce the event by.
250 251 252 |
# File 'lib/vedeu/events/event.rb', line 250 def debounce [:debounce] || defaults[:debounce] end |
#debounce_expired? ⇒ Boolean (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether the debounce has expired.
231 232 233 234 235 236 237 |
# File 'lib/vedeu/events/event.rb', line 231 def debounce_expired? return true if (@executed_at = @now) > @deadline Vedeu.log(type: :event, message: "Debouncing: '#{name.inspect}'") false end |
#debouncing? ⇒ Boolean (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether debouncing is required for this event. Setting the debounce option to any value greater than 0 will enable debouncing. Sets the deadline for when this event can be executed to a point in the future determined by the amount of debounce time left.
220 221 222 223 224 225 226 |
# File 'lib/vedeu/events/event.rb', line 220 def debouncing? @now = Vedeu.clock_time @deadline = @now + debounce unless deadline? [:debounce] > 0 end |
#defaults ⇒ Hash<Symbol => void> (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The default options/attributes for a new instance of this class.
270 271 272 273 274 275 |
# File 'lib/vedeu/events/event.rb', line 270 def defaults { delay: 0.0, debounce: 0.0, } end |
#delay ⇒ Fixnum|Float (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the amount of time in seconds to throttle the event by.
257 258 259 |
# File 'lib/vedeu/events/event.rb', line 257 def delay [:delay] || defaults[:delay] end |
#execute(*args) ⇒ void (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Execute the code stored in the event closure.
182 183 184 185 186 187 188 |
# File 'lib/vedeu/events/event.rb', line 182 def execute(*args) @deadline = 0 # reset deadline @executed_at = @now # set execution time to now @now = 0 # reset now closure.call(*args) end |
#options ⇒ Hash<Symbol => void> (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Combines the options provided at instantiation with the default values.
265 266 267 |
# File 'lib/vedeu/events/event.rb', line 265 def defaults.merge!(@options) end |
#throttle_expired? ⇒ Boolean (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether the throttle has expired.
204 205 206 207 208 209 210 |
# File 'lib/vedeu/events/event.rb', line 204 def throttle_expired? return true if (@now - @executed_at) > delay Vedeu.log(type: :event, message: "Throttling: '#{name.inspect}'") false end |
#throttling? ⇒ Boolean (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether throttling is required for this event. Setting the delay option to any value greater than 0 will enable throttling.
195 196 197 198 199 |
# File 'lib/vedeu/events/event.rb', line 195 def throttling? @now = Vedeu.clock_time [:delay] > 0 end |
#trigger(*args) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Triggers the event based on debouncing and throttling conditions.
158 159 160 161 162 163 164 |
# File 'lib/vedeu/events/event.rb', line 158 def trigger(*args) return execute(*args) unless debouncing? || throttling? return execute(*args) if debouncing? && debounce_expired? return execute(*args) if throttling? && throttle_expired? end |