From e25f679ea68760ff307856c9c7cc315247ae821e Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 22 Jan 2024 20:29:17 +0800 Subject: [PATCH] optimize code --- app/actions/themehook.go | 2 -- app/plugins/wphandle/tests/tt.go | 2 +- app/theme/hook.go | 9 +++-- app/theme/wp/pipe.go | 57 ++++++++++++++++++++++---------- app/theme/wp/wp.go | 17 +++------- 5 files changed, 52 insertions(+), 35 deletions(-) diff --git a/app/actions/themehook.go b/app/actions/themehook.go index aa7d1b4..d6f5300 100644 --- a/app/actions/themehook.go +++ b/app/actions/themehook.go @@ -10,8 +10,6 @@ func ThemeHook(scene string) func(*gin.Context) { return func(c *gin.Context) { t := theme.GetCurrentTemplateName() h := wp.NewHandle(c, scene, t) - templ, _ := theme.GetTemplate(t) - h.SetTemplate(templ) theme.Hook(t, h) } } diff --git a/app/plugins/wphandle/tests/tt.go b/app/plugins/wphandle/tests/tt.go index 49944a6..43cd9df 100644 --- a/app/plugins/wphandle/tests/tt.go +++ b/app/plugins/wphandle/tests/tt.go @@ -7,7 +7,7 @@ import ( ) func Tt(h *wp.Handle) { - h.HookHandle(constraints.PipeMiddleware, func(call wp.HandleCall) (wp.HandleCall, bool) { + h.HookHandle(constraints.PipeMiddleware, constraints.AllScene, func(call wp.HandleCall) (wp.HandleCall, bool) { return call, false }) /*h.PushPipeHook(constraints.Home, func(pipe wp.Pipe) (wp.Pipe, bool) { diff --git a/app/theme/hook.go b/app/theme/hook.go index 7f1b720..5acbcad 100644 --- a/app/theme/hook.go +++ b/app/theme/hook.go @@ -1,8 +1,8 @@ package theme import ( - "github.com/fthvgb1/wp-go/app/theme/twentyfifteen" "github.com/fthvgb1/wp-go/app/theme/wp" + str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/safety" ) @@ -15,11 +15,16 @@ func AddThemeHookFunc(name string, fn func(handle *wp.Handle)) { themeMap.Store(name, fn) } +func IsThemeHookFuncExist(name string) bool { + _, ok := themeMap.Load(name) + return ok +} + func Hook(themeName string, h *wp.Handle) { fn, ok := themeMap.Load(themeName) if ok && fn != nil { fn(h) return } - twentyfifteen.Hook(h) + panic(str.Join("theme ", themeName, " don't exist")) } diff --git a/app/theme/wp/pipe.go b/app/theme/wp/pipe.go index 4fadd54..4983cbb 100644 --- a/app/theme/wp/pipe.go +++ b/app/theme/wp/pipe.go @@ -67,9 +67,10 @@ func (h *Handle) PushDataHandler(scene string, fns ...HandleCall) { } func BuildHandlers(pipeScene string, keyFn func(*Handle, string) string, - fn func(*Handle, map[string][]HandleCall, string) []HandleCall) func(HandleFn[*Handle], *Handle) { + 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) { - pipeHandlerFn := reload.BuildMapFn[string]("pipeHandlers", BuildHandler(pipeScene, keyFn, fn)) + pipeHandlerFn := reload.BuildMapFn[string]("pipeHandlers", BuildHandler(pipeScene, keyFn, handleHook, handlesFn)) return func(next HandleFn[*Handle], h *Handle) { key := keyFn(h, pipeScene) @@ -87,16 +88,18 @@ func BuildHandlers(pipeScene string, keyFn func(*Handle, string) string, } func BuildHandler(pipeScene string, keyFn func(*Handle, string) string, - fn func(*Handle, map[string][]HandleCall, string) []HandleCall) func(*Handle) []HandleCall { + handleHook func(*Handle, map[string][]func(HandleCall) (HandleCall, bool)) []func(HandleCall) (HandleCall, bool), + handlesFn func(*Handle, map[string][]HandleCall, string) []HandleCall) func(*Handle) []HandleCall { return func(h *Handle) []HandleCall { key := keyFn(h, pipeScene) mut := reload.GetGlobeMutex() mut.Lock() - hookers, _ := handleHooks.Load(pipeScene) - hh, _ := handlerss.Load(pipeScene) + pipeHookers, _ := handleHooks.Load(pipeScene) + pipeHandlers, _ := handlerss.Load(pipeScene) mut.Unlock() - calls := fn(h, hh, key) + hookers := handleHook(h, pipeHookers) + calls := handlesFn(h, pipeHandlers, key) calls = slice.FilterAndMap(calls, func(call HandleCall) (HandleCall, bool) { ok := true for _, hook := range hookers { @@ -183,19 +186,34 @@ func PipeRender(h *Handle, renders map[string][]HandleCall, key string) (handler return } +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 +} + // DeleteHandle 写插件的时候用 -func (h *Handle) DeleteHandle(pipeScene string, name string) { - v, _ := handleHooks.Load(pipeScene) - v = append(v, func(call HandleCall) (HandleCall, bool) { +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) { return call, name != call.Name }) handleHooks.Store(pipeScene, v) } // ReplaceHandle 写插件的时候用 -func (h *Handle) ReplaceHandle(pipeScene, name string, fn HandleFn[*Handle]) { - v, _ := handleHooks.Load(pipeScene) - v = append(v, func(call HandleCall) (HandleCall, bool) { +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) { if name == call.Name { call.Fn = fn } @@ -205,9 +223,12 @@ func (h *Handle) ReplaceHandle(pipeScene, name string, fn HandleFn[*Handle]) { } // HookHandle 写插件的时候用 -func (h *Handle) HookHandle(pipeScene string, hook func(HandleCall) (HandleCall, bool)) { - v, _ := handleHooks.Load(pipeScene) - v = append(v, hook) +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) handleHooks.Store(pipeScene, v) } @@ -224,10 +245,10 @@ func (h *Handle) PipeHandleHook(name string, calls []HandleCall, m map[string][] func InitPipe(h *Handle) { h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeMiddleware, 300, - BuildHandlers(constraints.PipeMiddleware, MiddlewareKey, PipeMiddlewareHandle))) + BuildHandlers(constraints.PipeMiddleware, MiddlewareKey, HandleHook, PipeMiddlewareHandle))) h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeData, 200, - BuildHandlers(constraints.PipeData, PipeKey, PipeDataHandle))) + BuildHandlers(constraints.PipeData, PipeKey, HandleHook, PipeDataHandle))) h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeRender, 100, - BuildHandlers(constraints.PipeRender, PipeKey, PipeRender))) + BuildHandlers(constraints.PipeRender, PipeKey, HandleHook, PipeRender))) } diff --git a/app/theme/wp/wp.go b/app/theme/wp/wp.go index 4b2f3ec..168b55c 100644 --- a/app/theme/wp/wp.go +++ b/app/theme/wp/wp.go @@ -34,11 +34,10 @@ type Handle struct { err error abort bool stopPipe bool - template *template.Template } var handlerss = safety.NewMap[string, map[string][]HandleCall]() -var handleHooks = safety.NewMap[string, []func(HandleCall) (HandleCall, bool)]() +var handleHooks = safety.NewMap[string, map[string][]func(HandleCall) (HandleCall, bool)]() func (h *Handle) Theme() string { return h.theme @@ -64,18 +63,10 @@ func (h *Handle) Handlers() *safety.Map[string, map[string][]HandleCall] { return handlerss } -func (h *Handle) HandleHook() *safety.Map[string, []func(HandleCall) (HandleCall, bool)] { +func (h *Handle) HandleHook() *safety.Map[string, map[string][]func(HandleCall) (HandleCall, bool)] { return handleHooks } -func (h *Handle) SetTemplate(template *template.Template) { - h.template = template -} - -func (h *Handle) Template() *template.Template { - return h.template -} - type HandlePlugins map[string]HandleFn[*Handle] // Components Order 为执行顺序,降序执行 @@ -110,7 +101,6 @@ func SetConfigHandle(a ...any) Handle { fnHook.Flush() h.C = hh.C h.theme = hh.theme - h.template = hh.template configFn(h) v := apply.UsePlugins() pluginFn, ok := v.(func(*Handle)) @@ -164,6 +154,9 @@ func (h *Handle) SetErr(err error) { func (h *Handle) SetTempl(templ string) { h.templ = templ } +func (h *Handle) GetTempl() string { + return h.templ +} func (h *Handle) Scene() string { return h.scene