Class: Utilrb::EventLoop::Timer
Overview
Timer for the Utilrb::EventLoop which supports single shot and periodic activation
Instance Attribute Summary collapse
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#event_loop ⇒ Object
Returns the value of attribute event_loop.
-
#period ⇒ Object
Returns the value of attribute period.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#single_shot ⇒ Object
Returns the value of attribute single_shot.
Instance Method Summary collapse
-
#call(time = Time.now) ⇒ Object
Executes the code block tight to the timer and saves a time stamp.
-
#cancel ⇒ Object
(also: #stop)
Cancels the timer.
-
#initialize(event_loop, period = 0, single_shot = false, &block) ⇒ Timer
constructor
A timer.
-
#reset(time = Time.now) ⇒ Object
Resets the timer internal time to the given one.
-
#running? ⇒ boolean
Returns true if the timer is currently running.
-
#single_shot? ⇒ Boolean
Returns true if the timer is a single shot timer.
-
#start(period = @period, instantly = true) ⇒ Timer
Starts the timer by adding itself to the EventLoop the timer belongs to.
- #stopped? ⇒ Boolean
-
#timeout?(time = Time.now) ⇒ Boolean
Returns true if the timer should fire now.
Constructor Details
#initialize(event_loop, period = 0, single_shot = false, &block) ⇒ Timer
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
#doc ⇒ Object
Returns the value of attribute doc.
50 51 52 |
# File 'lib/utilrb/event_loop.rb', line 50 def doc @doc end |
#event_loop ⇒ Object
Returns the value of attribute event_loop.
48 49 50 |
# File 'lib/utilrb/event_loop.rb', line 48 def event_loop @event_loop end |
#period ⇒ Object
Returns the value of attribute period.
47 48 49 |
# File 'lib/utilrb/event_loop.rb', line 47 def period @period end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
49 50 51 |
# File 'lib/utilrb/event_loop.rb', line 49 def result @result end |
#single_shot ⇒ Object
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.
135 136 137 138 |
# File 'lib/utilrb/event_loop.rb', line 135 def call(time = Time.now) reset(time) @result = @block.call end |
#cancel ⇒ Object 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.
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.
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.
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.
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
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.
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 |