class Listen::Adapters::Polling

Polling Adapter that works cross-platform and has no dependencies. This is the adapter that uses the most CPU processing power and has higher file IO that the other implementations.

Public Class Methods

new(directories, options = {}, &callback) click to toggle source

Initialize the Adapter. See {Listen::Adapter#initialize} for more info.

Calls superclass method Listen::Adapter.new
# File lib/listen/adapters/polling.rb, line 16
def initialize(directories, options = {}, &callback)
  @latency ||= DEFAULT_POLLING_LATENCY
  super
end

Public Instance Methods

start(blocking = true) click to toggle source

Start the adapter.

@param [Boolean] blocking whether or not to block the current thread after starting

Calls superclass method Listen::Adapter#start
# File lib/listen/adapters/polling.rb, line 25
def start(blocking = true)
  @mutex.synchronize do
    return if @stop == false
    super
  end

  @poll_thread = Thread.new { poll }
  @poll_thread.join if blocking
end
stop() click to toggle source

Stop the adapter.

Calls superclass method Listen::Adapter#stop
# File lib/listen/adapters/polling.rb, line 37
def stop
  @mutex.synchronize do
    return if @stop == true
    super
  end

  @poll_thread.join
end

Private Instance Methods

poll() click to toggle source

Poll listener directory for file system changes.

# File lib/listen/adapters/polling.rb, line 50
def poll
  until @stop
    sleep(0.1) && next if @paused

    start = Time.now.to_f
    @callback.call(@directories.dup, :recursive => true)
    @turnstile.signal
    nap_time = @latency - (Time.now.to_f - start)
    sleep(nap_time) if nap_time > 0
  end
rescue Interrupt
end