Module: Denko::Behaviors::Poller

Instance Attribute Summary

Attributes included from Threaded

#interrupts_enabled, #thread

Attributes included from Callbacks

#callback_mutex

Instance Method Summary collapse

Methods included from Threaded

#enable_interrupts, included, #stop_thread, #threaded, #threaded_loop

Methods included from Reader

#_read, #read, #read_using, #wait_for_read

Methods included from Callbacks

#add_callback, #callbacks, #initialize, #pre_callback_filter, #remove_callback, #update

Methods included from State

#initialize, #state

Instance Method Details

#poll(interval, *args, &block) ⇒ Object



21
22
23
# File 'lib/denko/behaviors/poller.rb', line 21

def poll(interval, *args, &block)
  poll_using(self.method(:_read), interval, *args, &block)
end

#poll_using(method, interval, *args, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/denko/behaviors/poller.rb', line 7

def poll_using(method, interval, *args, &block)
  unless [Integer, Float].include? interval.class
    raise ArgumentError, "wrong interval given to #poll : #{interval.inspect}"
  end

  stop
  add_callback(:poll, &block) if block_given?

  threaded_loop do
    method.call(*args)
    sleep interval
  end
end

#stopObject



25
26
27
28
# File 'lib/denko/behaviors/poller.rb', line 25

def stop
  super if defined?(super)
  remove_callbacks :poll
end