Class: Timers::Timer

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/timers/timer.rb

Overview

An individual timer set to fire a given proc at a given time. A timer is always connected to a Timer::Group but it would ONLY be in @group.timers if it also has a @handle specified. Otherwise it is either PAUSED or has been FIRED and is not recurring. You can manually enter this state by calling #cancel and resume normal operation by calling #reset.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, interval, recurring = false, offset = nil, &block) ⇒ Timer

Returns a new instance of Timer.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/timers/timer.rb', line 20

def initialize(group, interval, recurring = false, offset = nil, &block)
	@group = group
	
	@interval = interval
	@recurring = recurring
	@block = block
	@offset = offset
	
	@handle = nil
	
	# If a start offset was supplied, use that, otherwise use the current timers offset.
	reset(@offset || @group.current_offset)
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



18
19
20
# File 'lib/timers/timer.rb', line 18

def interval
  @interval
end

#offsetObject (readonly)

Returns the value of attribute offset.



18
19
20
# File 'lib/timers/timer.rb', line 18

def offset
  @offset
end

#recurringObject (readonly)

Returns the value of attribute recurring.



18
19
20
# File 'lib/timers/timer.rb', line 18

def recurring
  @recurring
end

Instance Method Details

#cancelObject

Cancel this timer. Do not call while paused.



69
70
71
72
73
74
75
76
77
# File 'lib/timers/timer.rb', line 69

def cancel
	return unless @handle
	
	@handle.cancel! if @handle
	@handle = nil
	
	# This timer is no longer valid:
	@group.timers.delete self if @group
end

#delay(seconds) ⇒ Object

Extend this timer



60
61
62
63
64
65
66
# File 'lib/timers/timer.rb', line 60

def delay(seconds)
	@handle.cancel! if @handle
	
	@offset += seconds
	
	@handle = @group.events.schedule(@offset, self)
end

#fire(offset = @group.current_offset) ⇒ Object Also known as: call

Fire the block.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/timers/timer.rb', line 96

def fire(offset = @group.current_offset)
	if recurring == :strict
		# ... make the next interval strictly the last offset + the interval:
		reset(@offset)
	elsif recurring
		reset(offset)
	else
		@offset = offset
	end
	
	@block.call(offset, self)
	
	cancel unless recurring
end

#fires_inObject

Number of seconds until next fire / since last fire



114
115
116
# File 'lib/timers/timer.rb', line 114

def fires_in
	@offset - @group.current_offset if @offset
end

#inspectObject

Inspect a timer



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/timers/timer.rb', line 119

def inspect
	buffer = "#{to_s[0..-2]} ".dup
	
	if @offset
		if fires_in >= 0
			buffer << "fires in #{fires_in} seconds"
		else
			buffer << "fired #{fires_in.abs} seconds ago"
		end
		
		buffer << ", recurs every #{interval}" if recurring
	else
		buffer << "dead"
	end
	
	buffer << ">"
	
	return buffer
end

#pauseObject



38
39
40
41
42
43
44
45
46
# File 'lib/timers/timer.rb', line 38

def pause
	return if paused?
	
	@group.timers.delete self
	@group.paused_timers.add self
	
	@handle.cancel! if @handle
	@handle = nil
end

#paused?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/timers/timer.rb', line 34

def paused?
	@group.paused_timers.include? self
end

#reset(offset = @group.current_offset) ⇒ Object

Reset this timer. Do not call while paused.

Parameters:

  • offset (Numeric) (defaults to: @group.current_offset)

    the duration to add to the timer.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/timers/timer.rb', line 81

def reset(offset = @group.current_offset)
	# This logic allows us to minimise the interaction with @group.timers.
	# A timer with a handle is always registered with the group.
	if @handle
		@handle.cancel!
	else
		@group.timers << self
	end
	
	@offset = Float(offset) + @interval
	
	@handle = @group.events.schedule(@offset, self)
end

#resumeObject Also known as: continue



48
49
50
51
52
53
54
55
# File 'lib/timers/timer.rb', line 48

def resume
	return unless paused?
	
	@group.paused_timers.delete self
	
	# This will add us back to the group:
	reset
end