Class: RGossip::Timer
- Inherits:
-
Object
- Object
- RGossip::Timer
- Defined in:
- lib/rgossip/timer.rb
Instance Method Summary collapse
-
#initialize(timeout, &block) ⇒ Timer
constructor
A new instance of Timer.
- #reset ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(timeout, &block) ⇒ Timer
Returns a new instance of Timer.
3 4 5 6 |
# File 'lib/rgossip/timer.rb', line 3 def initialize(timeout, &block) @timeout = timeout @block = block end |
Instance Method Details
#reset ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/rgossip/timer.rb', line 28 def reset if is_thread_alive? @start_time = Time.now @thread.run end rescue ThreadError end |
#start ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rgossip/timer.rb', line 8 def start @thread.kill if is_thread_alive? @start_time = Time.now @thread = Thread.start(@start_time) {|start_time| loop do sleep @timeout if @start_time == start_time @block.call break elsif @start_time.nil? break else start_time = @start_time end end } end |
#stop ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/rgossip/timer.rb', line 36 def stop if is_thread_alive? @start_time = nil @thread.run end rescue ThreadError end |