Class: Philiprehberger::FileWatcher::Watcher
- Inherits:
-
Object
- Object
- Philiprehberger::FileWatcher::Watcher
- Defined in:
- lib/philiprehberger/file_watcher/watcher.rb
Overview
Watches file system paths for changes using polling.
Detects created, modified, and deleted files by comparing mtime snapshots at a configurable interval.
Instance Method Summary collapse
-
#initialize(paths, interval: 1.0, glob: '**/*', exclude: [], debounce: nil) ⇒ Watcher
constructor
A new instance of Watcher.
-
#on(type) {|Change| ... } ⇒ self
Register a callback for a specific change type.
-
#pause ⇒ self
Pause change detection.
-
#paused? ⇒ Boolean
True if the watcher is currently paused.
-
#resume ⇒ self
Resume change detection with a fresh snapshot.
-
#running? ⇒ Boolean
True if the watcher is currently running.
-
#snapshot ⇒ Hash{String => Hash}
Return a hash of all currently tracked files with their mtime and size.
-
#start ⇒ self
Start watching in a background thread.
-
#stats ⇒ Hash
Return runtime statistics for the watcher.
-
#stop ⇒ self
Stop the watcher and join the background thread.
Constructor Details
#initialize(paths, interval: 1.0, glob: '**/*', exclude: [], debounce: nil) ⇒ Watcher
Returns a new instance of Watcher.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/philiprehberger/file_watcher/watcher.rb', line 17 def initialize(paths, interval: 1.0, glob: '**/*', exclude: [], debounce: nil) @paths = Array(paths) @interval = interval @glob = glob @exclude = Array(exclude) @debounce = debounce @callbacks = { created: [], modified: [], deleted: [], any: [], error: [], batch: [] } @mutex = Mutex.new @thread = nil @running = false @paused = false @snapshot = {} @pending_debounce = {} @total_changes = 0 @last_change_at = nil end |
Instance Method Details
#on(type) {|Change| ... } ⇒ self
Register a callback for a specific change type.
42 43 44 45 46 47 48 49 50 |
# File 'lib/philiprehberger/file_watcher/watcher.rb', line 42 def on(type, &block) unless @callbacks.key?(type) raise ArgumentError, "invalid event type: #{type.inspect} (must be one of #{@callbacks.keys.join(', ')})" end @mutex.synchronize { @callbacks[type] << block } self end |
#pause ⇒ self
Pause change detection. The polling thread stays alive but skips detection.
87 88 89 90 |
# File 'lib/philiprehberger/file_watcher/watcher.rb', line 87 def pause @mutex.synchronize { @paused = true } self end |
#paused? ⇒ Boolean
Returns true if the watcher is currently paused.
107 108 109 |
# File 'lib/philiprehberger/file_watcher/watcher.rb', line 107 def paused? @mutex.synchronize { @paused } end |
#resume ⇒ self
Resume change detection with a fresh snapshot.
Changes that occurred while paused are silently ignored.
97 98 99 100 101 102 103 104 |
# File 'lib/philiprehberger/file_watcher/watcher.rb', line 97 def resume @mutex.synchronize do @snapshot = take_snapshot @pending_debounce.clear @paused = false end self end |
#running? ⇒ Boolean
Returns true if the watcher is currently running.
80 81 82 |
# File 'lib/philiprehberger/file_watcher/watcher.rb', line 80 def running? @mutex.synchronize { @running } end |
#snapshot ⇒ Hash{String => Hash}
Return a hash of all currently tracked files with their mtime and size.
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/philiprehberger/file_watcher/watcher.rb', line 114 def snapshot @mutex.synchronize do @snapshot.each_with_object({}) do |(path, mtime), result| size = begin File.size(path) rescue StandardError 0 end result[path] = { mtime: mtime, size: size } end end end |
#start ⇒ self
Start watching in a background thread.
Takes an initial snapshot and begins polling for changes.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/philiprehberger/file_watcher/watcher.rb', line 57 def start @mutex.synchronize do return self if @running @running = true @snapshot = take_snapshot @thread = Thread.new { poll_loop } end self end |
#stats ⇒ Hash
Return runtime statistics for the watcher.
The returned hash is a fresh copy on each call and safe to mutate.
133 134 135 136 137 138 139 140 141 |
# File 'lib/philiprehberger/file_watcher/watcher.rb', line 133 def stats @mutex.synchronize do { tracked_files: @snapshot.size, total_changes: @total_changes, last_change_at: @last_change_at } end end |
#stop ⇒ self
Stop the watcher and join the background thread.
71 72 73 74 75 76 77 |
# File 'lib/philiprehberger/file_watcher/watcher.rb', line 71 def stop @mutex.synchronize { @running = false } @thread&.join @thread = nil flush_debounced_changes self end |