wp-go/internal/cmd/reload/reload.go

72 lines
1.2 KiB
Go
Raw Normal View History

package reload
2023-03-04 06:44:51 +00:00
import (
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/safety"
"strings"
)
var calls []func()
2023-02-18 16:14:33 +00:00
var str = safety.NewMap[string, string]()
2023-03-06 15:43:58 +00:00
var anyMap = safety.NewMap[string, any]()
func GetAnyValBy[T any](k string, fn func() T) T {
v, ok := anyMap.Load(k)
if ok {
return v.(T)
}
vv := fn()
anyMap.Store(k, vv)
return vv
2023-02-18 16:14:33 +00:00
}
2023-03-18 05:17:21 +00:00
func GetAnyValBys[T, A any](k string, a A, fn func(A) T) T {
v, ok := anyMap.Load(k)
if ok {
return v.(T)
}
vv := fn(a)
anyMap.Store(k, vv)
return vv
}
2023-02-18 16:14:33 +00:00
2023-03-04 06:44:51 +00:00
func GetStrBy[T any](key, delimiter string, t T, fn ...func(T) string) string {
v, ok := str.Load(key)
if ok {
return v
}
v = strings.Join(slice.Map(fn, func(vv func(T) string) string {
return vv(t)
}), delimiter)
str.Store(key, v)
return v
}
func Vars[T any](defaults T) *safety.Var[T] {
ss := safety.NewVar(defaults)
calls = append(calls, func() {
ss.Store(defaults)
})
return ss
}
func VarsBy[T any](fn func() T) *safety.Var[T] {
ss := safety.NewVar(fn())
calls = append(calls, func() {
ss.Store(fn())
})
return ss
}
func Push(fn ...func()) {
calls = append(calls, fn...)
}
func Reload() {
for _, call := range calls {
call()
}
2023-03-06 15:43:58 +00:00
anyMap.Flush()
2023-02-18 16:14:33 +00:00
str.Flush()
}