optimize code
This commit is contained in:
parent
4842d53316
commit
e25f679ea6
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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"))
|
||||
}
|
||||
|
|
|
@ -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)))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user