Class: Timers::Group

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGroup

Returns a new instance of Group.



16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#eventsObject (readonly)

Scheduled events:



27
28
29
# File 'lib/timers/group.rb', line 27

def events
  @events
end

#paused_timersObject (readonly)

Paused timers:



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

def paused_timers
  @paused_timers
end

#timersObject (readonly)

Active timers:



30
31
32
# File 'lib/timers/group.rb', line 30

def timers
  @timers
end

Instance Method Details

#after(interval, &block) ⇒ Object

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.



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

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

#cancelObject

Cancel all timers.



121
122
123
124
125
# File 'lib/timers/group.rb', line 121

def cancel
  @timers.dup.each do |timer|
    timer.cancel
  end
end

#current_offsetObject

The group’s current time.



128
129
130
# File 'lib/timers/group.rb', line 128

def current_offset
  @interval.to_f
end

#delay(seconds) ⇒ Object

Delay all timers.



114
115
116
117
118
# File 'lib/timers/group.rb', line 114

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

#every(interval, recur = true, &block) ⇒ Object

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.



50
51
52
# 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) ⇒ Object

Fire all timers that are ready.



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

def fire(offset = self.current_offset)
  @events.fire(offset)
end

#now_and_after(interval, &block) ⇒ Object

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.



43
44
45
46
# 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) ⇒ Object

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.



56
57
58
59
# File 'lib/timers/group.rb', line 56

def now_and_every(interval, recur = true, &block)
  block.call
  every(interval, recur, &block)
end

#pauseObject

Pause all timers.



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

def pause
  @timers.dup.each do |timer|
    timer.pause
  end
end

#resumeObject Also known as: continue

Resume all timers.



105
106
107
108
109
# File 'lib/timers/group.rb', line 105

def resume
  @paused_timers.dup.each do |timer|
    timer.resume
  end
end

#wait(&block) ⇒ Object

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).



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Object

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



86
87
88
89
90
# 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