File: polling.rb

package info (click to toggle)
ruby-listen 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544 kB
  • sloc: ruby: 5,033; makefile: 9
file content (40 lines) | stat: -rw-r--r-- 1,036 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

module Listen
  module Adapter
    # 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 than the other implementations.
    #
    class Polling < Base
      OS_REGEXP = //.freeze # match every OS

      DEFAULTS = { latency: 1.0, wait_for_delay: 0.05 }.freeze

      private

      def _configure(_, &callback)
        @polling_callbacks ||= []
        @polling_callbacks << callback
      end

      def _run
        loop do
          start = MonotonicTime.now
          @polling_callbacks.each do |callback|
            callback.call(nil)
            if (nap_time = options.latency - (MonotonicTime.now - start)) > 0
              # TODO: warn if nap_time is negative (polling too slow)
              sleep(nap_time)
            end
          end
        end
      end

      def _process_event(dir, _)
        _queue_change(:dir, dir, '.', recursive: true)
      end
    end
  end
end