class Utilrb::EventLoop::Timer

Timer for the {EventLoop} which supports single shot and periodic activation

@example

loop = EventLoop.new
timer = EventLoop.every(0.1) do 
           puts 123
        end
loop.exec

Attributes

doc[RW]
event_loop[RW]
period[RW]
result[R]
single_shot[RW]

Public Class Methods

new(event_loop,period=0,single_shot=false,&block) click to toggle source

A timer

@param [EventLoop] event_loop the {EventLoop} the timer belongs to @param period The period of the timer in seconds. @param single_shot if true the timer will fire only once @param block The code block which will be executed each time the timer fires @see EventLoop#once

# File lib/utilrb/event_loop.rb, line 59
def initialize(event_loop,period=0,single_shot=false,&block)
    @block = block
    @event_loop = event_loop
    @last_call = Time.now
    @period = period
    @single_shot = single_shot
    @stopped = true
    @doc = Kernel.caller.find do |s|
        !(%r"#{Regexp.quote(__FILE__)}"o =~ s) && !(s =~ /^\/usr\/.+/)
    end.to_s
end

Public Instance Methods

call(time = Time.now) click to toggle source

Executes the code block tight to the timer and saves a time stamp.

@param [Time] time The time stamp

# File lib/utilrb/event_loop.rb, line 135
def call(time = Time.now)
    reset(time)
    @result = @block.call
end
cancel() click to toggle source

Cancels the timer. If it is not running it will do nothing

# File lib/utilrb/event_loop.rb, line 72
def cancel
    @stopped = true
    @event_loop.cancel_timer self
end
Also aliased as: stop
reset(time = Time.now) click to toggle source

Resets the timer internal time to the given one.

@param [Time] time the time

# File lib/utilrb/event_loop.rb, line 143
def reset(time = Time.now)
    @last_call = time
end
running?() click to toggle source

Returns true if the timer is currently running.

@return [boolean]

# File lib/utilrb/event_loop.rb, line 84
def running?
    @event_loop.timer? self
end
single_shot?() click to toggle source

Returns true if the timer is a single shot timer.

@return [Boolean}

# File lib/utilrb/event_loop.rb, line 127
def single_shot?
    @single_shot == true
end
start(period = @period,instantly = true) click to toggle source

Starts the timer by adding itself to the EventLoop the timer belongs to. If no period is given the one which was given during initializing will be used.

@param [Float] period The period in seconds @param [TrueClass,FalseClass] instantly If set to true the timer instantly runs otherwise

the timer waits until the first period passed.

@raise [ArgumentError] if no period is specified @return [Timer]

# File lib/utilrb/event_loop.rb, line 97
def start(period = @period,instantly = true)
    cancel
    @stopped = false
    @period = period
    raise ArgumentError,"no period is given" unless @period
    @last_call = if instantly
                     Time.at(0)
                 else
                     Time.now
                 end
    @event_loop.add_timer self
    self
end
stop()
Alias for: cancel
stopped?() click to toggle source
# File lib/utilrb/event_loop.rb, line 77
def stopped?
    @stopped 
end
timeout?(time = Time.now) click to toggle source

Returns true if the timer should fire now. This is called by the EventLoop to check if the timer elapsed.

@param [Time] time The time used for checking @return [Boolean}

# File lib/utilrb/event_loop.rb, line 116
def timeout?(time = Time.now)
    if(time-@last_call).to_f >= @period
        true
    else
        false
    end
end