class Timers::Timer

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.

Attributes

interval[R]
offset[R]
recurring[R]

Public Class Methods

new(group, interval, recurring = false, offset = nil, &block) click to toggle source
# File lib/timers/timer.rb, line 33
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

Public Instance Methods

call(offset = @group.current_offset)
Alias for: fire
cancel() click to toggle source

Cancel this timer. Do not call while paused.

# File lib/timers/timer.rb, line 82
def cancel
        return unless @handle
        
        @handle.cancel! if @handle
        @handle = nil
        
        # This timer is no longer valid:
        @group.timers.delete self if @group
end
continue()
Alias for: resume
delay(seconds) click to toggle source

Extend this timer

# File lib/timers/timer.rb, line 73
def delay(seconds)
        @handle.cancel! if @handle
        
        @offset += seconds
        
        @handle = @group.events.schedule(@offset, self)
end
fire(offset = @group.current_offset) click to toggle source

Fire the block.

# File lib/timers/timer.rb, line 109
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
Also aliased as: call
fires_in() click to toggle source

Number of seconds until next fire / since last fire

# File lib/timers/timer.rb, line 127
def fires_in
        @offset - @group.current_offset if @offset
end
inspect() click to toggle source

Inspect a timer

# File lib/timers/timer.rb, line 132
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
pause() click to toggle source
# File lib/timers/timer.rb, line 51
def pause
        return if paused?
        
        @group.timers.delete self
        @group.paused_timers.add self
        
        @handle.cancel! if @handle
        @handle = nil
end
paused?() click to toggle source
# File lib/timers/timer.rb, line 47
def paused?
        @group.paused_timers.include? self
end
reset(offset = @group.current_offset) click to toggle source

Reset this timer. Do not call while paused. @param offset [Numeric] the duration to add to the timer.

# File lib/timers/timer.rb, line 94
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() click to toggle source
# File lib/timers/timer.rb, line 61
def resume
        return unless paused?
        
        @group.paused_timers.delete self
        
        # This will add us back to the group:
        reset
end
Also aliased as: continue