Sha256: 5a2dd4485ceb5576117c15945cc55b95286f9175c8431276fb1166c5e981930f

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

require "set"
require "pathname"
require "mutex_m"

module Spring
  module Watcher
    # A user of a watcher can use IO.select to wait for changes:
    #
    #   watcher = MyWatcher.new(root, latency)
    #   IO.select([watcher]) # watcher is running in background
    #   watcher.stale? # => true
    class Abstract
      include Mutex_m

      attr_reader :files, :directories, :root, :latency

      def initialize(root, latency)
        super()

        @root        = File.realpath(root)
        @latency     = latency
        @files       = Set.new
        @directories = Set.new
        @stale       = false
        @io_listener = nil
      end

      def add(*items)
        items = items.flatten.map do |item|
          item = Pathname.new(item)

          if item.relative?
            Pathname.new("#{root}/#{item}")
          else
            item
          end
        end

        items = items.select(&:exist?)

        synchronize {
          items.each do |item|
            if item.directory?
              directories << item.realpath.to_s
            else
              files << item.realpath.to_s
            end
          end

          subjects_changed
        }
      end

      def stale?
        @stale
      end

      def mark_stale
        @stale = true
        @io_listener.write "." if @io_listener
      end

      def to_io
        read, write = IO.pipe
        @io_listener = write
        read
      end

      def restart
        stop
        start
      end

      def start
        raise NotImplementedError
      end

      def stop
        raise NotImplementedError
      end

      def subjects_changed
        raise NotImplementedError
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spring-1.1.0.beta1 lib/spring/watcher/abstract.rb
spring-1.0.0 lib/spring/watcher/abstract.rb
spring-0.9.2 lib/spring/watcher/abstract.rb
spring-0.9.1 lib/spring/watcher/abstract.rb
spring-0.9.0 lib/spring/watcher/abstract.rb
spring-0.0.11 lib/spring/watcher/abstract.rb