fix race in previews with lazy drawing

Related #445
This commit is contained in:
Gokcehan 2020-09-25 23:08:29 +03:00
parent 1460f49b3e
commit 50dd374f77

17
nav.go
View File

@ -416,19 +416,14 @@ func (nav *nav) position() {
} }
} }
func (nav *nav) preview() { func (nav *nav) preview(path string) {
curr, err := nav.currFile() reg := &reg{loadTime: time.Now(), path: path}
if err != nil {
return
}
reg := &reg{loadTime: time.Now(), path: curr.path}
var reader io.Reader var reader io.Reader
if len(gOpts.previewer) != 0 { if len(gOpts.previewer) != 0 {
exportOpts() exportOpts()
cmd := exec.Command(gOpts.previewer, curr.path, strconv.Itoa(nav.height)) cmd := exec.Command(gOpts.previewer, path, strconv.Itoa(nav.height))
out, err := cmd.StdoutPipe() out, err := cmd.StdoutPipe()
if err != nil { if err != nil {
@ -443,7 +438,7 @@ func (nav *nav) preview() {
defer out.Close() defer out.Close()
reader = out reader = out
} else { } else {
f, err := os.Open(curr.path) f, err := os.Open(path)
if err != nil { if err != nil {
log.Printf("opening file: %s", err) log.Printf("opening file: %s", err)
} }
@ -477,7 +472,7 @@ func (nav *nav) loadReg(path string) *reg {
if !ok { if !ok {
r := &reg{loading: true, loadTime: time.Now(), path: path} r := &reg{loading: true, loadTime: time.Now(), path: path}
nav.regCache[path] = r nav.regCache[path] = r
go nav.preview() go nav.preview(path)
return r return r
} }
@ -494,7 +489,7 @@ func (nav *nav) checkReg(reg *reg) {
if s.ModTime().After(reg.loadTime) { if s.ModTime().After(reg.loadTime) {
reg.loadTime = time.Now() reg.loadTime = time.Now()
go nav.preview() go nav.preview(reg.path)
} }
} }