Class: Core::Watch::System

Inherits:
Object
  • Object
show all
Includes:
Is::Async
Defined in:
lib/core/watch/system.rb

Overview

public

The system watcher.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval: 0.1, strategy: :digest) ⇒ System

Returns a new instance of System.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/core/watch/system.rb', line 30

def initialize(interval: 0.1, strategy: :digest)
  @interval = interval
  @status = Status.new
  @mutex = Mutex.new
  @watched = []
  @ignored = []
  @callbacks = []
  @working = false
  @latency = nil

  @strategy = case strategy.to_sym
  when :digest
    require_relative "strategies/digest"
    Strategies::Digest
  when :timestamp
    require_relative "strategies/timestamp"
    Strategies::Timestamp
  else
    raise ArgumentError, "unknown watch strategy `#{strategy.inspect}'"
  end
end

Instance Attribute Details

#intervalObject

public

The configured polling interval.



20
21
22
# File 'lib/core/watch/system.rb', line 20

def interval
  @interval
end

#latencyObject (readonly)

public

The current latency.



28
29
30
# File 'lib/core/watch/system.rb', line 28

def latency
  @latency
end

#statusObject (readonly)

public

The status object.



24
25
26
# File 'lib/core/watch/system.rb', line 24

def status
  @status
end

Instance Method Details

#callback(matcher = nil, &block) ⇒ Object

public

Register a callback to be called when a change is detected. The callback will only be called once per

tick and will be given a ‘Core::Watch::Diff` instance. If `matcher` is passed, the callback will only be called for matching changes. The matcher can be a string or any object that responds to `match?`.

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
# File 'lib/core/watch/system.rb', line 84

def callback(matcher = nil, &block)
  raise ArgumentError, "expected a block" if block.nil?

  @mutex.synchronize do
    @callbacks << Callback.build(matcher, &block)
  end
end

#ignore(path) ⇒ Object

public

Ignore changes at ‘path`.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/core/watch/system.rb', line 68

def ignore(path)
  path = path.to_s

  @mutex.synchronize do
    unless @ignored.include?(path)
      mutate do
        @ignored << path
      end
    end
  end
end

#pauseObject

public

Pause the watcher, causing it to continue running but not invoke callbacks.



114
115
116
# File 'lib/core/watch/system.rb', line 114

def pause
  @status.paused!
end

#resumeObject

public

Resume a paused watcher.



120
121
122
# File 'lib/core/watch/system.rb', line 120

def resume
  @status.running!
end

#startObject

public

Start the watcher.



94
95
96
97
98
99
100
# File 'lib/core/watch/system.rb', line 94

def start
  await do
    unless @status.running?
      @future = run
    end
  end
end

#stop(force: false) ⇒ Object

public

Stop the watcher.



104
105
106
107
108
109
110
# File 'lib/core/watch/system.rb', line 104

def stop(force: false)
  if !@working || force
    @future&.cancel
  end

  @status.stopped!
end

#watch(path) ⇒ Object

public

Watch ‘path` for changes.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/core/watch/system.rb', line 54

def watch(path)
  path = Pathname(path).expand_path

  @mutex.synchronize do
    unless @watched.include?(path)
      mutate do
        @watched << path
      end
    end
  end
end