From 76aacc708b94cf23e8f98055fe534b650b11e1ed Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Thu, 4 Apr 2024 17:16:45 +1100 Subject: [PATCH] Avoid using multiple threads Seems to cause issues if there are multiple threads and each one of them is loading the same new file, in which case there is a race condition. --- app.go | 116 ++++++++++++++++++++++++++++++++++++++++++--------------- nav.go | 2 - 2 files changed, 85 insertions(+), 33 deletions(-) diff --git a/app.go b/app.go index 4dc2e276..4e5c63b5 100644 --- a/app.go +++ b/app.go @@ -427,29 +427,6 @@ func (app *app) loop() { } } - app.ui.draw(app.nav) - case f := <-app.nav.fileChan: - dirs := app.nav.dirs - if app.ui.dirPrev != nil { - dirs = append(dirs, app.ui.dirPrev) - } - for _, dir := range dirs { - if dir.path != filepath.Dir(f.path) { - continue - } - - for i := range dir.allFiles { - if dir.allFiles[i].path == f.path { - dir.allFiles[i] = f - break - } - } - - name := dir.name() - dir.sort() - dir.sel(name, app.nav.height) - } - app.ui.loadFile(app, false) app.ui.draw(app.nav) case ev := <-app.ui.evChan: e := app.ui.readEvent(ev, app.nav) @@ -485,19 +462,22 @@ func (app *app) loop() { app.nav.previewLoading = true app.ui.draw(app.nav) case ev := <-app.nav.watcherEvents: - if ev.Has(fsnotify.Create) || ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) { - go func() { - app.nav.fileChan <- newFile(filepath.Dir(ev.Name)) - }() - app.nav.renew() + if ev.Has(fsnotify.Create) { + app.addFile(newFile(ev.Name)) + app.changeFile(newFile(filepath.Dir(ev.Name))) + app.ui.loadFile(app, false) + app.ui.draw(app.nav) + } + + if ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) { + app.removeFile(ev.Name) + app.changeFile(newFile(filepath.Dir(ev.Name))) app.ui.loadFile(app, false) app.ui.draw(app.nav) } if ev.Has(fsnotify.Write) || ev.Has(fsnotify.Chmod) { - go func() { - app.nav.fileChan <- newFile(ev.Name) - }() + app.changeFile(newFile(ev.Name)) currFile, err := app.nav.currFile() if err == nil && currFile.path == ev.Name { app.nav.startPreview() @@ -639,3 +619,77 @@ func (app *app) runShell(s string, args []string, prefix string) { } } + +func (app *app) addFile(f *file) { + dirs := app.nav.dirs + if app.ui.dirPrev != nil { + dirs = append(dirs, app.ui.dirPrev) + } + + for _, dir := range dirs { + if dir.path != filepath.Dir(f.path) { + continue + } + + for i := range dir.allFiles { + if dir.allFiles[i].path == f.path { + dir.allFiles[i] = f + return + } + } + + dir.allFiles = append(dir.allFiles, f) + name := dir.name() + dir.sort() + dir.sel(name, app.nav.height) + } +} + +func (app *app) changeFile(f *file) { + dirs := app.nav.dirs + if app.ui.dirPrev != nil { + dirs = append(dirs, app.ui.dirPrev) + } + + for _, dir := range dirs { + if dir.path != filepath.Dir(f.path) { + continue + } + + for i := range dir.allFiles { + if dir.allFiles[i].path == f.path { + dir.allFiles[i] = f + break + } + } + + name := dir.name() + dir.sort() + dir.sel(name, app.nav.height) + } +} + +func (app *app) removeFile(path string) { + dirs := app.nav.dirs + if app.ui.dirPrev != nil { + dirs = append(dirs, app.ui.dirPrev) + } + + for _, dir := range dirs { + if dir.path != filepath.Dir(path) { + continue + } + + var allFiles []*file + for _, file := range dir.allFiles { + if file.path != path { + allFiles = append(allFiles, file) + } + } + + dir.allFiles = allFiles + name := dir.name() + dir.sort() + dir.sel(name, app.nav.height) + } +} diff --git a/nav.go b/nav.go index d4bdacd8..0a5f6481 100644 --- a/nav.go +++ b/nav.go @@ -443,7 +443,6 @@ type nav struct { dirPreviewChan chan *dir dirChan chan *dir regChan chan *reg - fileChan chan *file dirCache map[string]*dir regCache map[string]*reg saves map[string]bool @@ -594,7 +593,6 @@ func newNav(height int) *nav { dirPreviewChan: make(chan *dir, 1024), dirChan: make(chan *dir), regChan: make(chan *reg), - fileChan: make(chan *file), dirCache: make(map[string]*dir), regCache: make(map[string]*reg), saves: make(map[string]bool),