2023-02-17 15:36:54 +00:00
|
|
|
package reload
|
|
|
|
|
2023-03-04 06:44:51 +00:00
|
|
|
import (
|
2023-04-25 12:58:22 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/number"
|
2023-10-14 06:57:57 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
2023-03-04 06:44:51 +00:00
|
|
|
"github.com/fthvgb1/wp-go/safety"
|
2023-04-04 15:02:54 +00:00
|
|
|
"sync"
|
2023-03-04 06:44:51 +00:00
|
|
|
)
|
2023-02-17 15:36:54 +00:00
|
|
|
|
2023-10-14 06:57:57 +00:00
|
|
|
type queue struct {
|
|
|
|
fn func()
|
|
|
|
order float64
|
2023-10-23 14:32:20 +00:00
|
|
|
name string
|
2023-10-14 06:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var calls []queue
|
2023-10-23 14:32:20 +00:00
|
|
|
var callsM = safety.NewMap[string, func()]()
|
2023-02-17 15:36:54 +00:00
|
|
|
|
2023-03-06 15:43:58 +00:00
|
|
|
var anyMap = safety.NewMap[string, any]()
|
|
|
|
|
2023-04-04 15:02:54 +00:00
|
|
|
type safetyVar[T, A any] struct {
|
|
|
|
Val *safety.Var[val[T]]
|
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
|
|
|
type val[T any] struct {
|
2023-04-25 12:58:22 +00:00
|
|
|
v T
|
|
|
|
ok bool
|
|
|
|
counter number.Counter[int]
|
2023-04-04 15:02:54 +00:00
|
|
|
}
|
|
|
|
type safetyMap[K comparable, V, A any] struct {
|
|
|
|
val *safety.Map[K, V]
|
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
var safetyMaps = safety.NewMap[string, any]()
|
|
|
|
var safetyMapLock = sync.Mutex{}
|
|
|
|
|
2023-10-23 14:32:20 +00:00
|
|
|
var flushMapFn = safety.NewMap[string, func(any)]()
|
|
|
|
|
2023-10-23 14:47:21 +00:00
|
|
|
func FlushMapVal[T any](namespace string, key T) {
|
2023-10-23 14:32:20 +00:00
|
|
|
fn, ok := flushMapFn.Load(namespace)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fn(key)
|
|
|
|
}
|
|
|
|
|
2023-10-24 08:06:59 +00:00
|
|
|
func FlushAnyVal(namespaces ...string) {
|
|
|
|
for _, namespace := range namespaces {
|
|
|
|
fn, ok := callsM.Load(namespace)
|
|
|
|
if !ok {
|
2023-10-24 08:07:48 +00:00
|
|
|
continue
|
2023-10-24 08:06:59 +00:00
|
|
|
}
|
|
|
|
fn()
|
2023-10-23 14:32:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 12:58:22 +00:00
|
|
|
func GetAnyMapFnBys[K comparable, V, A any](namespace string, fn func(A) V) func(key K, args A) V {
|
2023-04-04 15:02:54 +00:00
|
|
|
m := safetyMapFn[K, V, A](namespace)
|
|
|
|
return func(key K, a A) V {
|
|
|
|
v, ok := m.val.Load(key)
|
|
|
|
if ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
m.mutex.Lock()
|
|
|
|
v, ok = m.val.Load(key)
|
|
|
|
if ok {
|
|
|
|
m.mutex.Unlock()
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
v = fn(a)
|
|
|
|
m.val.Store(key, v)
|
|
|
|
m.mutex.Unlock()
|
|
|
|
return v
|
2023-03-06 15:43:58 +00:00
|
|
|
}
|
2023-02-18 16:14:33 +00:00
|
|
|
}
|
2023-04-04 15:02:54 +00:00
|
|
|
|
2023-10-23 14:32:20 +00:00
|
|
|
func safetyMapFn[K comparable, V, A any](namespace string, args ...any) *safetyMap[K, V, A] {
|
2023-04-04 15:02:54 +00:00
|
|
|
vv, ok := safetyMaps.Load(namespace)
|
|
|
|
var m *safetyMap[K, V, A]
|
2023-03-18 05:17:21 +00:00
|
|
|
if ok {
|
2023-04-04 15:02:54 +00:00
|
|
|
m = vv.(*safetyMap[K, V, A])
|
|
|
|
} else {
|
|
|
|
safetyMapLock.Lock()
|
|
|
|
vv, ok = safetyMaps.Load(namespace)
|
|
|
|
if ok {
|
|
|
|
m = vv.(*safetyMap[K, V, A])
|
|
|
|
} else {
|
|
|
|
m = &safetyMap[K, V, A]{safety.NewMap[K, V](), sync.Mutex{}}
|
2023-10-23 14:32:20 +00:00
|
|
|
ord, _ := parseArgs(args...)
|
|
|
|
flushMapFn.Store(namespace, func(a any) {
|
|
|
|
k, ok := a.(K)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.val.Delete(k)
|
|
|
|
})
|
2023-04-04 15:02:54 +00:00
|
|
|
Push(func() {
|
|
|
|
m.val.Flush()
|
2023-10-23 14:32:20 +00:00
|
|
|
}, ord, namespace)
|
2023-04-04 15:02:54 +00:00
|
|
|
safetyMaps.Store(namespace, m)
|
|
|
|
}
|
|
|
|
safetyMapLock.Unlock()
|
2023-03-18 05:17:21 +00:00
|
|
|
}
|
2023-04-04 15:02:54 +00:00
|
|
|
return m
|
2023-03-18 05:17:21 +00:00
|
|
|
}
|
2023-02-18 16:14:33 +00:00
|
|
|
|
2023-10-23 14:32:20 +00:00
|
|
|
func GetAnyValMapBy[K comparable, V, A any](namespace string, key K, a A, fn func(A) V, args ...any) V {
|
|
|
|
m := safetyMapFn[K, V, A](namespace, args...)
|
2023-04-04 15:02:54 +00:00
|
|
|
v, ok := m.val.Load(key)
|
2023-03-04 06:44:51 +00:00
|
|
|
if ok {
|
|
|
|
return v
|
|
|
|
}
|
2023-04-04 15:02:54 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
v, ok = m.val.Load(key)
|
|
|
|
if ok {
|
|
|
|
m.mutex.Unlock()
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
v = fn(a)
|
|
|
|
m.val.Store(key, v)
|
|
|
|
m.mutex.Unlock()
|
2023-03-04 06:44:51 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:32:20 +00:00
|
|
|
func anyVal[T, A any](namespace string, counter bool, args ...any) *safetyVar[T, A] {
|
2023-04-04 15:02:54 +00:00
|
|
|
var vv *safetyVar[T, A]
|
|
|
|
vvv, ok := safetyMaps.Load(namespace)
|
|
|
|
if ok {
|
|
|
|
vv = vvv.(*safetyVar[T, A])
|
|
|
|
} else {
|
|
|
|
safetyMapLock.Lock()
|
|
|
|
vvv, ok = safetyMaps.Load(namespace)
|
|
|
|
if ok {
|
|
|
|
vv = vvv.(*safetyVar[T, A])
|
|
|
|
} else {
|
2023-04-25 12:58:22 +00:00
|
|
|
v := val[T]{}
|
|
|
|
if counter {
|
|
|
|
v.counter = number.Counters[int]()
|
|
|
|
}
|
|
|
|
vv = &safetyVar[T, A]{safety.NewVar(v), sync.Mutex{}}
|
2023-10-23 14:32:20 +00:00
|
|
|
ord, _ := parseArgs(args...)
|
2023-04-04 15:02:54 +00:00
|
|
|
Push(func() {
|
|
|
|
vv.Val.Flush()
|
2023-10-23 14:32:20 +00:00
|
|
|
}, ord, namespace)
|
2023-04-04 15:02:54 +00:00
|
|
|
safetyMaps.Store(namespace, vv)
|
|
|
|
}
|
|
|
|
safetyMapLock.Unlock()
|
|
|
|
}
|
2023-04-25 12:58:22 +00:00
|
|
|
return vv
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:32:20 +00:00
|
|
|
func GetAnyValBy[T, A any](namespace string, tryTimes int, a A, fn func(A) (T, bool), args ...any) T {
|
|
|
|
var vv = anyVal[T, A](namespace, true, args...)
|
2023-04-25 12:58:22 +00:00
|
|
|
var ok bool
|
|
|
|
v := vv.Val.Load()
|
|
|
|
if v.ok {
|
|
|
|
return v.v
|
|
|
|
}
|
|
|
|
vv.mutex.Lock()
|
|
|
|
v = vv.Val.Load()
|
|
|
|
if v.ok {
|
|
|
|
vv.mutex.Unlock()
|
|
|
|
return v.v
|
|
|
|
}
|
|
|
|
v.v, ok = fn(a)
|
|
|
|
times := v.counter()
|
2023-04-25 13:05:10 +00:00
|
|
|
if ok || times >= tryTimes {
|
2023-04-25 12:58:22 +00:00
|
|
|
v.ok = true
|
|
|
|
vv.Val.Store(v)
|
|
|
|
}
|
|
|
|
vv.mutex.Unlock()
|
|
|
|
return v.v
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:32:20 +00:00
|
|
|
func GetAnyValBys[T, A any](namespace string, a A, fn func(A) T, args ...any) T {
|
|
|
|
var vv = anyVal[T, A](namespace, false, args...)
|
2023-04-04 15:02:54 +00:00
|
|
|
v := vv.Val.Load()
|
|
|
|
if v.ok {
|
|
|
|
return v.v
|
|
|
|
}
|
|
|
|
vv.mutex.Lock()
|
|
|
|
v = vv.Val.Load()
|
|
|
|
if v.ok {
|
|
|
|
vv.mutex.Unlock()
|
|
|
|
return v.v
|
|
|
|
}
|
|
|
|
v.v = fn(a)
|
|
|
|
v.ok = true
|
|
|
|
vv.Val.Store(v)
|
|
|
|
vv.mutex.Unlock()
|
|
|
|
return v.v
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:32:20 +00:00
|
|
|
func Vars[T any](defaults T, args ...any) *safety.Var[T] {
|
2023-02-17 15:36:54 +00:00
|
|
|
ss := safety.NewVar(defaults)
|
2023-10-23 14:32:20 +00:00
|
|
|
ord, name := parseArgs(args...)
|
|
|
|
Push(func() {
|
2023-02-17 15:36:54 +00:00
|
|
|
ss.Store(defaults)
|
2023-10-23 14:32:20 +00:00
|
|
|
}, ord, name)
|
2023-02-17 15:36:54 +00:00
|
|
|
return ss
|
|
|
|
}
|
2023-10-14 06:57:57 +00:00
|
|
|
|
2023-10-23 14:32:20 +00:00
|
|
|
func parseArgs(a ...any) (ord float64, name string) {
|
|
|
|
if len(a) > 0 {
|
|
|
|
for _, arg := range a {
|
|
|
|
v, ok := arg.(float64)
|
|
|
|
if ok {
|
|
|
|
ord = v
|
|
|
|
}
|
|
|
|
vv, ok := arg.(string)
|
|
|
|
if ok {
|
|
|
|
name = vv
|
|
|
|
}
|
|
|
|
}
|
2023-10-14 06:57:57 +00:00
|
|
|
}
|
2023-10-23 14:32:20 +00:00
|
|
|
return ord, name
|
2023-10-14 06:57:57 +00:00
|
|
|
}
|
2023-10-23 14:32:20 +00:00
|
|
|
func VarsBy[T any](fn func() T, args ...any) *safety.Var[T] {
|
2023-02-17 15:36:54 +00:00
|
|
|
ss := safety.NewVar(fn())
|
2023-10-23 14:32:20 +00:00
|
|
|
ord, name := parseArgs(args...)
|
|
|
|
Push(func() {
|
|
|
|
ss.Store(fn())
|
|
|
|
}, ord, name)
|
2023-02-17 15:36:54 +00:00
|
|
|
return ss
|
|
|
|
}
|
2023-10-23 14:32:20 +00:00
|
|
|
func MapBy[K comparable, T any](fn func(*safety.Map[K, T]), args ...any) *safety.Map[K, T] {
|
2023-08-26 14:01:20 +00:00
|
|
|
m := safety.NewMap[K, T]()
|
|
|
|
if fn != nil {
|
|
|
|
fn(m)
|
|
|
|
}
|
2023-10-23 14:32:20 +00:00
|
|
|
ord, name := parseArgs(args...)
|
|
|
|
Push(func() {
|
|
|
|
m.Flush()
|
|
|
|
if fn != nil {
|
|
|
|
fn(m)
|
|
|
|
}
|
|
|
|
}, ord, name)
|
2023-08-26 14:01:20 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:32:20 +00:00
|
|
|
func SafeMap[K comparable, T any](args ...any) *safety.Map[K, T] {
|
2023-08-26 14:01:20 +00:00
|
|
|
m := safety.NewMap[K, T]()
|
2023-10-23 14:32:20 +00:00
|
|
|
ord, name := parseArgs(args...)
|
|
|
|
Push(func() {
|
2023-08-26 14:01:20 +00:00
|
|
|
m.Flush()
|
2023-10-23 14:32:20 +00:00
|
|
|
}, ord, name)
|
2023-08-26 14:01:20 +00:00
|
|
|
return m
|
|
|
|
}
|
2023-02-17 15:36:54 +00:00
|
|
|
|
2023-10-23 14:32:20 +00:00
|
|
|
func Push(fn func(), a ...any) {
|
|
|
|
ord, name := parseArgs(a...)
|
|
|
|
calls = append(calls, queue{fn, ord, name})
|
|
|
|
if name != "" {
|
|
|
|
callsM.Store(name, fn)
|
|
|
|
}
|
2023-02-17 15:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Reload() {
|
2023-10-23 14:38:52 +00:00
|
|
|
anyMap.Flush()
|
|
|
|
safetyMaps.Flush()
|
|
|
|
callsM.Flush()
|
|
|
|
flushMapFn.Flush()
|
2023-10-14 06:57:57 +00:00
|
|
|
slice.Sort(calls, func(i, j queue) bool {
|
|
|
|
return i.order > j.order
|
|
|
|
})
|
2023-02-17 15:36:54 +00:00
|
|
|
for _, call := range calls {
|
2023-10-14 06:57:57 +00:00
|
|
|
call.fn()
|
2023-02-17 15:36:54 +00:00
|
|
|
}
|
2023-10-23 14:38:52 +00:00
|
|
|
return
|
2023-02-17 15:36:54 +00:00
|
|
|
}
|