Class: Spring::Watcher::Polling

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

Instance Attribute Summary collapse

Attributes inherited from Abstract

#directories, #files, #latency, #root

Instance Method Summary collapse

Methods inherited from Abstract

#mark_stale, #restart, #stale?, #to_io

Constructor Details

#initialize(root, latency) ⇒ Polling

Returns a new instance of Polling.



6
7
8
9
10
# File 'lib/spring/watcher/polling.rb', line 6

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

Instance Attribute Details

#mtimeObject (readonly)

Returns the value of attribute mtime.



4
5
6
# File 'lib/spring/watcher/polling.rb', line 4

def mtime
  @mtime
end

Instance Method Details

#addObject



16
17
18
19
# File 'lib/spring/watcher/polling.rb', line 16

def add(*)
  check_stale if @poller
  super
end

#check_staleObject



12
13
14
# File 'lib/spring/watcher/polling.rb', line 12

def check_stale
  synchronize { mark_stale if mtime < compute_mtime }
end

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spring/watcher/polling.rb', line 21

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

      loop do
        Kernel.sleep latency
        check_stale
      end
    }
  end
end

#stopObject



34
35
36
37
38
39
# File 'lib/spring/watcher/polling.rb', line 34

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

#subjects_changedObject



41
42
43
# File 'lib/spring/watcher/polling.rb', line 41

def subjects_changed
  @mtime = compute_mtime
end