class LSync::EventTimer
The EventTimer
provides a simple time based callback mechanism in which events can be aggregated. If the timer is triggered once, it will take at most max time for the callback to be triggered.
Public Class Methods
new(max, &block)
click to toggle source
Times are measured in seconds. Min specifies the minimum duration between callback invocations. Max specifies the maximum duration between callback invocations.
# File lib/lsync/event_timer.rb, line 55 def initialize(max, &block) @max = max @fired = nil @timeout = nil @callback = block @processing = Mutex.new end
Public Instance Methods
join()
click to toggle source
Wait for the timeout to complete nicely.
# File lib/lsync/event_timer.rb, line 92 def join if @timeout @timeout.thread.join end end
trigger!()
click to toggle source
Trigger the event timer such that within the specified time, the callback will be fired.
# File lib/lsync/event_timer.rb, line 66 def trigger! unless @timeout @timeout = Timeout.new(@max) { fire! } end end
Private Instance Methods
expired?(duration = nil)
click to toggle source
Return true of the timeout has expired, e.g if it has not been fired within the given duration.
# File lib/lsync/event_timer.rb, line 75 def expired?(duration = nil) !@fired || ((Time.now - @fired) > duration) end
fire!()
click to toggle source
Fire the callback.
# File lib/lsync/event_timer.rb, line 80 def fire! @processing.synchronize do @timeout = nil @fired = Time.now @callback.call end end