Class: Warchat::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/warchat/timer.rb

Instance Method Summary collapse

Constructor Details

#initialize(interval, &handler) ⇒ Timer

Returns a new instance of Timer.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/warchat/timer.rb', line 6

def initialize(interval, &handler)
  
  raise ArgumentError, "Illegal interval" if interval < 0
  @interval = interval
  extend MonitorMixin
  @run = true
  @th = Thread.new do
    while run?
      if do_sleep 
        begin
          handler.call
        rescue Exception => e
          Warchat.debug e.message
          Warchat.debug e.backtrace
        end
      end
    end
  end
  @th['name'] = 'Timer'
end

Instance Method Details

#stopObject



27
28
29
30
31
32
# File 'lib/warchat/timer.rb', line 27

def stop
  synchronize do
    @run = false
  end
  sleeping? and @th.kill or @th.join 
end