Sha256: e294e5d047c6b05a18a276be5e467daeff49a2e4dfb0088ffccee7395f7e2839

Contents?: true

Size: 1.14 KB

Versions: 21

Compression:

Stored size: 1.14 KB

Contents

require "spring/watcher"

module Spring
  module Watcher
    class Polling < Abstract
      attr_reader :mtime

      def initialize(root, latency)
        super
        @mtime  = 0
        @poller = nil
      end

      def check_stale
        synchronize { mark_stale if mtime < compute_mtime }
      end

      def add(*)
        check_stale if @poller
        super
      end

      def start
        unless @poller
          @poller = Thread.new {
            Thread.current.abort_on_exception = true

            loop do
              Kernel.sleep latency
              check_stale
            end
          }
        end
      end

      def stop
        if @poller
          @poller.kill
          @poller = nil
        end
      end

      def subjects_changed
        @mtime = compute_mtime
      end

      private

      def compute_mtime
        expanded_files.map { |f| File.mtime(f).to_f }.max || 0
      rescue Errno::ENOENT
        # if a file does no longer exist, the watcher is always stale.
        Float::MAX
      end

      def expanded_files
        files + Dir["{#{directories.map { |d| "#{d}/**/*" }.join(",")}}"]
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
spring-1.7.1 lib/spring/watcher/polling.rb
spring-1.7.0 lib/spring/watcher/polling.rb
spring-1.6.4 lib/spring/watcher/polling.rb
spring-1.6.3 lib/spring/watcher/polling.rb
spring-1.6.2 lib/spring/watcher/polling.rb
spring-1.6.1 lib/spring/watcher/polling.rb
spring-1.6.0 lib/spring/watcher/polling.rb
spring-1.5.0 lib/spring/watcher/polling.rb
spring-1.4.4 lib/spring/watcher/polling.rb
spring-1.4.3 lib/spring/watcher/polling.rb
spring-1.4.2 lib/spring/watcher/polling.rb
spring-1.4.1 lib/spring/watcher/polling.rb
spring-1.4.0 lib/spring/watcher/polling.rb
spring-1.3.6 lib/spring/watcher/polling.rb
spring-1.3.5 lib/spring/watcher/polling.rb
spring-1.3.4 lib/spring/watcher/polling.rb
spring-1.3.3 lib/spring/watcher/polling.rb
spring-1.3.2 lib/spring/watcher/polling.rb
spring-1.3.1 lib/spring/watcher/polling.rb
spring-1.3.0 lib/spring/watcher/polling.rb