Parent

Namespace

Included Modules

Class/Module Index [+]

Quicksearch

Timers

Constants

VERSION

Public Class Methods

new() click to toggle source
# File lib/timers.rb, line 15
def initialize
  @timers = SortedSet.new
  @paused_timers = SortedSet.new
  @interval = Hitimes::Interval.new
  @interval.start
end

Public Instance Methods

add(timer) click to toggle source
# File lib/timers.rb, line 66
def add(timer)
  raise TypeError, "not a Timers::Timer" unless timer.is_a? Timers::Timer
  @timers.add(timer)
end
after(interval, &block) click to toggle source

Call the given block after the given interval

# File lib/timers.rb, line 23
def after(interval, &block)
  Timer.new(self, interval, false, &block)
end
after_milliseconds(interval, &block) click to toggle source

Call the given block after the given interval has expired. interval is measured in milliseconds.

Timer.new.after_milliseconds(25) { puts "fired!" }
# File lib/timers.rb, line 32
def after_milliseconds(interval, &block)
  after(interval / 1000.0, &block)
end
Also aliased as: after_ms
after_ms(interval, &block) click to toggle source
Alias for: after_milliseconds
continue(timer = nil) click to toggle source
# File lib/timers.rb, line 82
def continue(timer = nil)
  return continue_all if timer.nil?
  raise TypeError, "not a Timers::Timer" unless timer.is_a? Timers::Timer
  @paused_timers.delete timer
  @timers.add timer
end
continue_all() click to toggle source
# File lib/timers.rb, line 89
def continue_all
  @paused_timers.each {|timer| timer.continue}
end
current_offset() click to toggle source
# File lib/timers.rb, line 99
def current_offset
  @interval.to_f
end
delay(seconds) click to toggle source
# File lib/timers.rb, line 93
def delay(seconds)
  @timers.each {|timer| timer.delay(seconds)}
end
every(interval, &block) click to toggle source

Call the given block periodically at the given interval

# File lib/timers.rb, line 38
def every(interval, &block)
  Timer.new(self, interval, true, &block)
end
fire(offset = self.current_offset) click to toggle source

Fire all timers that are ready

# File lib/timers.rb, line 58
def fire(offset = self.current_offset)
  time = Float(offset) + 0.001 # Fudge 1ms in case of clock imprecision
  while (timer = @timers.first) && (time >= timer.offset)
    @timers.delete timer
    timer.fire(offset)
  end
end
pause(timer = nil) click to toggle source
# File lib/timers.rb, line 71
def pause(timer = nil)
  return pause_all if timer.nil?
  raise TypeError, "not a Timers::Timer" unless timer.is_a? Timers::Timer
  @timers.delete timer
  @paused_timers.add timer
end
pause_all() click to toggle source
# File lib/timers.rb, line 78
def pause_all
  @timers.each {|timer| timer.pause}
end
wait() click to toggle source

Wait for the next timer and fire it

# File lib/timers.rb, line 43
def wait
  i = wait_interval
  sleep i if i
  fire
end
wait_interval(offset = self.current_offset) click to toggle source

Interval to wait until when the next timer will fire

# File lib/timers.rb, line 50
def wait_interval(offset = self.current_offset)
  timer = @timers.first
  return unless timer
  interval = timer.offset - Float(offset)
  interval > 0 ? interval : 0
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.