github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/mediators/config/listeners.go (about) 1 package config 2 3 import ( 4 "github.com/ActiveState/cli/internal/logging" 5 ) 6 7 type listener struct { 8 key string 9 callback func() 10 } 11 12 var listeners = make([]listener, 0) 13 14 // AddListener adds a listener for changes to the given config key that calls the given callback 15 // function. 16 // Client code is responsible for calling NotifyListeners to signal config changes to listeners. 17 func AddListener(key string, callback func()) { 18 logging.Debug("Adding listener for config key: %s", key) 19 listeners = append(listeners, listener{key, callback}) 20 } 21 22 // NotifyListeners notifies listeners that the given config key has changed. 23 func NotifyListeners(key string) { 24 for _, listener := range listeners { 25 if listener.key != key { 26 continue 27 } 28 logging.Debug("Invoking callback for config key: %s", key) 29 listener.callback() 30 } 31 }