Moonshark/core/watchers/modulewatcher.go
2025-03-22 16:39:13 -05:00

51 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 identifies changed files
dirCopy := dir // Capture for closure
callback := func() error {
log.Debug("Detected changes in Lua module directory: %s", dirCopy)
// Instead of clearing everything, use directory-level smart refresh
// This will scan lib directory and refresh all modified Lua modules
if err := luaRunner.ReloadAllModules(); err != nil {
log.Warning("Error reloading modules: %v", err)
}
return nil
}
config := WatcherConfig{
Dir: dir,
Callback: callback,
Log: log,
Recursive: true,
Adaptive: true,
DebounceTime: defaultDebounceTime,
}
watcher, err := WatchDirectory(config)
if err != nil {
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
}