Skip to content

Commit

Permalink
Try batching renew updates
Browse files Browse the repository at this point in the history
  • Loading branch information
joelim-work committed Apr 23, 2024
1 parent e0dfe94 commit f9a51ad
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 101 deletions.
52 changes: 21 additions & 31 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,30 +459,30 @@ func (app *app) loop() {
app.nav.previewLoading = true
app.ui.draw(app.nav)
case ev := <-app.nav.watcherEvents:
if ev.Has(fsnotify.Create) {
app.updateFile(ev.Name, true)
app.updateFile(filepath.Dir(ev.Name), true)
app.ui.loadFile(app, false)
app.ui.draw(app.nav)
}

if ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) {
app.updateFile(ev.Name, false)
app.updateFile(filepath.Dir(ev.Name), true)
app.ui.loadFile(app, false)
app.ui.draw(app.nav)
if ev.Has(fsnotify.Create) || ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) {
app.updateFile(filepath.Dir(ev.Name))
if !app.nav.watcherRenew {
app.nav.watcherRenew = true
app.nav.watcherRenewTimer.Stop()
app.nav.watcherRenewTimer.Reset(10 * time.Millisecond)
}
}

if ev.Has(fsnotify.Write) || ev.Has(fsnotify.Chmod) {
if !app.nav.watcherWrites[ev.Name] {
app.nav.watcherWrites[ev.Name] = true
app.nav.watcherTimer.Stop()
app.nav.watcherTimer.Reset(10 * time.Millisecond)
app.nav.watcherWriteTimer.Stop()
app.nav.watcherWriteTimer.Reset(10 * time.Millisecond)
}
}
case <-app.nav.watcherTimer.C:
case <-app.nav.watcherRenewTimer.C:
app.nav.renew()
app.ui.loadFile(app, false)
app.ui.draw(app.nav)
app.nav.watcherRenew = false
case <-app.nav.watcherWriteTimer.C:
for path := range app.nav.watcherWrites {
app.updateFile(path, true)
app.updateFile(path)
currFile, err := app.nav.currFile()
if err == nil && currFile.path == path {
app.nav.startPreview()
Expand Down Expand Up @@ -625,7 +625,7 @@ func (app *app) runShell(s string, args []string, prefix string) {
}
}

func (app *app) updateFile(path string, add bool) {
func (app *app) updateFile(path string) {
dirs := app.nav.dirs
if app.ui.dirPrev != nil {
dirs = append(dirs, app.ui.dirPrev)
Expand All @@ -636,22 +636,12 @@ func (app *app) updateFile(path string, add bool) {
continue
}

var allFiles []*file
for _, file := range dir.allFiles {
if file.path != path {
allFiles = append(allFiles, file)
}
}

if add {
if stat, err := os.Stat(path); err == nil {
allFiles = append(allFiles, newFile(path))
if stat.IsDir() {
app.nav.setWatches()
}
for i := range dir.allFiles {
if dir.allFiles[i].path == path {
dir.allFiles[i] = newFile(path)
break
}
}
dir.allFiles = allFiles

name := dir.name()
dir.sort()
Expand Down
144 changes: 74 additions & 70 deletions nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,53 +418,55 @@ func (dir *dir) boundPos(height int) {
}

type nav struct {
init bool
dirs []*dir
copyBytes int64
copyTotal int64
copyUpdate int
moveCount int
moveTotal int
moveUpdate int
deleteCount int
deleteTotal int
deleteUpdate int
copyBytesChan chan int64
copyTotalChan chan int64
moveCountChan chan int
moveTotalChan chan int
deleteCountChan chan int
deleteTotalChan chan int
previewChan chan string
dirPreviewChan chan *dir
dirChan chan *dir
regChan chan *reg
dirCache map[string]*dir
regCache map[string]*reg
saves map[string]bool
marks map[string]string
renameOldPath string
renameNewPath string
selections map[string]int
tags map[string]string
selectionInd int
height int
find string
findBack bool
search string
searchBack bool
searchInd int
searchPos int
prevFilter []string
volatilePreview bool
previewTimer *time.Timer
previewLoading bool
jumpList []string
jumpListInd int
watcher *fsnotify.Watcher
watcherEvents <-chan fsnotify.Event
watcherWrites map[string]bool
watcherTimer *time.Timer
init bool
dirs []*dir
copyBytes int64
copyTotal int64
copyUpdate int
moveCount int
moveTotal int
moveUpdate int
deleteCount int
deleteTotal int
deleteUpdate int
copyBytesChan chan int64
copyTotalChan chan int64
moveCountChan chan int
moveTotalChan chan int
deleteCountChan chan int
deleteTotalChan chan int
previewChan chan string
dirPreviewChan chan *dir
dirChan chan *dir
regChan chan *reg
dirCache map[string]*dir
regCache map[string]*reg
saves map[string]bool
marks map[string]string
renameOldPath string
renameNewPath string
selections map[string]int
tags map[string]string
selectionInd int
height int
find string
findBack bool
search string
searchBack bool
searchInd int
searchPos int
prevFilter []string
volatilePreview bool
previewTimer *time.Timer
previewLoading bool
jumpList []string
jumpListInd int
watcher *fsnotify.Watcher
watcherEvents <-chan fsnotify.Event
watcherRenew bool
watcherWrites map[string]bool
watcherRenewTimer *time.Timer
watcherWriteTimer *time.Timer
}

func (nav *nav) loadDirInternal(path string) *dir {
Expand Down Expand Up @@ -580,29 +582,31 @@ func (nav *nav) getDirs(wd string) {

func newNav(height int) *nav {
nav := &nav{
copyBytesChan: make(chan int64, 1024),
copyTotalChan: make(chan int64, 1024),
moveCountChan: make(chan int, 1024),
moveTotalChan: make(chan int, 1024),
deleteCountChan: make(chan int, 1024),
deleteTotalChan: make(chan int, 1024),
previewChan: make(chan string, 1024),
dirPreviewChan: make(chan *dir, 1024),
dirChan: make(chan *dir),
regChan: make(chan *reg),
dirCache: make(map[string]*dir),
regCache: make(map[string]*reg),
saves: make(map[string]bool),
marks: make(map[string]string),
selections: make(map[string]int),
tags: make(map[string]string),
selectionInd: 0,
height: height,
previewTimer: time.NewTimer(0),
jumpList: make([]string, 0),
jumpListInd: -1,
watcherWrites: make(map[string]bool),
watcherTimer: time.NewTimer(0),
copyBytesChan: make(chan int64, 1024),
copyTotalChan: make(chan int64, 1024),
moveCountChan: make(chan int, 1024),
moveTotalChan: make(chan int, 1024),
deleteCountChan: make(chan int, 1024),
deleteTotalChan: make(chan int, 1024),
previewChan: make(chan string, 1024),
dirPreviewChan: make(chan *dir, 1024),
dirChan: make(chan *dir),
regChan: make(chan *reg),
dirCache: make(map[string]*dir),
regCache: make(map[string]*reg),
saves: make(map[string]bool),
marks: make(map[string]string),
selections: make(map[string]int),
tags: make(map[string]string),
selectionInd: 0,
height: height,
previewTimer: time.NewTimer(0),
jumpList: make([]string, 0),
jumpListInd: -1,
watcherRenew: false,
watcherWrites: make(map[string]bool),
watcherRenewTimer: time.NewTimer(0),
watcherWriteTimer: time.NewTimer(0),
}

return nav
Expand Down

0 comments on commit f9a51ad

Please sign in to comment.