1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00

concurrent-safe handling of context state

This commit is contained in:
Jesse Duffield 2020-08-18 22:41:27 +10:00
parent 9c866fd49c
commit 2488e0044d

View File

@ -87,23 +87,27 @@ type ContextTree struct {
} }
func (gui *Gui) switchContext(c Context) error { func (gui *Gui) switchContext(c Context) error {
// push onto stack gui.g.Update(func(*gocui.Gui) error {
// if we are switching to a side context, remove all other contexts in the stack // push onto stack
if c.GetKind() == SIDE_CONTEXT { // if we are switching to a side context, remove all other contexts in the stack
for _, stackContext := range gui.State.ContextStack { if c.GetKind() == SIDE_CONTEXT {
if stackContext.GetKey() != c.GetKey() { for _, stackContext := range gui.State.ContextStack {
if err := gui.deactivateContext(stackContext); err != nil { if stackContext.GetKey() != c.GetKey() {
return err if err := gui.deactivateContext(stackContext); err != nil {
return err
}
} }
} }
gui.State.ContextStack = []Context{c}
} else {
// TODO: think about other exceptional cases
gui.State.ContextStack = append(gui.State.ContextStack, c)
} }
gui.State.ContextStack = []Context{c}
} else {
// TODO: think about other exceptional cases
gui.State.ContextStack = append(gui.State.ContextStack, c)
}
return gui.activateContext(c) return gui.activateContext(c)
})
return nil
} }
// switchContextToView is to be used when you don't know which context you // switchContextToView is to be used when you don't know which context you
@ -114,25 +118,29 @@ func (gui *Gui) switchContextToView(viewName string) error {
} }
func (gui *Gui) returnFromContext() error { func (gui *Gui) returnFromContext() error {
// TODO: add mutexes gui.g.Update(func(*gocui.Gui) error {
// TODO: add mutexes
if len(gui.State.ContextStack) == 1 { if len(gui.State.ContextStack) == 1 {
// cannot escape from bottommost context // cannot escape from bottommost context
return nil return nil
} }
n := len(gui.State.ContextStack) - 1 n := len(gui.State.ContextStack) - 1
currentContext := gui.State.ContextStack[n] currentContext := gui.State.ContextStack[n]
newContext := gui.State.ContextStack[n-1] newContext := gui.State.ContextStack[n-1]
gui.State.ContextStack = gui.State.ContextStack[:n] gui.State.ContextStack = gui.State.ContextStack[:n]
if err := gui.deactivateContext(currentContext); err != nil { if err := gui.deactivateContext(currentContext); err != nil {
return err return err
} }
return gui.activateContext(newContext) return gui.activateContext(newContext)
})
return nil
} }
func (gui *Gui) deactivateContext(c Context) error { func (gui *Gui) deactivateContext(c Context) error {