优化代码

This commit is contained in:
xing 2023-02-23 20:22:42 +08:00
parent 2a87ebdfd6
commit bd7d20160a
4 changed files with 10 additions and 17 deletions

View File

@ -85,7 +85,7 @@ type HandleFn[T any] func(T)
type HandlePipeFn[T any] func(HandleFn[T], T) type HandlePipeFn[T any] func(HandleFn[T], T)
// HandlePipe 方便把功能写在其它包里 // HandlePipe 方便把功能写在其它包里
func HandlePipe[T any](fns []HandlePipeFn[T], initial func(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) {
return func(t T) { return func(t T) {
next(f, t) next(f, t)

View File

@ -17,19 +17,12 @@ type DetailHandle struct {
CommentRender plugins.CommentHtml CommentRender plugins.CommentHtml
Comments []models.Comments Comments []models.Comments
Post models.Posts Post models.Posts
Pipes []HandlePipeFn[*DetailHandle]
} }
func NewDetailHandle(handle *Handle) *DetailHandle { func NewDetailHandle(handle *Handle) *DetailHandle {
return &DetailHandle{Handle: handle} return &DetailHandle{Handle: handle}
} }
func (d *DetailHandle) Pipe(calls ...HandlePipeFn[*DetailHandle]) {
HandlePipe[*DetailHandle](append(calls, d.Pipes...), func(d *DetailHandle) {
d.Render()
})(d)
}
func (d *DetailHandle) BuildDetailData() (err error) { func (d *DetailHandle) BuildDetailData() (err error) {
d.GinH["title"] = wpconfig.GetOption("blogname") d.GinH["title"] = wpconfig.GetOption("blogname")
err = d.CheckAndGetPost() err = d.CheckAndGetPost()

View File

@ -22,19 +22,12 @@ type IndexHandle struct {
PageEle pagination.Elements PageEle pagination.Elements
TotalRows int TotalRows int
PostsPlugins map[string]Plugin[models.Posts, *Handle] PostsPlugins map[string]Plugin[models.Posts, *Handle]
Pipes []HandlePipeFn[*IndexHandle]
} }
func NewIndexHandle(handle *Handle) *IndexHandle { func NewIndexHandle(handle *Handle) *IndexHandle {
return &IndexHandle{Handle: handle} return &IndexHandle{Handle: handle}
} }
func (i *IndexHandle) Pipe(calls ...HandlePipeFn[*IndexHandle]) {
HandlePipe[*IndexHandle](append(calls, i.Pipes...), func(i *IndexHandle) {
i.Render()
})(i)
}
func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) { func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) {
i.Param = parm i.Param = parm
switch i.Scene { switch i.Scene {

View File

@ -28,16 +28,23 @@ var paginate = func() plugins.PageEle {
return p return p
}() }()
var detailPipe = common.HandlePipe(func(d *common.DetailHandle) {
d.Render()
}, detail)
var indexPipe = common.HandlePipe(func(i *common.IndexHandle) {
i.Render()
}, index)
func Hook(h *common.Handle) { func Hook(h *common.Handle) {
h.WidgetAreaData() h.WidgetAreaData()
h.GetPassword() h.GetPassword()
h.HandleFns = append(h.HandleFns, calClass) h.HandleFns = append(h.HandleFns, calClass)
h.GinH["HeaderImage"] = getHeaderImage(h.C) h.GinH["HeaderImage"] = getHeaderImage(h.C)
if h.Scene == constraints.Detail { if h.Scene == constraints.Detail {
common.NewDetailHandle(h).Pipe(detail) detailPipe(common.NewDetailHandle(h))
return return
} }
common.NewIndexHandle(h).Pipe(index) indexPipe(common.NewIndexHandle(h))
} }
var pluginFns = func() map[string]common.Plugin[models.Posts, *common.Handle] { var pluginFns = func() map[string]common.Plugin[models.Posts, *common.Handle] {