From b06e420b40d86d100281afbabb2fa14e4ebc0cf4 Mon Sep 17 00:00:00 2001 From: Star <> Date: Thu, 4 Jul 2024 11:32:09 +0800 Subject: [PATCH] support watch file change for auto refresh on debug mode --- go.mod | 7 +++++++ plugin.go | 48 +++++++++++++++++++++++++++++++++++++++++++----- tests/go.mod | 14 ++++++++------ 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index d4839c4..2f5f58e 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,12 @@ go 1.18 require ( apigo.cc/apigo/plugin v1.0.2 + github.com/fsnotify/fsnotify v1.7.0 + github.com/ssgo/u v1.7.6 github.com/webview/webview_go v0.0.0-20240217094020-f8e8d34d29dd ) + +require ( + golang.org/x/sys v0.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/plugin.go b/plugin.go index 35ebc38..7fdac4e 100644 --- a/plugin.go +++ b/plugin.go @@ -2,6 +2,9 @@ package client import ( "apigo.cc/apigo/plugin" + "fmt" + "github.com/fsnotify/fsnotify" + "github.com/ssgo/u" webview "github.com/webview/webview_go" "os" "path/filepath" @@ -27,10 +30,45 @@ type Webview struct { w webview.WebView isRunning bool isAsync bool + isDebug bool + webRoot string +} + +func addAllDirForWatcher(filename string, watcher *fsnotify.Watcher) { + watcher.Add(filename) + for _, f := range u.ReadDirN(filename) { + if f.IsDir { + addAllDirForWatcher(f.FullName, watcher) + } + } } func (w *Webview) Run() { w.isRunning = true + if w.isDebug && w.webRoot != "" { + if watcher, err := fsnotify.NewWatcher(); err == nil { + fmt.Println("watching...", w.webRoot) + go func() { + for w.isRunning { + select { + case event, ok := <-watcher.Events: + if !ok { + break + } + if event.Has(fsnotify.Write) { + fmt.Println("file changed:", event.Name) + w.Eval("location.reload()") + } + case _, ok := <-watcher.Errors: + if !ok { + break + } + } + } + }() + addAllDirForWatcher(w.webRoot, watcher) + } + } w.w.Run() if w.isRunning { w.isRunning = false @@ -53,12 +91,12 @@ func (w *Webview) LoadURL(url string) { } func (w *Webview) LoadFile(file string) { - if filepath.IsAbs(file) { - w.w.Navigate("file://" + file) - } else { + if !filepath.IsAbs(file) { curPath, _ := os.Getwd() - w.w.Navigate("file://" + filepath.Join(curPath, file)) + file = filepath.Join(curPath, file) } + w.webRoot = filepath.Dir(file) + w.w.Navigate("file://" + file) } func (w *Webview) Dispatch(f func()) { @@ -124,7 +162,7 @@ func newWindow(title string, width int, height int, sizeMode *string, isDebug *b if isDebug != nil { isDebugV = *isDebug } - w := &Webview{w: webview.New(isDebugV)} + w := &Webview{w: webview.New(isDebugV), isDebug: isDebugV} w.SetTitle(title) w.SetSize(width, height, sizeMode) w.binds() diff --git a/tests/go.mod b/tests/go.mod index 8d3ee7b..3eef707 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -3,18 +3,20 @@ module tests go 1.18 require ( - apigo.cc/apigo/gojs v0.0.9 + apigo.cc/apigo/gojs v0.0.10 current-plugin v0.0.0 - github.com/ssgo/u v1.7.5 + github.com/ssgo/u v1.7.6 ) require ( apigo.cc/apigo/plugin v1.0.2 // indirect - apigo.cc/apigo/qjs v0.0.3 // indirect - github.com/ssgo/config v1.7.5 // indirect - github.com/ssgo/log v1.7.5 // indirect - github.com/ssgo/standard v1.7.5 // indirect + apigo.cc/apigo/quickjs-go v0.4.12 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/ssgo/config v1.7.6 // indirect + github.com/ssgo/log v1.7.6 // indirect + github.com/ssgo/standard v1.7.6 // indirect github.com/webview/webview_go v0.0.0-20240217094020-f8e8d34d29dd // indirect + golang.org/x/sys v0.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect )