Moonshark/core/watchers/modulewatcher.go
2025-03-21 22:25:05 -05:00

49 lines
1.3 KiB
Go

package watchers
import (
"git.sharkk.net/Sky/Moonshark/core/logger"
"git.sharkk.net/Sky/Moonshark/core/runner"
)
// WatchLuaModules sets up an optimized watcher for Lua module directories
func WatchLuaModules(luaRunner *runner.LuaRunner, libDirs []string, log *logger.Logger) ([]*Watcher, error) {
watchers := make([]*Watcher, 0, len(libDirs))
for _, dir := range libDirs {
// Create a directory-specific callback that only signals changes
// without doing heavy processing in the callback itself
dirCopy := dir // Capture for closure
callback := func() error {
log.Debug("Detected changes in Lua module directory: %s", dirCopy)
// Only mark that refresh is needed instead of doing refresh now
luaRunner.RequireCache().MarkNeedsRefresh()
return nil
}
config := WatcherConfig{
Dir: dir,
Callback: callback,
Log: log,
Recursive: true,
Adaptive: true,
// Use a longer debounce time to avoid too frequent updates
DebounceTime: defaultDebounceTime * 4,
}
watcher, err := WatchDirectory(config)
if err != nil {
// Close any watchers we've already created
for _, w := range watchers {
w.Close()
}
return nil, err
}
watchers = append(watchers, watcher)
log.Info("Started watching Lua modules directory: %s", dir)
}
return watchers, nil
}