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 }