optimize code

This commit is contained in:
xing 2024-01-22 20:29:17 +08:00
parent 4842d53316
commit e25f679ea6
5 changed files with 52 additions and 35 deletions

View File

@ -10,8 +10,6 @@ func ThemeHook(scene string) func(*gin.Context) {
return func(c *gin.Context) { return func(c *gin.Context) {
t := theme.GetCurrentTemplateName() t := theme.GetCurrentTemplateName()
h := wp.NewHandle(c, scene, t) h := wp.NewHandle(c, scene, t)
templ, _ := theme.GetTemplate(t)
h.SetTemplate(templ)
theme.Hook(t, h) theme.Hook(t, h)
} }
} }

View File

@ -7,7 +7,7 @@ import (
) )
func Tt(h *wp.Handle) { 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 return call, false
}) })
/*h.PushPipeHook(constraints.Home, func(pipe wp.Pipe) (wp.Pipe, bool) { /*h.PushPipeHook(constraints.Home, func(pipe wp.Pipe) (wp.Pipe, bool) {

View File

@ -1,8 +1,8 @@
package theme package theme
import ( import (
"github.com/fthvgb1/wp-go/app/theme/twentyfifteen"
"github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/theme/wp"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/safety" "github.com/fthvgb1/wp-go/safety"
) )
@ -15,11 +15,16 @@ func AddThemeHookFunc(name string, fn func(handle *wp.Handle)) {
themeMap.Store(name, fn) themeMap.Store(name, fn)
} }
func IsThemeHookFuncExist(name string) bool {
_, ok := themeMap.Load(name)
return ok
}
func Hook(themeName string, h *wp.Handle) { func Hook(themeName string, h *wp.Handle) {
fn, ok := themeMap.Load(themeName) fn, ok := themeMap.Load(themeName)
if ok && fn != nil { if ok && fn != nil {
fn(h) fn(h)
return return
} }
twentyfifteen.Hook(h) panic(str.Join("theme ", themeName, " don't exist"))
} }

View File

@ -67,9 +67,10 @@ func (h *Handle) PushDataHandler(scene string, fns ...HandleCall) {
} }
func BuildHandlers(pipeScene string, keyFn func(*Handle, string) string, 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) { return func(next HandleFn[*Handle], h *Handle) {
key := keyFn(h, pipeScene) 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, 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 { return func(h *Handle) []HandleCall {
key := keyFn(h, pipeScene) key := keyFn(h, pipeScene)
mut := reload.GetGlobeMutex() mut := reload.GetGlobeMutex()
mut.Lock() mut.Lock()
hookers, _ := handleHooks.Load(pipeScene) pipeHookers, _ := handleHooks.Load(pipeScene)
hh, _ := handlerss.Load(pipeScene) pipeHandlers, _ := handlerss.Load(pipeScene)
mut.Unlock() 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) { calls = slice.FilterAndMap(calls, func(call HandleCall) (HandleCall, bool) {
ok := true ok := true
for _, hook := range hookers { for _, hook := range hookers {
@ -183,19 +186,34 @@ func PipeRender(h *Handle, renders map[string][]HandleCall, key string) (handler
return 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 写插件的时候用 // DeleteHandle 写插件的时候用
func (h *Handle) DeleteHandle(pipeScene string, name string) { func (h *Handle) DeleteHandle(pipeScene, scene, name string) {
v, _ := handleHooks.Load(pipeScene) v, ok := handleHooks.Load(pipeScene)
v = append(v, func(call HandleCall) (HandleCall, bool) { 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 return call, name != call.Name
}) })
handleHooks.Store(pipeScene, v) handleHooks.Store(pipeScene, v)
} }
// ReplaceHandle 写插件的时候用 // ReplaceHandle 写插件的时候用
func (h *Handle) ReplaceHandle(pipeScene, name string, fn HandleFn[*Handle]) { func (h *Handle) ReplaceHandle(pipeScene, scene, name string, fn HandleFn[*Handle]) {
v, _ := handleHooks.Load(pipeScene) v, ok := handleHooks.Load(pipeScene)
v = append(v, func(call HandleCall) (HandleCall, bool) { if !ok {
v = make(map[string][]func(HandleCall) (HandleCall, bool))
}
v[scene] = append(v[scene], func(call HandleCall) (HandleCall, bool) {
if name == call.Name { if name == call.Name {
call.Fn = fn call.Fn = fn
} }
@ -205,9 +223,12 @@ func (h *Handle) ReplaceHandle(pipeScene, name string, fn HandleFn[*Handle]) {
} }
// HookHandle 写插件的时候用 // HookHandle 写插件的时候用
func (h *Handle) HookHandle(pipeScene string, hook func(HandleCall) (HandleCall, bool)) { func (h *Handle) HookHandle(pipeScene, scene string, hook func(HandleCall) (HandleCall, bool)) {
v, _ := handleHooks.Load(pipeScene) v, ok := handleHooks.Load(pipeScene)
v = append(v, hook) if !ok {
v = make(map[string][]func(HandleCall) (HandleCall, bool))
}
v[scene] = append(v[scene], hook)
handleHooks.Store(pipeScene, v) handleHooks.Store(pipeScene, v)
} }
@ -224,10 +245,10 @@ func (h *Handle) PipeHandleHook(name string, calls []HandleCall, m map[string][]
func InitPipe(h *Handle) { func InitPipe(h *Handle) {
h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeMiddleware, 300, 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, 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, h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeRender, 100,
BuildHandlers(constraints.PipeRender, PipeKey, PipeRender))) BuildHandlers(constraints.PipeRender, PipeKey, HandleHook, PipeRender)))
} }

View File

@ -34,11 +34,10 @@ type Handle struct {
err error err error
abort bool abort bool
stopPipe bool stopPipe bool
template *template.Template
} }
var handlerss = safety.NewMap[string, map[string][]HandleCall]() 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 { func (h *Handle) Theme() string {
return h.theme return h.theme
@ -64,18 +63,10 @@ func (h *Handle) Handlers() *safety.Map[string, map[string][]HandleCall] {
return handlerss 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 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] type HandlePlugins map[string]HandleFn[*Handle]
// Components Order 为执行顺序,降序执行 // Components Order 为执行顺序,降序执行
@ -110,7 +101,6 @@ func SetConfigHandle(a ...any) Handle {
fnHook.Flush() fnHook.Flush()
h.C = hh.C h.C = hh.C
h.theme = hh.theme h.theme = hh.theme
h.template = hh.template
configFn(h) configFn(h)
v := apply.UsePlugins() v := apply.UsePlugins()
pluginFn, ok := v.(func(*Handle)) pluginFn, ok := v.(func(*Handle))
@ -164,6 +154,9 @@ func (h *Handle) SetErr(err error) {
func (h *Handle) SetTempl(templ string) { func (h *Handle) SetTempl(templ string) {
h.templ = templ h.templ = templ
} }
func (h *Handle) GetTempl() string {
return h.templ
}
func (h *Handle) Scene() string { func (h *Handle) Scene() string {
return h.scene return h.scene