Class: Timers

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/timers.rb,
lib/timers/version.rb

Defined Under Namespace

Classes: Timer

Constant Summary collapse

VERSION =
"2.0.0"

Instance Method Summary collapse

Constructor Details

#initializeTimers

Returns a new instance of Timers.



15
16
17
18
19
20
# File 'lib/timers.rb', line 15

def initialize
  @timers = SortedSet.new
  @paused_timers = SortedSet.new
  @interval = Hitimes::Interval.new
  @interval.start
end

Instance Method Details

#add(timer) ⇒ Object

Raises:

  • (TypeError)


66
67
68
69
# 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) ⇒ Object

Call the given block after the given interval



23
24
25
# File 'lib/timers.rb', line 23

def after(interval, &block)
  Timer.new(self, interval, false, &block)
end

#after_milliseconds(interval, &block) ⇒ Object Also known as: after_ms

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

Timer.new.after_milliseconds(25) { puts "fired!" }


32
33
34
# File 'lib/timers.rb', line 32

def after_milliseconds(interval, &block)
  after(interval / 1000.0, &block)
end

#continue(timer = nil) ⇒ Object

Raises:

  • (TypeError)


82
83
84
85
86
87
# 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_allObject



89
90
91
# File 'lib/timers.rb', line 89

def continue_all
  @paused_timers.each {|timer| timer.continue}
end

#current_offsetObject



99
100
101
# File 'lib/timers.rb', line 99

def current_offset
  @interval.to_f
end

#delay(seconds) ⇒ Object



93
94
95
# File 'lib/timers.rb', line 93

def delay(seconds)
  @timers.each {|timer| timer.delay(seconds)}
end

#every(interval, &block) ⇒ Object

Call the given block periodically at the given interval



38
39
40
# File 'lib/timers.rb', line 38

def every(interval, &block)
  Timer.new(self, interval, true, &block)
end

#fire(offset = self.current_offset) ⇒ Object

Fire all timers that are ready



58
59
60
61
62
63
64
# 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) ⇒ Object

Raises:

  • (TypeError)


71
72
73
74
75
76
# 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_allObject



78
79
80
# File 'lib/timers.rb', line 78

def pause_all
  @timers.each {|timer| timer.pause}
end

#waitObject

Wait for the next timer and fire it



43
44
45
46
47
# File 'lib/timers.rb', line 43

def wait
  i = wait_interval
  sleep i if i
  fire
end

#wait_interval(offset = self.current_offset) ⇒ Object

Interval to wait until when the next timer will fire



50
51
52
53
54
55
# 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