Class: Spring::Watcher::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/spring/watcher/abstract.rb

Overview

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

Direct Known Subclasses

Polling

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, latency) ⇒ Abstract

Returns a new instance of Abstract.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/spring/watcher/abstract.rb', line 13

def initialize(root, latency)
  @mutex       = Mutex.new
  @root        = File.realpath(root)
  @latency     = latency
  @files       = {}
  @directories = {}
  @stale       = false
  @listeners   = []

  @on_debug    = nil
end

Instance Attribute Details

#directoriesObject (readonly)

Returns the value of attribute directories.



11
12
13
# File 'lib/spring/watcher/abstract.rb', line 11

def directories
  @directories
end

#filesObject (readonly)

Returns the value of attribute files.



11
12
13
# File 'lib/spring/watcher/abstract.rb', line 11

def files
  @files
end

#latencyObject (readonly)

Returns the value of attribute latency.



11
12
13
# File 'lib/spring/watcher/abstract.rb', line 11

def latency
  @latency
end

#rootObject (readonly)

Returns the value of attribute root.



11
12
13
# File 'lib/spring/watcher/abstract.rb', line 11

def root
  @root
end

Instance Method Details

#add(*items) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/spring/watcher/abstract.rb', line 38

def add(*items)
  debug { "watcher: add: #{items.inspect}" }

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

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

  items = items.select do |item|
    if item.symlink?
      item.readlink.exist?.tap do |exists|
        if !exists
          debug { "add: ignoring dangling symlink: #{item.inspect} -> #{item.readlink.inspect}" }
        end
      end
    else
      item.exist?
    end
  end

  @mutex.synchronize do
    items.each do |item|
      if item.directory?
        directories[item.realpath.to_s] = true
      else
        begin
          files[item.realpath.to_s] = true
        rescue Errno::ENOENT
          # Race condition. Ignore symlinks whose target was removed
          # since the check above, or are deeply chained.
          debug { "add: ignoring now-dangling symlink: #{item.inspect} -> #{item.readlink.inspect}" }
        end
      end
    end

    subjects_changed
  end
end

#debugObject



34
35
36
# File 'lib/spring/watcher/abstract.rb', line 34

def debug
  @on_debug.call(yield) if @on_debug
end

#mark_staleObject



91
92
93
94
95
96
# File 'lib/spring/watcher/abstract.rb', line 91

def mark_stale
  return if stale?
  @stale = true
  debug { "marked stale, calling listeners: listeners=#{@listeners.inspect}" }
  @listeners.each(&:call)
end

#on_debug(&block) ⇒ Object



30
31
32
# File 'lib/spring/watcher/abstract.rb', line 30

def on_debug(&block)
  @on_debug = block
end

#on_stale(&block) ⇒ Object



86
87
88
89
# File 'lib/spring/watcher/abstract.rb', line 86

def on_stale(&block)
  debug { "added listener: #{block.inspect}" }
  @listeners << block
end

#restartObject



98
99
100
101
102
# File 'lib/spring/watcher/abstract.rb', line 98

def restart
  debug { "restarting" }
  stop
  start
end

#stale?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/spring/watcher/abstract.rb', line 82

def stale?
  @stale
end

#startObject

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/spring/watcher/abstract.rb', line 104

def start
  raise NotImplementedError
end

#stopObject

Raises:

  • (NotImplementedError)


108
109
110
# File 'lib/spring/watcher/abstract.rb', line 108

def stop
  raise NotImplementedError
end

#subjects_changedObject

Raises:

  • (NotImplementedError)


112
113
114
# File 'lib/spring/watcher/abstract.rb', line 112

def subjects_changed
  raise NotImplementedError
end

#synchronize(&block) ⇒ Object



25
26
27
28
# File 'lib/spring/watcher/abstract.rb', line 25

def synchronize(&block)
  # Used by some gems.
  @mutex.synchronize(&block)
end