Class: PeriodicTimer

Inherits:
Timer show all
Defined in:
lib/infra/timer.rb

Instance Method Summary collapse

Methods inherited from Timer

#cancel, #initialize, #reset, #running?, start

Constructor Details

This class inherits a constructor from Timer

Instance Method Details

#last_shotObject



100
101
102
# File 'lib/infra/timer.rb', line 100

def last_shot
  @continue = false
end

#start(_interval = @interval, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/infra/timer.rb', line 85

def start _interval=@interval, &block
  @continue = true
  changed and notify_observers(:ev_timer_start, id, Time.now.strftime("%M:%S"))
  _code = block || @code
  @_timer_thread_ = Thread.new(_interval, _code) do |interval, code|
    loop do
      sleep(interval)
      code.call if code
      changed and notify_observers(:ev_timer_fire, id, Time.now.strftime("%M:%S"))
      break unless @continue
    end
    changed and notify_observers(:ev_timer_stop, id, Time.now.strftime("%M:%S"))
  end
  self
end