Skip to content

Commit

Permalink
refactor: Drop handlePanic dependency from file watcher and instead…
Browse files Browse the repository at this point in the history
… use `errors.Join` like usual

Signed-off-by: Felicitas Pojtinger <felicitas@pojtinger.com>
  • Loading branch information
pojntfx committed Jul 6, 2024
1 parent ec6623a commit 7074eb3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
13 changes: 10 additions & 3 deletions hydrapp/pkg/ui/browser_launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
ErrCouldNotCreateEpiphanyProfileDirectory = errors.New("could not create Epiphany profile directory")
ErrCouldNotWriteDesktopFile = errors.New("could not write desktop file")
ErrCouldNotFindSupportedBrowser = errors.New("could not find a supported browser")
ErrCouldNotWaitForBrowserLockfileRemoval = errors.New("could not wait for browser lockfile removal")
)

type Browser struct {
Expand Down Expand Up @@ -294,7 +295,9 @@ func LaunchBrowser(
}

// Wait till lock for browser has been removed
utils.WaitForFileRemoval(filepath.Join(userDataDir, "SingletonSocket"), handlePanic)
if err := utils.WaitForFileRemoval(filepath.Join(userDataDir, "SingletonSocket")); err != nil {
handlePanic(ErrCouldNotWaitForBrowserLockfileRemoval.Error(), errors.Join(ErrCouldNotWaitForBrowserLockfileRemoval, err))
}

// Launch Firefox-like browser
case BrowserTypeFirefox:
Expand Down Expand Up @@ -406,7 +409,9 @@ func LaunchBrowser(
}

// Wait till lock for browser has been removed
utils.WaitForFileRemoval(filepath.Join(profileDir, "cookies.sqlite-wal"), handlePanic)
if err := utils.WaitForFileRemoval(filepath.Join(profileDir, "cookies.sqlite-wal")); err != nil {
handlePanic(ErrCouldNotWaitForBrowserLockfileRemoval.Error(), errors.Join(ErrCouldNotWaitForBrowserLockfileRemoval, err))
}

// Launch Epiphany-like browser
case BrowserTypeEpiphany:
Expand Down Expand Up @@ -470,7 +475,9 @@ func LaunchBrowser(
}

// Wait till lock for browser has been removed
utils.WaitForFileRemoval(filepath.Join(profileDir, "ephy-history.db-wal"), handlePanic)
if err := utils.WaitForFileRemoval(filepath.Join(profileDir, "ephy-history.db-wal")); err != nil {
handlePanic(ErrCouldNotWaitForBrowserLockfileRemoval.Error(), errors.Join(ErrCouldNotWaitForBrowserLockfileRemoval, err))
}

// Launch Lynx-like browser
case BrowserTypeLynx:
Expand Down
56 changes: 28 additions & 28 deletions hydrapp/pkg/utils/filewatcher.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
package utils

import (
"errors"
"os"

"github.com/fsnotify/fsnotify"
)

func WaitForFileRemoval(path string, handlePanic func(msg string, err error)) {
var (
ErrCouldNotStartFileWatcher = errors.New("could not start file watcher")
ErrCouldNotAddPathToFileWatcher = errors.New("could not add path to file watcher")
ErrCouldNotWatchFile = errors.New("could not watch file")
)

func WaitForFileRemoval(path string) error {
if _, err := os.Stat(path); err == nil {
// Wait until browser has exited
watcher, err := fsnotify.NewWatcher()
if err != nil {
handlePanic("could not start lockfile watcher", err)
return errors.Join(ErrCouldNotStartFileWatcher, err)
}
defer watcher.Close()

done := make(chan struct{})
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}

// Stop the app
if event.Op&fsnotify.Remove == fsnotify.Remove {
done <- struct{}{}
if err = watcher.Add(path); err != nil {
return errors.Join(ErrCouldNotAddPathToFileWatcher, err)
}

return
}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return nil
}

case err, ok := <-watcher.Errors:
if !ok {
return
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
return nil
}

handlePanic("could not continue watching lockfile", err)
case err, ok := <-watcher.Errors:
if !ok {
return nil
}
}
}()

err = watcher.Add(path)
if err != nil {
handlePanic("could not watch lockfile", err)
return errors.Join(ErrCouldNotWatchFile, err)
}
}

<-done
}

return nil
}

0 comments on commit 7074eb3

Please sign in to comment.