2023-05-03 15:57:49 +00:00
|
|
|
package wp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-10-30 13:52:15 +00:00
|
|
|
"github.com/fthvgb1/wp-go/safety"
|
2023-05-03 15:57:49 +00:00
|
|
|
)
|
|
|
|
|
2023-12-05 13:39:13 +00:00
|
|
|
var fnMap = safety.NewMap[string, map[string]any]()
|
|
|
|
var fnHook = safety.NewMap[string, map[string]any]()
|
2023-05-03 15:57:49 +00:00
|
|
|
|
|
|
|
func GetFn[T any](fnType string, name string) []T {
|
2023-10-30 13:52:15 +00:00
|
|
|
v, ok := fnMap.Load(fnType)
|
2023-05-03 15:57:49 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
vv, ok := v[name]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return vv.([]T)
|
|
|
|
}
|
|
|
|
func GetFnHook[T any](fnType string, name string) []T {
|
2023-10-30 13:52:15 +00:00
|
|
|
v, ok := fnHook.Load(fnType)
|
2023-05-03 15:57:49 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
vv, ok := v[name]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return vv.([]T)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PushFn[T any](fnType string, name string, fns ...T) error {
|
2023-10-30 13:52:15 +00:00
|
|
|
v, ok := fnMap.Load(fnType)
|
2023-05-03 15:57:49 +00:00
|
|
|
if !ok {
|
|
|
|
v = make(map[string]any)
|
2023-10-30 13:52:15 +00:00
|
|
|
fnMap.Store(fnType, v)
|
2023-05-03 15:57:49 +00:00
|
|
|
v[name] = fns
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
vv, ok := v[name]
|
|
|
|
if !ok || vv == nil {
|
|
|
|
v[name] = fns
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s, ok := vv.([]T)
|
|
|
|
if ok {
|
|
|
|
s = append(s, fns...)
|
|
|
|
v[name] = s
|
|
|
|
}
|
|
|
|
return errors.New("error fn type")
|
|
|
|
}
|
|
|
|
|
|
|
|
func PushFnHook[T any](fnType string, name string, fns ...T) error {
|
2023-10-30 13:52:15 +00:00
|
|
|
v, ok := fnHook.Load(fnType)
|
2023-05-03 15:57:49 +00:00
|
|
|
if !ok {
|
|
|
|
v = make(map[string]any)
|
2023-10-30 13:52:15 +00:00
|
|
|
fnHook.Store(fnType, v)
|
2023-05-03 15:57:49 +00:00
|
|
|
v[name] = fns
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
vv, ok := v[name]
|
|
|
|
if !ok || vv == nil {
|
|
|
|
v[name] = fns
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s, ok := vv.([]T)
|
|
|
|
if ok {
|
|
|
|
s = append(s, fns...)
|
|
|
|
v[name] = s
|
|
|
|
}
|
|
|
|
return errors.New("error fn type")
|
|
|
|
}
|