close goEvery goroutines when switching to a subprocess

This commit is contained in:
Jesse Duffield 2019-06-23 19:56:23 +10:00
parent 54a8068bdd
commit f34c7b9058
2 changed files with 15 additions and 1 deletions

View file

@ -121,6 +121,11 @@ type guiState struct {
Panels *panelStates
SubProcessOutput string
Stats map[string]commands.ContainerStats
// SessionIndex tells us how many times we've come back from a subprocess.
// We increment it each time we switch to a new subprocess
// Every time we go to a subprocess we need to close a few goroutines so this index is used for that purpose
SessionIndex int
}
// NewGui builds a new gui handler
@ -144,6 +149,7 @@ func NewGui(log *logrus.Entry, dockerCommand *commands.DockerCommand, oSCommand
},
Status: &statusState{ContextIndex: 0},
},
SessionIndex: 0,
}
cyclableViews := []string{"status", "containers", "images", "volumes"}
@ -230,9 +236,13 @@ func (gui *Gui) renderGlobalOptions() error {
}
func (gui *Gui) goEvery(interval time.Duration, function func() error) {
currentSessionIndex := gui.State.SessionIndex
_ = function() // time.Tick doesn't run immediately so we'll do that here // TODO: maybe change
go func() {
for range time.Tick(interval) {
if gui.State.SessionIndex > currentSessionIndex {
return
}
_ = function()
}
}()

View file

@ -23,11 +23,15 @@ func (gui *Gui) RunWithSubprocesses() error {
} else if err == gui.Errors.ErrSubProcess {
// preparing the state for when we return
gui.State.PreviousView = gui.currentViewName()
gui.State.Panels.Main.ObjectKey = ""
// giving goEvery goroutines time to finish
gui.State.SessionIndex++
if err := gui.runCommand(); err != nil {
return err
}
// ensuring we render e.g. the logs of the currently selected item upon return
gui.State.Panels.Main.ObjectKey = ""
} else {
return err
}