Sha256: 7380e18edac1d994610eee2c511770e8ac5d162daa3f69ab3f0ed17398746946

Contents?: true

Size: 1.64 KB

Versions: 32

Compression:

Stored size: 1.64 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
        @listeners   = []
      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 on_stale(&block)
        @listeners << block
      end

      def mark_stale
        return if stale?
        @stale = true
        @listeners.each(&:call)
      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

32 entries across 32 versions & 2 rubygems

Version Path
spring-2.0.1 lib/spring/watcher/abstract.rb
spring-2.0.0 lib/spring/watcher/abstract.rb
spring-1.7.2 lib/spring/watcher/abstract.rb
spring-1.7.1 lib/spring/watcher/abstract.rb
spring-1.7.0 lib/spring/watcher/abstract.rb
spring-1.6.4 lib/spring/watcher/abstract.rb
spring-1.6.3 lib/spring/watcher/abstract.rb
spring-1.6.2 lib/spring/watcher/abstract.rb
spring-1.6.1 lib/spring/watcher/abstract.rb
spring-1.6.0 lib/spring/watcher/abstract.rb
spring-1.5.0 lib/spring/watcher/abstract.rb
spring-1.4.4 lib/spring/watcher/abstract.rb
spring-jruby-1.4.3 lib/spring-jruby/watcher/abstract.rb
spring-1.4.3 lib/spring/watcher/abstract.rb
spring-1.4.2 lib/spring/watcher/abstract.rb
spring-1.4.1 lib/spring/watcher/abstract.rb
spring-1.4.0 lib/spring/watcher/abstract.rb
spring-1.3.6 lib/spring/watcher/abstract.rb
spring-1.3.5 lib/spring/watcher/abstract.rb
spring-1.3.4 lib/spring/watcher/abstract.rb