Class: Utilrb::EventLoop::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/utilrb/event_loop.rb

Overview

Timer for the Utilrb::EventLoop which supports single shot and periodic activation

Examples:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_loop, period = 0, single_shot = false, &block) ⇒ Timer

A timer

@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

Parameters:

See Also:



59
60
61
62
63
64
65
66
67
68
69
# 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

Instance Attribute Details

#docObject

Returns the value of attribute doc.



50
51
52
# File 'lib/utilrb/event_loop.rb', line 50

def doc
  @doc
end

#event_loopObject

Returns the value of attribute event_loop.



48
49
50
# File 'lib/utilrb/event_loop.rb', line 48

def event_loop
  @event_loop
end

#periodObject

Returns the value of attribute period.



47
48
49
# File 'lib/utilrb/event_loop.rb', line 47

def period
  @period
end

#resultObject (readonly)

Returns the value of attribute result.



49
50
51
# File 'lib/utilrb/event_loop.rb', line 49

def result
  @result
end

#single_shotObject

Returns the value of attribute single_shot.



46
47
48
# File 'lib/utilrb/event_loop.rb', line 46

def single_shot
  @single_shot
end

Instance Method Details

#call(time = Time.now) ⇒ Object

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

Parameters:

  • time (Time) (defaults to: Time.now)

    The time stamp



135
136
137
138
# File 'lib/utilrb/event_loop.rb', line 135

def call(time = Time.now)
    reset(time)
    @result = @block.call
end

#cancelObject Also known as: stop

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



72
73
74
75
# File 'lib/utilrb/event_loop.rb', line 72

def cancel
    @stopped = true
    @event_loop.cancel_timer self
end

#reset(time = Time.now) ⇒ Object

Resets the timer internal time to the given one.

Parameters:

  • time (Time) (defaults to: Time.now)

    the time



143
144
145
# File 'lib/utilrb/event_loop.rb', line 143

def reset(time = Time.now)
    @last_call = time
end

#running?boolean

Returns true if the timer is currently running.

Returns:

  • (boolean)


84
85
86
# File 'lib/utilrb/event_loop.rb', line 84

def running?
    @event_loop.timer? self
end

#single_shot?Boolean

Returns true if the timer is a single shot timer.

Returns:

  • (Boolean)


127
128
129
# File 'lib/utilrb/event_loop.rb', line 127

def single_shot?
    @single_shot == true
end

#start(period = @period, instantly = true) ⇒ Timer

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.

Parameters:

  • period (Float) (defaults to: @period)

    The period in seconds

  • instantly (TrueClass, FalseClass) (defaults to: true)

    If set to true the timer instantly runs otherwise the timer waits until the first period passed.

Returns:

Raises:

  • (ArgumentError)

    if no period is specified



97
98
99
100
101
102
103
104
105
106
107
108
109
# 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

#stopped?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/utilrb/event_loop.rb', line 77

def stopped?
    @stopped 
end

#timeout?(time = Time.now) ⇒ Boolean

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

Parameters:

  • time (Time) (defaults to: Time.now)

    The time used for checking

Returns:

  • (Boolean)


116
117
118
119
120
121
122
# 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