diff options
author | Richard Burke <rich.g.burke@gmail.com> | 2019-02-13 21:48:05 +0000 |
---|---|---|
committer | Richard Burke <rich.g.burke@gmail.com> | 2019-02-13 21:48:05 +0000 |
commit | 9c5a90181967b069374d9c72a15ae2a48839ec87 (patch) | |
tree | 592a9580184bd946b9412e90549739cfac675fd2 | |
parent | aea21d2559b5f250cd66261292b2f8fc4405edc5 (diff) |
Remove config listeners when views are removed
-rw-r--r-- | cmd/grv/config.go | 25 | ||||
-rw-r--r-- | cmd/grv/grv.go | 2 |
2 files changed, 26 insertions, 1 deletions
diff --git a/cmd/grv/config.go b/cmd/grv/config.go index 4198d86..02481d9 100644 --- a/cmd/grv/config.go +++ b/cmd/grv/config.go @@ -1071,6 +1071,31 @@ func (config *Configuration) KeyStrings(actionType ActionType, viewHierarchy Vie return } +// HandleEvent reacts to an event +func (config *Configuration) HandleEvent(event Event) (err error) { + switch event.EventType { + case ViewRemovedEvent: + for _, view := range event.Args { + if listener, ok := view.(ConfigVariableOnChangeListener); ok { + config.removeOnChangeListener(listener) + } + } + } + + return +} + +func (config *Configuration) removeOnChangeListener(onChangeListener ConfigVariableOnChangeListener) { + for _, variable := range config.configVariables { + for index, listener := range variable.onChangeListeners { + if onChangeListener == listener { + variable.onChangeListeners = append(variable.onChangeListeners[:index], variable.onChangeListeners[index+1:]...) + break + } + } + } +} + // GenerateHelpSections generates all help tables related to configuration func (config *Configuration) GenerateHelpSections() (helpSections []*HelpSection) { helpSections = append(helpSections, config.keyBindings.GenerateHelpSections(config)...) diff --git a/cmd/grv/grv.go b/cmd/grv/grv.go index db64fc5..5a48936 100644 --- a/cmd/grv/grv.go +++ b/cmd/grv/grv.go @@ -218,7 +218,7 @@ func NewGRV(readOnly bool) *GRV { config: config, inputBuffer: NewInputBuffer(keyBindings), input: NewInputKeyMapper(ui), - eventListeners: []EventListener{view, repoData}, + eventListeners: []EventListener{view, repoData, config}, variables: variables, } } |