class Timers::Group

Attributes

events[R]

Scheduled events:

paused_timers[R]

Paused timers:

timers[R]

Active timers:

Public Class Methods

new() click to toggle source
# File lib/timers/group.rb, line 16
def initialize
  @events = Events.new
  
  @timers = Set.new
  @paused_timers = Set.new
  
  @interval = Hitimes::Interval.new
  @interval.start
end

Public Instance Methods

after(interval, &block) click to toggle source

Call the given block after the given interval. The first argument will be the time at which the group was asked to fire timers for.

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

Cancel all timers.

# File lib/timers/group.rb, line 121
def cancel
  @timers.dup.each do |timer|
    timer.cancel
  end
end
continue()
Alias for: resume
current_offset() click to toggle source

The group's current time.

# File lib/timers/group.rb, line 128
def current_offset
  @interval.to_f
end
delay(seconds) click to toggle source

Delay all timers.

# File lib/timers/group.rb, line 114
def delay(seconds)
  @timers.each do |timer|
    timer.delay(seconds)
  end
end
every(interval, recur = true, &block) click to toggle source

Call the given block periodically at the given interval. The first argument will be the time at which the group was asked to fire timers for.

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

Fire all timers that are ready.

# File lib/timers/group.rb, line 93
def fire(offset = self.current_offset)
  @events.fire(offset)
end
now_and_after(interval, &block) click to toggle source

Call the given block immediately, and then after the given interval. The first argument will be the time at which the group was asked to fire timers for.

# File lib/timers/group.rb, line 43
def now_and_after(interval, &block)
  block.call
  after(interval, &block)
end
now_and_every(interval, recur = true, &block) click to toggle source

Call the given block immediately, and then periodically at the given interval. The first argument will be the time at which the group was asked to fire timers for.

# File lib/timers/group.rb, line 56
def now_and_every(interval, recur = true, &block)
  block.call
  every(interval, recur, &block)
end
pause() click to toggle source

Pause all timers.

# File lib/timers/group.rb, line 98
def pause
  @timers.dup.each do |timer|
    timer.pause
  end
end
resume() click to toggle source

Resume all timers.

# File lib/timers/group.rb, line 105
def resume
  @paused_timers.dup.each do |timer|
    timer.resume
  end
end
Also aliased as: continue
wait() { |wait_interval| ... } click to toggle source

Wait for the next timer and fire it. Can take a block, which should behave like sleep(n), except that n may be nil (sleep forever) or a negative number (fire immediately after return).

# File lib/timers/group.rb, line 64
def wait(&block)
  if block_given?
    yield wait_interval
    
    while interval = wait_interval and interval > 0
      yield interval
    end
  else
    while interval = wait_interval and interval > 0
      # We cannot assume that sleep will wait for the specified time, it might be +/- a bit.
      sleep interval
    end
  end

  return fire
end
wait_interval(offset = self.current_offset) click to toggle source

Interval to wait until when the next timer will fire.

  • nil: no timers

  • -ve: timers expired already

  • 0: timers ready to fire

  • +ve: timers waiting to fire

# File lib/timers/group.rb, line 86
def wait_interval(offset = self.current_offset)
  if handle = @events.first
    return handle.time - Float(offset)
  end
end