2023-04-21 17:12:39 +00:00
|
|
|
package wp
|
|
|
|
|
|
|
|
import (
|
2023-05-04 12:37:06 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/constraints"
|
2023-11-12 13:39:04 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache/reload"
|
2023-04-21 17:12:39 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
2023-04-29 14:05:24 +00:00
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2023-04-21 17:12:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HandlePipeFn[T any] func(HandleFn[T], T)
|
|
|
|
|
2023-05-03 15:57:49 +00:00
|
|
|
type Pipe struct {
|
|
|
|
Name string
|
2023-06-15 10:00:57 +00:00
|
|
|
Order float64
|
2023-05-03 15:57:49 +00:00
|
|
|
Fn HandlePipeFn[*Handle]
|
|
|
|
}
|
|
|
|
|
2023-06-15 10:00:57 +00:00
|
|
|
func NewPipe(name string, order float64, fn HandlePipeFn[*Handle]) Pipe {
|
2023-05-03 15:57:49 +00:00
|
|
|
return Pipe{Name: name, Order: order, Fn: fn}
|
|
|
|
}
|
|
|
|
|
2023-04-21 17:12:39 +00:00
|
|
|
// HandlePipe 方便把功能写在其它包里
|
|
|
|
func HandlePipe[T any](initial func(T), fns ...HandlePipeFn[T]) HandleFn[T] {
|
2024-01-04 15:32:10 +00:00
|
|
|
return slice.ReverseReduce(fns, func(next HandlePipeFn[T], f HandleFn[T]) HandleFn[T] {
|
2023-04-21 17:12:39 +00:00
|
|
|
return func(t T) {
|
|
|
|
next(f, t)
|
|
|
|
}
|
|
|
|
}, initial)
|
|
|
|
}
|
|
|
|
|
2023-05-03 15:57:49 +00:00
|
|
|
func (h *Handle) PushPipe(scene string, pipes ...Pipe) error {
|
|
|
|
return PushFn("pipe", scene, pipes...)
|
|
|
|
}
|
|
|
|
func (h *Handle) PushPipeHook(scene string, pipes ...func(Pipe) (Pipe, bool)) error {
|
|
|
|
return PushFnHook("pipeHook", scene, pipes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) DeletePipe(scene, pipeName string) error {
|
|
|
|
return PushFnHook("pipeHook", scene, func(pipe Pipe) (Pipe, bool) {
|
|
|
|
return pipe, pipeName != pipe.Name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) ReplacePipe(scene, pipeName string, pipe Pipe) error {
|
|
|
|
return PushFnHook("pipeHook", scene, func(p Pipe) (Pipe, bool) {
|
|
|
|
if pipeName == p.Name {
|
|
|
|
p = pipe
|
|
|
|
}
|
|
|
|
return p, true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-24 13:51:43 +00:00
|
|
|
func (h *Handle) PushHandler(pipScene string, scene string, fns ...HandleCall) {
|
2024-01-18 15:29:50 +00:00
|
|
|
v, ok := handlerss.Load(pipScene)
|
|
|
|
if !ok {
|
|
|
|
v = make(map[string][]HandleCall)
|
2023-04-21 17:12:39 +00:00
|
|
|
}
|
2024-01-18 15:29:50 +00:00
|
|
|
v[scene] = append(v[scene], fns...)
|
|
|
|
handlerss.Store(pipScene, v)
|
2023-04-21 17:12:39 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 13:51:43 +00:00
|
|
|
func (h *Handle) PushRender(statsOrScene string, fns ...HandleCall) {
|
|
|
|
h.PushHandler(constraints.PipeRender, statsOrScene, fns...)
|
2023-04-21 17:12:39 +00:00
|
|
|
}
|
2023-04-24 13:51:43 +00:00
|
|
|
func (h *Handle) PushDataHandler(scene string, fns ...HandleCall) {
|
|
|
|
h.PushHandler(constraints.PipeData, scene, fns...)
|
2023-04-21 17:12:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
func BuildHandlers(pipeScene string, keyFn func(*Handle, string) string,
|
2024-01-22 12:29:17 +00:00
|
|
|
handleHook func(*Handle, map[string][]func(HandleCall) (HandleCall, bool)) []func(HandleCall) (HandleCall, bool),
|
|
|
|
handlesFn func(*Handle, map[string][]HandleCall, string) []HandleCall) func(HandleFn[*Handle], *Handle) {
|
2024-01-18 15:29:50 +00:00
|
|
|
|
2024-01-22 12:29:17 +00:00
|
|
|
pipeHandlerFn := reload.BuildMapFn[string]("pipeHandlers", BuildHandler(pipeScene, keyFn, handleHook, handlesFn))
|
2024-01-18 15:29:50 +00:00
|
|
|
|
2023-04-21 17:12:39 +00:00
|
|
|
return func(next HandleFn[*Handle], h *Handle) {
|
2023-05-08 14:24:39 +00:00
|
|
|
key := keyFn(h, pipeScene)
|
2024-01-18 15:29:50 +00:00
|
|
|
handlers := pipeHandlerFn(key, h)
|
2023-04-21 17:12:39 +00:00
|
|
|
for _, handler := range handlers {
|
|
|
|
handler.Fn(h)
|
|
|
|
if h.abort {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !h.stopPipe {
|
|
|
|
next(h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
func BuildHandler(pipeScene string, keyFn func(*Handle, string) string,
|
2024-01-22 12:29:17 +00:00
|
|
|
handleHook func(*Handle, map[string][]func(HandleCall) (HandleCall, bool)) []func(HandleCall) (HandleCall, bool),
|
|
|
|
handlesFn func(*Handle, map[string][]HandleCall, string) []HandleCall) func(*Handle) []HandleCall {
|
2024-01-18 15:29:50 +00:00
|
|
|
|
|
|
|
return func(h *Handle) []HandleCall {
|
|
|
|
key := keyFn(h, pipeScene)
|
|
|
|
mut := reload.GetGlobeMutex()
|
|
|
|
mut.Lock()
|
2024-01-22 12:29:17 +00:00
|
|
|
pipeHookers, _ := handleHooks.Load(pipeScene)
|
|
|
|
pipeHandlers, _ := handlerss.Load(pipeScene)
|
2024-01-18 15:29:50 +00:00
|
|
|
mut.Unlock()
|
2024-01-22 12:29:17 +00:00
|
|
|
hookers := handleHook(h, pipeHookers)
|
|
|
|
calls := handlesFn(h, pipeHandlers, key)
|
2024-01-18 15:29:50 +00:00
|
|
|
calls = slice.FilterAndMap(calls, func(call HandleCall) (HandleCall, bool) {
|
|
|
|
ok := true
|
|
|
|
for _, hook := range hookers {
|
|
|
|
call, ok = hook(call)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return call, ok
|
|
|
|
})
|
|
|
|
slice.SimpleSort(calls, slice.DESC, func(t HandleCall) float64 {
|
|
|
|
return t.Order
|
|
|
|
})
|
|
|
|
return calls
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 13:51:43 +00:00
|
|
|
func PipeKey(h *Handle, pipScene string) string {
|
2023-04-29 14:05:24 +00:00
|
|
|
key := str.Join("pipekey", "-", pipScene, "-", h.scene, "-", h.Stats)
|
2023-08-26 14:01:20 +00:00
|
|
|
return h.DoActionFilter("pipeKey", key, pipScene)
|
2023-04-21 17:12:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
var pipeInitFn = reload.BuildMapFn[string]("pipeInit", BuildPipe)
|
|
|
|
|
2023-05-03 15:57:49 +00:00
|
|
|
func Run(h *Handle, conf func(*Handle)) {
|
2024-01-18 15:29:50 +00:00
|
|
|
if !h.isInited {
|
2023-05-05 12:45:20 +00:00
|
|
|
InitHandle(conf, h)
|
|
|
|
}
|
2024-01-18 15:29:50 +00:00
|
|
|
pipeInitFn(h.scene, h.scene)(h)
|
2024-01-13 13:15:54 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
func BuildPipe(scene string) func(*Handle) {
|
|
|
|
pipees := GetFn[Pipe]("pipe", constraints.AllScene)
|
|
|
|
pipees = append(pipees, GetFn[Pipe]("pipe", scene)...)
|
|
|
|
pipes := slice.FilterAndMap(pipees, func(pipe Pipe) (Pipe, bool) {
|
2024-01-13 13:15:54 +00:00
|
|
|
var ok bool
|
2024-01-18 15:29:50 +00:00
|
|
|
mut := reload.GetGlobeMutex()
|
|
|
|
mut.Lock()
|
2024-01-13 13:15:54 +00:00
|
|
|
hooks := GetFnHook[func(Pipe) (Pipe, bool)]("pipeHook", constraints.AllScene)
|
2024-01-18 15:29:50 +00:00
|
|
|
hooks = append(hooks, GetFnHook[func(Pipe) (Pipe, bool)]("pipeHook", scene)...)
|
|
|
|
mut.Unlock()
|
2024-01-13 13:15:54 +00:00
|
|
|
for _, fn := range hooks {
|
|
|
|
pipe, ok = fn(pipe)
|
|
|
|
if !ok {
|
|
|
|
return pipe, false
|
2023-05-03 15:57:49 +00:00
|
|
|
}
|
2024-01-13 13:15:54 +00:00
|
|
|
}
|
|
|
|
return pipe, pipe.Fn != nil
|
|
|
|
})
|
|
|
|
slice.SimpleSort(pipes, slice.DESC, func(t Pipe) float64 {
|
|
|
|
return t.Order
|
|
|
|
})
|
2023-12-05 13:39:13 +00:00
|
|
|
|
2024-01-13 13:15:54 +00:00
|
|
|
arr := slice.Map(pipes, func(t Pipe) HandlePipeFn[*Handle] {
|
|
|
|
return t.Fn
|
|
|
|
})
|
2024-01-18 15:29:50 +00:00
|
|
|
return HandlePipe(NothingToDo, arr...)
|
2023-05-03 15:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MiddlewareKey(h *Handle, pipScene string) string {
|
2023-12-05 13:39:13 +00:00
|
|
|
return h.DoActionFilter("middleware", str.Join("pipe-middleware-", h.scene), pipScene)
|
2023-05-03 15:57:49 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 14:24:39 +00:00
|
|
|
func PipeMiddlewareHandle(h *Handle, middlewares map[string][]HandleCall, key string) (handlers []HandleCall) {
|
2023-05-03 15:57:49 +00:00
|
|
|
handlers = append(handlers, middlewares[h.scene]...)
|
|
|
|
handlers = append(handlers, middlewares[constraints.AllScene]...)
|
2023-05-08 16:56:28 +00:00
|
|
|
handlers = h.PipeHandleHook("PipeMiddlewareHandle", handlers, middlewares, key)
|
2023-05-03 15:57:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-08 14:24:39 +00:00
|
|
|
func PipeDataHandle(h *Handle, dataHandlers map[string][]HandleCall, key string) (handlers []HandleCall) {
|
2023-04-21 17:12:39 +00:00
|
|
|
handlers = append(handlers, dataHandlers[h.scene]...)
|
|
|
|
handlers = append(handlers, dataHandlers[constraints.AllScene]...)
|
2023-05-08 16:56:28 +00:00
|
|
|
handlers = h.PipeHandleHook("PipeDataHandle", handlers, dataHandlers, key)
|
2023-04-21 17:12:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-08 14:24:39 +00:00
|
|
|
func PipeRender(h *Handle, renders map[string][]HandleCall, key string) (handlers []HandleCall) {
|
2023-04-21 17:12:39 +00:00
|
|
|
handlers = append(handlers, renders[h.Stats]...)
|
|
|
|
handlers = append(handlers, renders[h.scene]...)
|
|
|
|
handlers = append(handlers, renders[constraints.AllStats]...)
|
|
|
|
handlers = append(handlers, renders[constraints.AllScene]...)
|
2023-05-08 16:56:28 +00:00
|
|
|
handlers = h.PipeHandleHook("PipeRender", handlers, renders, key)
|
2023-04-21 17:12:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-22 12:29:17 +00:00
|
|
|
func HandleHook(h *Handle, hooks map[string][]func(HandleCall) (HandleCall, bool)) []func(HandleCall) (HandleCall, bool) {
|
|
|
|
var r []func(HandleCall) (HandleCall, bool)
|
|
|
|
r = append(r, hooks[h.scene]...)
|
|
|
|
r = append(r, hooks[h.Stats]...)
|
|
|
|
r = append(r, hooks[constraints.AllScene]...)
|
|
|
|
r = append(r, hooks[constraints.AllStats]...)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2023-04-21 17:12:39 +00:00
|
|
|
// DeleteHandle 写插件的时候用
|
2024-01-22 12:29:17 +00:00
|
|
|
func (h *Handle) DeleteHandle(pipeScene, scene, name string) {
|
|
|
|
v, ok := handleHooks.Load(pipeScene)
|
|
|
|
if !ok {
|
|
|
|
v = make(map[string][]func(HandleCall) (HandleCall, bool))
|
|
|
|
}
|
|
|
|
v[scene] = append(v[scene], func(call HandleCall) (HandleCall, bool) {
|
2023-04-21 17:12:39 +00:00
|
|
|
return call, name != call.Name
|
|
|
|
})
|
2024-01-18 15:29:50 +00:00
|
|
|
handleHooks.Store(pipeScene, v)
|
2023-04-21 17:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceHandle 写插件的时候用
|
2024-01-22 12:29:17 +00:00
|
|
|
func (h *Handle) ReplaceHandle(pipeScene, scene, name string, fn HandleFn[*Handle]) {
|
|
|
|
v, ok := handleHooks.Load(pipeScene)
|
|
|
|
if !ok {
|
|
|
|
v = make(map[string][]func(HandleCall) (HandleCall, bool))
|
|
|
|
}
|
|
|
|
v[scene] = append(v[scene], func(call HandleCall) (HandleCall, bool) {
|
2023-04-21 17:12:39 +00:00
|
|
|
if name == call.Name {
|
|
|
|
call.Fn = fn
|
|
|
|
}
|
|
|
|
return call, true
|
|
|
|
})
|
2024-01-18 15:29:50 +00:00
|
|
|
handleHooks.Store(pipeScene, v)
|
2023-04-21 17:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HookHandle 写插件的时候用
|
2024-01-22 12:29:17 +00:00
|
|
|
func (h *Handle) HookHandle(pipeScene, scene string, hook func(HandleCall) (HandleCall, bool)) {
|
|
|
|
v, ok := handleHooks.Load(pipeScene)
|
|
|
|
if !ok {
|
|
|
|
v = make(map[string][]func(HandleCall) (HandleCall, bool))
|
|
|
|
}
|
|
|
|
v[scene] = append(v[scene], hook)
|
2024-01-18 15:29:50 +00:00
|
|
|
handleHooks.Store(pipeScene, v)
|
2023-04-21 17:12:39 +00:00
|
|
|
}
|
2023-05-03 15:57:49 +00:00
|
|
|
|
|
|
|
func (h *Handle) PushPipeHandleHook(name string, fn ...func([]HandleCall) []HandleCall) error {
|
|
|
|
return PushFnHook("pipeHandleHook", name, fn...)
|
|
|
|
}
|
|
|
|
|
2023-05-08 16:56:28 +00:00
|
|
|
func (h *Handle) PipeHandleHook(name string, calls []HandleCall, m map[string][]HandleCall, key string) []HandleCall {
|
|
|
|
fn := GetFnHook[func(*Handle, []HandleCall, map[string][]HandleCall, string) []HandleCall]("pipeHandleHook", name)
|
|
|
|
return slice.Reduce(fn, func(t func(*Handle, []HandleCall, map[string][]HandleCall, string) []HandleCall, r []HandleCall) []HandleCall {
|
|
|
|
return t(h, r, m, key)
|
2023-05-03 15:57:49 +00:00
|
|
|
}, calls)
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitPipe(h *Handle) {
|
2023-12-05 13:39:13 +00:00
|
|
|
h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeMiddleware, 300,
|
2024-01-22 12:29:17 +00:00
|
|
|
BuildHandlers(constraints.PipeMiddleware, MiddlewareKey, HandleHook, PipeMiddlewareHandle)))
|
2023-05-03 15:57:49 +00:00
|
|
|
|
|
|
|
h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeData, 200,
|
2024-01-22 12:29:17 +00:00
|
|
|
BuildHandlers(constraints.PipeData, PipeKey, HandleHook, PipeDataHandle)))
|
2023-05-03 15:57:49 +00:00
|
|
|
h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeRender, 100,
|
2024-01-22 12:29:17 +00:00
|
|
|
BuildHandlers(constraints.PipeRender, PipeKey, HandleHook, PipeRender)))
|
2023-05-03 15:57:49 +00:00
|
|
|
}
|