Class: PromptObjects::Server::FileWatcher
- Inherits:
-
Object
- Object
- PromptObjects::Server::FileWatcher
- Defined in:
- lib/prompt_objects/server/file_watcher.rb
Overview
Watches the environment directory for file changes and notifies subscribers. This enables live updates when POs are created/modified/deleted.
Instance Method Summary collapse
-
#initialize(runtime:, env_path:) ⇒ FileWatcher
constructor
A new instance of FileWatcher.
- #start ⇒ Object
- #stop ⇒ Object
- #subscribe(&block) ⇒ Object
- #unsubscribe(block) ⇒ Object
Constructor Details
#initialize(runtime:, env_path:) ⇒ FileWatcher
Returns a new instance of FileWatcher.
10 11 12 13 14 15 |
# File 'lib/prompt_objects/server/file_watcher.rb', line 10 def initialize(runtime:, env_path:) @runtime = runtime @env_path = env_path @subscribers = [] @listener = nil end |
Instance Method Details
#start ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/prompt_objects/server/file_watcher.rb', line 17 def start objects_dir = File.join(@env_path, "objects") unless Dir.exist?(objects_dir) puts "FileWatcher: objects directory not found at #{objects_dir}" return end @listener = Listen.to(objects_dir, only: /\.md$/) do |modified, added, removed| handle_changes(modified: modified, added: added, removed: removed) end @listener.start puts "FileWatcher: watching #{objects_dir} for changes" end |
#stop ⇒ Object
33 34 35 |
# File 'lib/prompt_objects/server/file_watcher.rb', line 33 def stop @listener&.stop end |
#subscribe(&block) ⇒ Object
37 38 39 |
# File 'lib/prompt_objects/server/file_watcher.rb', line 37 def subscribe(&block) @subscribers << block end |
#unsubscribe(block) ⇒ Object
41 42 43 |
# File 'lib/prompt_objects/server/file_watcher.rb', line 41 def unsubscribe(block) @subscribers.delete(block) end |