aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/conf/storewatcher.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-02-28 04:16:04 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2019-02-28 08:05:02 +0100
commit37ce1d0654ea7b69242097068ae4a75f40b3a2b2 (patch)
treee9ced18b3272875520c43f6ffea0c7e07cc8438a /conf/storewatcher.go
parentipc: implement event system with pipes (diff)
downloadwireguard-windows-37ce1d0654ea7b69242097068ae4a75f40b3a2b2.tar.xz
wireguard-windows-37ce1d0654ea7b69242097068ae4a75f40b3a2b2.zip
manager: wire up tunnels changed notifier
Diffstat (limited to 'conf/storewatcher.go')
-rw-r--r--conf/storewatcher.go34
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)
}