diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-02-28 04:16:04 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-02-28 08:05:02 +0100 |
commit | 9ea46af463d6f195a2cb6d990be702d10e01eb65 (patch) | |
tree | e9ced18b3272875520c43f6ffea0c7e07cc8438a /conf/storewatcher.go | |
parent | ipc: implement event system with pipes (diff) | |
download | wireguard-windows-9ea46af463d6f195a2cb6d990be702d10e01eb65.tar.xz wireguard-windows-9ea46af463d6f195a2cb6d990be702d10e01eb65.zip |
manager: wire up tunnels changed notifier
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'conf/storewatcher.go')
-rw-r--r-- | conf/storewatcher.go | 34 |
1 files changed, 10 insertions, 24 deletions
diff --git a/conf/storewatcher.go b/conf/storewatcher.go index 4f9c2ef7..7a5ab387 100644 --- a/conf/storewatcher.go +++ b/conf/storewatcher.go @@ -5,34 +5,20 @@ package conf -import "reflect" - -type StoreCallback func() +type storeCallback struct { + cb func() +} -var storeCallbacks []StoreCallback +var storeCallbacks = make(map[*storeCallback]bool) -func RegisterStoreChangeCallback(cb StoreCallback) { +func RegisterStoreChangeCallback(cb func()) *storeCallback { startWatchingConfigDir() cb() - storeCallbacks = append(storeCallbacks, cb) + s := &storeCallback{cb} + storeCallbacks[s] = true + return s } -func UnregisterStoreChangeCallback(cb StoreCallback) { - //TODO: this function is ridiculous, doing slow iteration like this and reflection too. - - index := -1 - for i, e := range storeCallbacks { - if reflect.ValueOf(e).Pointer() == reflect.ValueOf(cb).Pointer() { - index = i - break - } - } - if index == -1 { - return - } - newList := storeCallbacks[0:index] - if index < len(storeCallbacks)-1 { - newList = append(newList, storeCallbacks[index+1:]...) - } - storeCallbacks = newList +func UnregisterStoreChangeCallback(cb *storeCallback) { + delete(storeCallbacks, cb) } |