Class: Timers::Group

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

Overview

A collection of timers which may fire at different times

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGroup

Returns a new instance of Group.



24
25
26
27
28
29
30
31
32
# File 'lib/timers/group.rb', line 24

def initialize
	@events = Events.new
	
	@timers = Set.new
	@paused_timers = Set.new
	
	@interval = Interval.new
	@interval.start
end

Instance Attribute Details

#eventsObject (readonly)

Scheduled events:



35
36
37
# File 'lib/timers/group.rb', line 35

def events
  @events
end

#paused_timersObject (readonly)

Paused timers:



41
42
43
# File 'lib/timers/group.rb', line 41

def paused_timers
  @paused_timers
end

#timersObject (readonly)

Active timers:



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

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.



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

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

#cancelObject

Cancel all timers.



124
125
126
# File 'lib/timers/group.rb', line 124

def cancel
	@timers.dup.each(&:cancel)
end

#current_offsetObject

The group’s current time.



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

def current_offset
	@interval.to_f
end

#delay(seconds) ⇒ Object

Delay all timers.



117
118
119
120
121
# File 'lib/timers/group.rb', line 117

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.



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

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

#fire(offset = current_offset) ⇒ Object

Fire all timers that are ready.



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

def fire(offset = 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.



51
52
53
54
# File 'lib/timers/group.rb', line 51

def now_and_after(interval, &block)
	yield
	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.



64
65
66
67
# File 'lib/timers/group.rb', line 64

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

#pauseObject

Pause all timers.



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

def pause
	@timers.dup.each(&:pause)
end

#resumeObject Also known as: continue

Resume all timers.



110
111
112
# File 'lib/timers/group.rb', line 110

def resume
	@paused_timers.dup.each(&:resume)
end

#waitObject

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/timers/group.rb', line 72

def wait
	if block_given?
		yield wait_interval
		
		while (interval = wait_interval) && interval > 0
			yield interval
		end
	else
		while (interval = wait_interval) && interval > 0
			# We cannot assume that sleep will wait for the specified time, it might be +/- a bit.
			sleep interval
		end
	end
	
	fire
end

#wait_interval(offset = 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



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

def wait_interval(offset = current_offset)
	handle = @events.first
	handle.time - Float(offset) if handle
end