lib/vedeu/support/event.rb in vedeu-0.1.18 vs lib/vedeu/support/event.rb in vedeu-0.1.19

- old
+ new

@@ -1,8 +1,14 @@ module Vedeu + + # Contains all the logic of an event. Handles debouncing and throttling. + # + # @api private class Event + # Returns a new instance of Event. + # # @param closure [Proc] # @param options [Hash] # @return [Event] def initialize(closure, options = {}) @closure = closure @@ -10,10 +16,12 @@ @deadline = 0 @executed_at = 0 @now = 0 end + # Triggers the event based on debouncing and throttling conditions. + # # @param args [Array] # @return [] def trigger(*args) return execute(*args) unless debouncing? || throttling? @@ -25,10 +33,12 @@ private attr_reader :closure attr_accessor :deadline, :executed_at, :now + # Execute the code stored in the event closure. + # # @api private # @return [] def execute(*args) reset_deadline @@ -37,18 +47,26 @@ reset_time closure.call(*args) end + # 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] def throttling? set_time options[:delay] > 0 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] def debouncing? set_time @@ -102,17 +120,17 @@ end # @api private # @return [Fixnum|Float] def debounce - options[:debounce] + options[:debounce] || defaults[:debounce] end # @api private # @return [Fixnum|Float] def delay - options[:delay] + options[:delay] || defaults[:delay] end # @api private # @return [Hash] def options @@ -121,11 +139,11 @@ # @api private # @return [Hash] def defaults { - delay: 0, - debounce: 0 + delay: 0.0, + debounce: 0.0 } end end end