class Sweatpants::Timer

Attributes

blocks[R]

Public Class Methods

new(frequency) click to toggle source
# File lib/sweatpants/timer.rb, line 4
def initialize frequency
  @blocks = []
  @frequency = frequency
  spawn_tick_thread
end

Public Instance Methods

call_blocks() click to toggle source
# File lib/sweatpants/timer.rb, line 19
def call_blocks
  @blocks.each &:call
end
on_tick(&block) click to toggle source
# File lib/sweatpants/timer.rb, line 23
def on_tick &block
  raise 'Timer#register requires a block' unless block
  @blocks << block 
end
spawn_tick_thread() click to toggle source
# File lib/sweatpants/timer.rb, line 10
def spawn_tick_thread
  Thread.new do 
    while true do
      sleep @frequency
      call_blocks
    end
  end
end