lib/vedeu/support/event.rb in vedeu-0.2.0 vs lib/vedeu/support/event.rb in vedeu-0.2.1

- old
+ new

@@ -23,13 +23,13 @@ # @param args [Array] # @return [] def trigger(*args) return execute(*args) unless debouncing? || throttling? - return execute(*args) if debouncing? && set_executed > deadline + return execute(*args) if debouncing? && debounce_expired? - return execute(*args) if throttling? && elapsed_time > delay + return execute(*args) if throttling? && throttle_expired? end private # @return [Proc] @@ -61,32 +61,66 @@ # Returns a boolean indicating whether throttling is required for this # event. Setting the delay option to any value greater than 0 will enable # throttling. # # @api private - # @return [TrueClass|FalseClass] + # @return [Boolean] def throttling? set_time options[:delay] > 0 end + # Returns a boolean indicating whether the throttle has expired. + # + # @api private + # @return [Boolean] + def throttle_expired? + if elapsed_time > delay + Vedeu.log("Event throttle has expired for '#{event_name}', executing " \ + "event.") + true + + else + Vedeu.log("Event throttle not yet expired for '#{event_name}'.") + false + + end + end + # Returns a boolean indicating whether debouncing is required for this # event. Setting the debounce option to any value greater than 0 will # enable debouncing. # # @api private - # @return [TrueClass|FalseClass] + # @return [Boolean] def debouncing? set_time set_deadline unless has_deadline? options[:debounce] > 0 end + # Returns a boolean indicating whether the debounce has expired. + # # @api private + # @return [Boolean] + def debounce_expired? + if set_executed > deadline + Vedeu.log("Event debounce has expired for '#{event_name}', executing " \ + "event.") + true + + else + Vedeu.log("Event debounce not yet expired for '#{event_name}'.") + false + + end + end + + # @api private # @return [Float] def elapsed_time now - @executed_at end @@ -107,11 +141,11 @@ def reset_time @now = 0 end # @api private - # @return [TrueClass|FalseClass] + # @return [Boolean] def has_deadline? @deadline > 0 end # @api private @@ -127,10 +161,16 @@ nil end # @api private + # @return [String] + def event_name + options[:event_name].to_s + end + + # @api private # @return [Fixnum|Float] def debounce options[:debounce] || defaults[:debounce] end @@ -148,11 +188,12 @@ # @api private # @return [Hash] def defaults { - delay: 0.0, - debounce: 0.0 + delay: 0.0, + debounce: 0.0, + event_name: '', } end end end