Class: Hoodie::Timers::Timer
- Includes:
- Comparable
- Defined in:
- lib/hoodie/timers.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
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#recurring ⇒ Object
readonly
Returns the value of attribute recurring.
Instance Method Summary collapse
-
#cancel ⇒ Object
Cancel this timer.
-
#delay(seconds) ⇒ Object
Extend this timer.
-
#fire(offset = @group.current_offset) ⇒ Object
(also: #call)
Fire the block.
-
#fires_in ⇒ Object
Number of seconds until next fire / since last fire.
-
#initialize(group, interval, recurring = false, offset = nil, &block) ⇒ Timer
constructor
A new instance of Timer.
-
#inspect ⇒ Object
Inspect a timer.
- #pause ⇒ Object
- #paused? ⇒ Boolean
-
#reset(offset = @group.current_offset) ⇒ Object
Reset this timer.
- #resume ⇒ Object (also: #continue)
Constructor Details
#initialize(group, interval, recurring = false, offset = nil, &block) ⇒ Timer
Returns a new instance of Timer.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/hoodie/timers.rb', line 16 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
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
14 15 16 |
# File 'lib/hoodie/timers.rb', line 14 def interval @interval end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
14 15 16 |
# File 'lib/hoodie/timers.rb', line 14 def offset @offset end |
#recurring ⇒ Object (readonly)
Returns the value of attribute recurring.
14 15 16 |
# File 'lib/hoodie/timers.rb', line 14 def recurring @recurring end |
Instance Method Details
#cancel ⇒ Object
Cancel this timer. Do not call while paused.
57 58 59 60 61 62 63 |
# File 'lib/hoodie/timers.rb', line 57 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
50 51 52 53 54 |
# File 'lib/hoodie/timers.rb', line 50 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.
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/hoodie/timers.rb', line 79 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) cancel unless recurring end |
#fires_in ⇒ Object
Number of seconds until next fire / since last fire
94 95 96 |
# File 'lib/hoodie/timers.rb', line 94 def fires_in @offset - @group.current_offset if @offset end |
#inspect ⇒ Object
Inspect a timer
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/hoodie/timers.rb', line 99 def inspect str = "#<Timers::Timer:#{object_id.to_s(16)} " if @offset if fires_in >= 0 str << "fires in #{fires_in} seconds" else str << "fired #{fires_in.abs} seconds ago" end str << ", recurs every #{interval}" if recurring else str << 'dead' end str << '>' end |
#pause ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/hoodie/timers.rb', line 33 def pause return if paused? @group.timers.delete self @group.paused_timers.add self @handle.cancel! if @handle @handle = nil end |
#paused? ⇒ Boolean
29 30 31 |
# File 'lib/hoodie/timers.rb', line 29 def paused? @group.paused_timers.include? self end |
#reset(offset = @group.current_offset) ⇒ Object
Reset this timer. Do not call while paused.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/hoodie/timers.rb', line 66 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 |
#resume ⇒ Object Also known as: continue
41 42 43 44 45 46 |
# File 'lib/hoodie/timers.rb', line 41 def resume return unless paused? @group.paused_timers.delete self # This will add us back to the group: reset end |