class EasyTimers::Group
Maintains a list of timers.
Constants
- GRANULARITY
Public Class Methods
new()
click to toggle source
Create a new instance and begin the loop
# File lib/easy_timers/group.rb, line 11 def initialize @events = [] # Maintained in sorted order of furthest-soonest, aka descending order. @names = Hash.new do |hash,key| # Each array in descending order. hash[key] = [] end @looping_thread = Thread.new() do while true self.loop() end end end
Public Instance Methods
delete(name)
click to toggle source
Delete a timer from the group. @param name [Symbol]
# File lib/easy_timers/group.rb, line 45 def delete(name) if @names[name].size() > 0 @names[name].each do |timer| timer.cancel() end @names.delete(name) @looping_thread.run() end end
get_current_time()
click to toggle source
Return the current time in seconds
# File lib/easy_timers/group.rb, line 88 def get_current_time() return Time.now.gmtime.to_f end
insert(timer)
click to toggle source
Insert a new timer into the group @param timer [Timer]
# File lib/easy_timers/group.rb, line 26 def insert(timer) index = @events.index do |element| timer.time > element.time end if index == nil @events.push(timer) else @events.insert(index, timer) end @names[timer.name].push(timer) @looping_thread.run() return timer.name end
loop()
click to toggle source
Perform a single loop.
# File lib/easy_timers/group.rb, line 57 def loop() if @events.empty? sleep #until woken else time = self.get_current_time() while @events.last.time <= time timer = @events.pop @names[timer.name].pop if timer.cancelled? == false if timer.recurring == true newTime = timer.time + timer.interval newTimer = Timer.new(newTime, timer.name, timer.interval, timer.recurring, timer.callback) self.insert(newTimer) end Thread.new do timer.callback.call end end end untilNext = @events.last.time - time if untilNext < 0.0 untilNext = 0.0 end sleep untilNext end end