Class: Ichiban::Watcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Watcher

Returns a new instance of Watcher.



3
4
5
6
7
8
9
# File 'lib/ichiban/watcher.rb', line 3

def initialize(options = {})
  @options = {
    :latency => 0.5
  }.merge(options)
  @listen_event_log = []
  @loader = Ichiban::Loader.new
end

Instance Attribute Details

#listen_event_logObject (readonly)

Returns the value of attribute listen_event_log.



13
14
15
# File 'lib/ichiban/watcher.rb', line 13

def listen_event_log
  @listen_event_log
end

#listenerObject (readonly)

Returns the value of attribute listener.



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

def listener
  @listener
end

Instance Method Details

#on_change(modified, added, deleted) ⇒ Object

The test suite calls this method directly to bypass the Listen gem for certain tests.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ichiban/watcher.rb', line 17

def on_change(modified, added, deleted)
  # Modifications and additions are treated the same.
  (modified + added).uniq.each do |path|
    if file = Ichiban::ProjectFile.from_abs(path)
      begin
        @loader.change(file) # Tell the Loader that this file has changed
        file.update
      rescue Exception => exc
        Ichiban.logger.exception(exc)
      end
    end
  end
  
  # Deletions are handled specially.
  deleted.each do |path|
    Ichiban::Deleter.new.delete_dest(path)
  end
  
  # Finally, propagate this change to any dependent files.
  (modified + added + deleted).uniq.each do |path|
    begin
      Ichiban::Dependencies.propagate(path)
    rescue => exc
      Ichiban.logger.exception(exc)
    end
  end
end

#startObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ichiban/watcher.rb', line 45

def start
  Ichiban.logger.out 'Starting watcher'
  begin
    @listener = Listen.to(
      Ichiban.project_root,
      ignore: /.listen_test$/,
      latency: @options[:latency]
    ) do |modified, added, deleted|
      @listen_event_log << [modified, added, deleted]
      on_change modified, added, deleted
    end
    @listener.start
    sleep
  rescue Interrupt
    stop
    exit 0
  end
end

#stopObject



64
65
66
67
68
69
70
# File 'lib/ichiban/watcher.rb', line 64

def stop
  if @listener
    Ichiban.logger.out "Stopping watcher"
    @listener.stop
    @listener = nil
  end
end