Hilbish/golibs/fs/watcher.go

112 lines
2.0 KiB
Go
Raw Permalink Normal View History

package fs
import (
"fmt"
"github.com/rjeczalik/notify"
rt "github.com/arnodel/golua/runtime"
)
2024-12-22 16:39:15 +00:00
// #type
// Watcher type describes a `fs` library file watcher.
type watcher struct{
path string
callback *rt.Closure
paused bool
started bool
ud *rt.UserData
notifyChan chan notify.EventInfo
2024-12-22 16:46:40 +00:00
rtm *rt.Runtime
}
2024-12-22 16:39:15 +00:00
func (w *watcher) start() {
if w.callback == nil || w.started {
return
}
w.started = true
w.notifyChan = make(chan notify.EventInfo)
notify.Watch(w.path, w.notifyChan, notify.All)
go func() {
for notif := range w.notifyChan {
ev := notif.Event().String()
path := notif.Path()
2024-12-22 16:46:40 +00:00
_, err := rt.Call1(w.rtm.MainThread(), rt.FunctionValue(w.callback), rt.StringValue(ev), rt.StringValue(path))
if err != nil {
// TODO: throw error
}
}
}()
}
2024-12-22 16:39:15 +00:00
func (w *watcher) stop() {
w.started = false
notify.Stop(w.notifyChan)
}
2024-12-22 16:39:15 +00:00
// #member
// start()
// Start/resume file watching.
func watcherStart(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
pw, err := watcherArg(c, 0)
if err != nil {
return nil, err
}
pw.start()
return c.Next(), nil
}
2024-12-22 16:39:15 +00:00
// #member
// stop()
// Stops watching for changes. Effectively ignores changes.
func watcherStop(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
pw, err := watcherArg(c, 0)
if err != nil {
return nil, err
}
pw.stop()
return c.Next(), nil
}
2024-12-22 16:46:40 +00:00
func newWatcher(path string, callback *rt.Closure, rtm *rt.Runtime) *watcher {
2024-12-22 16:39:15 +00:00
pw := &watcher{
path: path,
2024-12-22 16:46:40 +00:00
rtm: rtm,
callback: callback,
}
pw.ud = watcherUserData(pw)
pw.start()
return pw
}
2024-12-22 16:39:15 +00:00
func watcherArg(c *rt.GoCont, arg int) (*watcher, error) {
j, ok := valueToWatcher(c.Arg(arg))
if !ok {
return nil, fmt.Errorf("#%d must be a watcher", arg + 1)
}
return j, nil
}
2024-12-22 16:39:15 +00:00
func valueToWatcher(val rt.Value) (*watcher, bool) {
u, ok := val.TryUserData()
if !ok {
return nil, false
}
2024-12-22 16:39:15 +00:00
j, ok := u.Value().(*watcher)
return j, ok
}
2024-12-22 16:39:15 +00:00
func watcherUserData(j *watcher) *rt.UserData {
2024-12-22 16:46:40 +00:00
watcherMeta := j.rtm.Registry(watcherMetaKey)
return rt.NewUserData(j, watcherMeta.AsTable())
}