Skip to content

Commit

Permalink
Invalidate cache for recreated files (#1756)
Browse files Browse the repository at this point in the history
* Invalidate cache for recreated files

* Sanity check for file updates

* Prevent load when watching

* Revert "Prevent load when watching"

This reverts commit 4cf4629.

* Load is redundant when watch is enabled
  • Loading branch information
joelim-work committed Jun 23, 2024
1 parent 38ddfea commit 6703876
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
5 changes: 4 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func newApp(ui *ui, nav *nav) *app {
nav: nav,
ticker: new(time.Ticker),
quitChan: quitChan,
watch: newWatch(nav.dirChan, nav.fileChan),
watch: newWatch(nav.dirChan, nav.fileChan, nav.delChan),
}

sigChan := make(chan os.Signal, 1)
Expand Down Expand Up @@ -460,6 +460,9 @@ func (app *app) loop() {
app.ui.loadFileInfo(app.nav)
}
app.ui.draw(app.nav)
case path := <-app.nav.delChan:
delete(app.nav.dirCache, path)
delete(app.nav.regCache, path)
case ev := <-app.ui.evChan:
e := app.ui.readEvent(ev, app.nav)
if e == nil {
Expand Down
2 changes: 1 addition & 1 deletion eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ func (e *callExpr) eval(app *app, args []string) {
app.ui.loadFile(app, true)
onRedraw(app)
case "load":
if !app.nav.init {
if !app.nav.init || gOpts.watch {
return
}
app.nav.renew()
Expand Down
2 changes: 2 additions & 0 deletions nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ type nav struct {
dirChan chan *dir
regChan chan *reg
fileChan chan *file
delChan chan string
dirCache map[string]*dir
regCache map[string]*reg
saves map[string]bool
Expand Down Expand Up @@ -585,6 +586,7 @@ func newNav(height int) *nav {
dirChan: make(chan *dir),
regChan: make(chan *reg),
fileChan: make(chan *file),
delChan: make(chan string),
dirCache: make(map[string]*dir),
regCache: make(map[string]*reg),
saves: make(map[string]bool),
Expand Down
20 changes: 18 additions & 2 deletions watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"os"
"path/filepath"
"time"

Expand All @@ -18,9 +19,10 @@ type watch struct {
updateTimer *time.Timer
dirChan chan<- *dir
fileChan chan<- *file
delChan chan<- string
}

func newWatch(dirChan chan<- *dir, fileChan chan<- *file) *watch {
func newWatch(dirChan chan<- *dir, fileChan chan<- *file, delChan chan<- string) *watch {
return &watch{
quit: make(chan struct{}),
loads: make(map[string]bool),
Expand All @@ -29,6 +31,7 @@ func newWatch(dirChan chan<- *dir, fileChan chan<- *file) *watch {
updateTimer: time.NewTimer(0),
dirChan: dirChan,
fileChan: fileChan,
delChan: delChan,
}
}

Expand Down Expand Up @@ -81,7 +84,14 @@ func (watch *watch) loop() {
for {
select {
case ev := <-watch.events:
if ev.Has(fsnotify.Create) || ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) {
if ev.Has(fsnotify.Create) {
dir := filepath.Dir(ev.Name)
watch.addLoad(dir)
watch.addUpdate(dir)
}

if ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) {
watch.delChan <- ev.Name
dir := filepath.Dir(ev.Name)
watch.addLoad(dir)
watch.addUpdate(dir)
Expand All @@ -92,13 +102,19 @@ func (watch *watch) loop() {
}
case <-watch.loadTimer.C:
for path := range watch.loads {
if _, err := os.Lstat(path); err != nil {
continue
}
dir := newDir(path)
dir.sort()
watch.dirChan <- dir
}
watch.loads = make(map[string]bool)
case <-watch.updateTimer.C:
for path := range watch.updates {
if _, err := os.Lstat(path); err != nil {
continue
}
watch.fileChan <- newFile(path)
}
watch.updates = make(map[string]bool)
Expand Down

0 comments on commit 6703876

Please sign in to comment.