Moonshark/core/watchers/modulewatcher.go
2025-03-22 14:16:45 -05:00

51 lines
1.2 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 does minimal work
dirCopy := dir // Capture for closure
callback := func() error {
log.Debug("Detected changes in Lua module directory: %s", dirCopy)
// Completely reset the cache to match fresh-start performance
luaRunner.RequireCache().Clear()
// Force reset of Lua's module registry
luaRunner.ResetPackageLoaded()
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
}