aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/conf/storewatcher.go
blob: 4f9c2ef73e41d22e10ad7aa58b45e4afe9008d17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */

package conf

import "reflect"

type StoreCallback func()

var storeCallbacks []StoreCallback

func RegisterStoreChangeCallback(cb StoreCallback) {
	startWatchingConfigDir()
	cb()
	storeCallbacks = append(storeCallbacks, cb)
}

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
}