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.
This commit is contained in:
2 files changed
+85
-33
No files matched your search
@@ -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)
|
app.ui.draw(app.nav)
|
||||||
case ev := <-app.ui.evChan:
|
case ev := <-app.ui.evChan:
|
||||||
e := app.ui.readEvent(ev, app.nav)
|
e := app.ui.readEvent(ev, app.nav)
|
||||||
@@ -485,19 +462,22 @@ func (app *app) loop() {
|
|||||||
app.nav.previewLoading = true
|
app.nav.previewLoading = true
|
||||||
app.ui.draw(app.nav)
|
app.ui.draw(app.nav)
|
||||||
case ev := <-app.nav.watcherEvents:
|
case ev := <-app.nav.watcherEvents:
|
||||||
if ev.Has(fsnotify.Create) || ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) {
|
if ev.Has(fsnotify.Create) {
|
||||||
go func() {
|
app.addFile(newFile(ev.Name))
|
||||||
app.nav.fileChan <- newFile(filepath.Dir(ev.Name))
|
app.changeFile(newFile(filepath.Dir(ev.Name)))
|
||||||
}()
|
app.ui.loadFile(app, false)
|
||||||
app.nav.renew()
|
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.loadFile(app, false)
|
||||||
app.ui.draw(app.nav)
|
app.ui.draw(app.nav)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ev.Has(fsnotify.Write) || ev.Has(fsnotify.Chmod) {
|
if ev.Has(fsnotify.Write) || ev.Has(fsnotify.Chmod) {
|
||||||
go func() {
|
app.changeFile(newFile(ev.Name))
|
||||||
app.nav.fileChan <- newFile(ev.Name)
|
|
||||||
}()
|
|
||||||
currFile, err := app.nav.currFile()
|
currFile, err := app.nav.currFile()
|
||||||
if err == nil && currFile.path == ev.Name {
|
if err == nil && currFile.path == ev.Name {
|
||||||
app.nav.startPreview()
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -443,7 +443,6 @@ type nav struct {
|
|||||||
dirPreviewChan chan *dir
|
dirPreviewChan chan *dir
|
||||||
dirChan chan *dir
|
dirChan chan *dir
|
||||||
regChan chan *reg
|
regChan chan *reg
|
||||||
fileChan chan *file
|
|
||||||
dirCache map[string]*dir
|
dirCache map[string]*dir
|
||||||
regCache map[string]*reg
|
regCache map[string]*reg
|
||||||
saves map[string]bool
|
saves map[string]bool
|
||||||
@@ -594,7 +593,6 @@ func newNav(height int) *nav {
|
|||||||
dirPreviewChan: make(chan *dir, 1024),
|
dirPreviewChan: make(chan *dir, 1024),
|
||||||
dirChan: make(chan *dir),
|
dirChan: make(chan *dir),
|
||||||
regChan: make(chan *reg),
|
regChan: make(chan *reg),
|
||||||
fileChan: make(chan *file),
|
|
||||||
dirCache: make(map[string]*dir),
|
dirCache: make(map[string]*dir),
|
||||||
regCache: make(map[string]*reg),
|
regCache: make(map[string]*reg),
|
||||||
saves: make(map[string]bool),
|
saves: make(map[string]bool),
|
||||||
|
|||||||
Reference in new issue
Block a user