Module: Denko::Behaviors::Poller

Constant Summary

Constants included from Reader

Reader::READ_WAIT_TIME

Constants included from Lifecycle

Lifecycle::CALLBACK_METHODS

Instance Attribute Summary

Attributes included from Threaded

#interrupts_enabled, #thread

Attributes included from State

#state

Instance Method Summary collapse

Methods included from Threaded

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

Methods included from Reader

#_read, #read, #read_busy?, #read_nb, #read_raw, #read_using, #update

Methods included from Callbacks

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

Methods included from State

#update_state

Methods included from Lifecycle

included

Instance Method Details

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



30
31
32
# File 'lib/denko/behaviors/poller.rb', line 30

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
20
21
22
23
24
25
26
27
28
# File 'lib/denko/behaviors/poller.rb', line 7

def poll_using(method, interval, *args, &block)
  mruby_thread_check

  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
    # Lock, THEN wait for other normal reads to finish.
    @reader_mutex.lock
    sleep 0.001 while read_busy?
    @reading_normally = true

    method.call(*args)
    @reader_mutex.unlock

    sleep interval
  end
end

#stopObject



34
35
36
37
# File 'lib/denko/behaviors/poller.rb', line 34

def stop
  begin; super; rescue NoMethodError; end
  remove_callbacks :poll
end