This commit is contained in:
xing 2023-02-28 20:51:02 +08:00
parent dd384b0169
commit cbf3cabb24
3 changed files with 24 additions and 8 deletions

View File

@ -28,7 +28,7 @@ type Handle struct {
Class []string Class []string
Components map[string][]Components Components map[string][]Components
ThemeMods wpconfig.ThemeMods ThemeMods wpconfig.ThemeMods
HandleFns map[int][]HandleFn[*Handle] HandleFns map[int][]HandleCall
Error error Error error
} }
@ -44,7 +44,7 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle {
Stats: constraints.Ok, Stats: constraints.Ok,
ThemeMods: mods, ThemeMods: mods,
Components: make(map[string][]Components), Components: make(map[string][]Components),
HandleFns: make(map[int][]HandleFn[*Handle]), HandleFns: make(map[int][]HandleCall),
} }
} }
@ -58,7 +58,7 @@ func NewComponents(fn func(*Handle) string, order int) Components {
return Components{Fn: fn, Order: order} return Components{Fn: fn, Order: order}
} }
func (h *Handle) PushHandleFn(stats int, fns ...HandleFn[*Handle]) { func (h *Handle) PushHandleFn(stats int, fns ...HandleCall) {
h.HandleFns[stats] = append(h.HandleFns[stats], fns...) h.HandleFns[stats] = append(h.HandleFns[stats], fns...)
} }
@ -88,15 +88,22 @@ func (h *Handle) GetPassword() {
func (h *Handle) ExecHandleFns() { func (h *Handle) ExecHandleFns() {
calls, ok := h.HandleFns[h.Stats] calls, ok := h.HandleFns[h.Stats]
if ok { if ok {
slice.SortSelf(calls, func(i, j HandleCall) bool {
return i.Order > j.Order
})
for _, call := range calls { for _, call := range calls {
call(h) call.Fn(h)
} }
} }
for _, fn := range h.HandleFns[constraints.AllStats] { fns, ok := h.HandleFns[constraints.AllStats]
fn(h) if ok {
for _, fn := range fns {
fn.Fn(h)
} }
} }
}
func (h *Handle) PreTemplate() { func (h *Handle) PreTemplate() {
if h.Templ == "" { if h.Templ == "" {
h.Templ = str.Join(h.Theme, "/posts/index.gohtml") h.Templ = str.Join(h.Theme, "/posts/index.gohtml")
@ -155,6 +162,15 @@ type HandleFn[T any] func(T)
type HandlePipeFn[T any] func(HandleFn[T], T) type HandlePipeFn[T any] func(HandleFn[T], T)
type HandleCall struct {
Fn HandleFn[*Handle]
Order int
}
func NewHandleFn(fn HandleFn[*Handle], order int) HandleCall {
return HandleCall{Fn: fn, Order: order}
}
// HandlePipe 方便把功能写在其它包里 // HandlePipe 方便把功能写在其它包里
func HandlePipe[T any](initial func(T), fns ...HandlePipeFn[T]) HandleFn[T] { func HandlePipe[T any](initial func(T), fns ...HandlePipeFn[T]) HandleFn[T] {
return slice.ReverseReduce(fns, func(next HandlePipeFn[T], f func(t T)) func(t T) { return slice.ReverseReduce(fns, func(next HandlePipeFn[T], f func(t T)) func(t T) {

View File

@ -41,7 +41,7 @@ func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
common.NewComponents(CalCustomBackGround, 10), common.NewComponents(CalCustomBackGround, 10),
common.NewComponents(colorSchemeCss, 10), common.NewComponents(colorSchemeCss, 10),
) )
h.PushHandleFn(constraints.AllStats, customHeader) h.PushHandleFn(constraints.AllStats, common.NewHandleFn(customHeader, 10))
switch h.Scene { switch h.Scene {
case constraints.Detail: case constraints.Detail:
detail(next, h.Detail) detail(next, h.Detail)

View File

@ -36,7 +36,7 @@ func Hook(h *common.Handle) {
func ready(next common.HandleFn[*common.Handle], h *common.Handle) { func ready(next common.HandleFn[*common.Handle], h *common.Handle) {
h.WidgetAreaData() h.WidgetAreaData()
h.GetPassword() h.GetPassword()
h.PushHandleFn(constraints.AllStats, calClass) h.PushHandleFn(constraints.AllStats, common.NewHandleFn(calClass, 10))
h.PushHeadScript( h.PushHeadScript(
common.NewComponents(colorScheme, 10), common.NewComponents(colorScheme, 10),
common.NewComponents(customHeader, 10), common.NewComponents(customHeader, 10),