mirror of https://github.com/Hilbis/Hilbish
docs: document fs watcher
parent
459c9baa89
commit
6fa8eea5e2
|
@ -329,6 +329,12 @@ func fstat(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
return c.PushingNext1(t.Runtime, rt.TableValue(statTbl)), nil
|
return c.PushingNext1(t.Runtime, rt.TableValue(statTbl)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// watch(path, callback)
|
||||||
|
// Watches a path for changes made to it. For example, to monitor
|
||||||
|
// new files created in a folder.
|
||||||
|
// The callback passed 2 string arguments, the `event` and the absolute `path`
|
||||||
|
// #param path string
|
||||||
|
// #param callback function
|
||||||
func fwatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func fwatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
if err := c.CheckNArgs(2); err != nil {
|
if err := c.CheckNArgs(2); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -7,7 +7,9 @@ import (
|
||||||
rt "github.com/arnodel/golua/runtime"
|
rt "github.com/arnodel/golua/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pathWatcher struct{
|
// #type
|
||||||
|
// Watcher type describes a `fs` library file watcher.
|
||||||
|
type watcher struct{
|
||||||
path string
|
path string
|
||||||
callback *rt.Closure
|
callback *rt.Closure
|
||||||
paused bool
|
paused bool
|
||||||
|
@ -16,7 +18,7 @@ type pathWatcher struct{
|
||||||
notifyChan chan notify.EventInfo
|
notifyChan chan notify.EventInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *pathWatcher) start() {
|
func (w *watcher) start() {
|
||||||
if w.callback == nil || w.started {
|
if w.callback == nil || w.started {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -38,11 +40,14 @@ func (w *pathWatcher) start() {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *pathWatcher) stop() {
|
func (w *watcher) stop() {
|
||||||
w.started = false
|
w.started = false
|
||||||
notify.Stop(w.notifyChan)
|
notify.Stop(w.notifyChan)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #member
|
||||||
|
// start()
|
||||||
|
// Start/resume file watching.
|
||||||
func watcherStart(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func watcherStart(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
pw, err := watcherArg(c, 0)
|
pw, err := watcherArg(c, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -54,6 +59,9 @@ func watcherStart(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
return c.Next(), nil
|
return c.Next(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #member
|
||||||
|
// stop()
|
||||||
|
// Stops watching for changes. Effectively ignores changes.
|
||||||
func watcherStop(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func watcherStop(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
pw, err := watcherArg(c, 0)
|
pw, err := watcherArg(c, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -65,8 +73,8 @@ func watcherStop(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
return c.Next(), nil
|
return c.Next(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWatcher(path string, callback *rt.Closure) *pathWatcher {
|
func newWatcher(path string, callback *rt.Closure) *watcher {
|
||||||
pw := &pathWatcher{
|
pw := &watcher{
|
||||||
path: path,
|
path: path,
|
||||||
callback: callback,
|
callback: callback,
|
||||||
}
|
}
|
||||||
|
@ -76,7 +84,7 @@ func newWatcher(path string, callback *rt.Closure) *pathWatcher {
|
||||||
return pw
|
return pw
|
||||||
}
|
}
|
||||||
|
|
||||||
func watcherArg(c *rt.GoCont, arg int) (*pathWatcher, error) {
|
func watcherArg(c *rt.GoCont, arg int) (*watcher, error) {
|
||||||
j, ok := valueToWatcher(c.Arg(arg))
|
j, ok := valueToWatcher(c.Arg(arg))
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("#%d must be a watcher", arg + 1)
|
return nil, fmt.Errorf("#%d must be a watcher", arg + 1)
|
||||||
|
@ -85,17 +93,17 @@ func watcherArg(c *rt.GoCont, arg int) (*pathWatcher, error) {
|
||||||
return j, nil
|
return j, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func valueToWatcher(val rt.Value) (*pathWatcher, bool) {
|
func valueToWatcher(val rt.Value) (*watcher, bool) {
|
||||||
u, ok := val.TryUserData()
|
u, ok := val.TryUserData()
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
j, ok := u.Value().(*pathWatcher)
|
j, ok := u.Value().(*watcher)
|
||||||
return j, ok
|
return j, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func watcherUserData(j *pathWatcher) *rt.UserData {
|
func watcherUserData(j *watcher) *rt.UserData {
|
||||||
watcherMeta := rtmm.Registry(watcherMetaKey)
|
watcherMeta := rtmm.Registry(watcherMetaKey)
|
||||||
return rt.NewUserData(j, watcherMeta.AsTable())
|
return rt.NewUserData(j, watcherMeta.AsTable())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue