class Lita::Timer

A timer that executes a block after a certain number of seconds, either once or repeatedly. @since 3.0.0

Public Class Methods

new(interval: 0, recurring: false, &block) click to toggle source

@param interval [Integer] The number of seconds to wait before calling the block. @param recurring [Boolean] If true, the timer will fire repeatedly until stopped. @yieldparam timer [Lita::Timer] The current {Lita::Timer} instance.

# File lib/lita/timer.rb, line 8
def initialize(interval: 0, recurring: false, &block)
  @interval = interval
  @recurring = recurring
  @running = false
  @block = block
end

Public Instance Methods

start() click to toggle source

Starts running the timer.

# File lib/lita/timer.rb, line 16
def start
  @running = true
  run
end
stop() click to toggle source

Stops the timer, preventing any further invocations of the block until started again.

# File lib/lita/timer.rb, line 22
def stop
  @running = false
end

Private Instance Methods

recurring?() click to toggle source

Is this a recurring timer?

# File lib/lita/timer.rb, line 29
def recurring?
  @recurring
end
run() click to toggle source

Sleep for the given interval, call the block, then run again if it's a recurring timer.

# File lib/lita/timer.rb, line 34
def run
  loop do
    sleep @interval
    @block.call(self) if running? && @block
    break unless running? && recurring?
  end
end
running?() click to toggle source

Is the timer currently running?

# File lib/lita/timer.rb, line 43
def running?
  @running
end