optimize code and fix bug

This commit is contained in:
xing 2024-01-19 15:18:55 +08:00
parent 9285698ee4
commit c60146d614
5 changed files with 14 additions and 18 deletions

View File

@ -28,7 +28,6 @@ func configs(h *wp.Handle) {
h.PushCacheGroupHeadScript(constraints.AllScene, "colorSchemeCss", 10.0056, colorSchemeCss) h.PushCacheGroupHeadScript(constraints.AllScene, "colorSchemeCss", 10.0056, colorSchemeCss)
h.CommonComponents() h.CommonComponents()
components.WidgetArea(h) components.WidgetArea(h)
wp.ReplyCommentJs(h)
h.SetData("customHeader", customHeader(h)) h.SetData("customHeader", customHeader(h))
wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(wp.IndexRender, 50.005, "wp.IndexRender")) wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(wp.IndexRender, 50.005, "wp.IndexRender"))
h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50.005, "wp.DetailRender")) h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50.005, "wp.DetailRender"))

View File

@ -69,7 +69,6 @@ func configs(h *wp.Handle) {
h.SetData("colophon", colophon) h.SetData("colophon", colophon)
setPaginationAndRender(h) setPaginationAndRender(h)
h.CommonComponents() h.CommonComponents()
wp.ReplyCommentJs(h)
h.PushPostPlugin(postThumbnail) h.PushPostPlugin(postThumbnail)
wp.SetComponentsArgsForMap(widgets.Search, "{$form}", searchForm) wp.SetComponentsArgsForMap(widgets.Search, "{$form}", searchForm)
wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(wp.IndexRender, 10.005, "wp.IndexRender")) wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(wp.IndexRender, 10.005, "wp.IndexRender"))

View File

@ -39,24 +39,21 @@ func (h *Handle) HookComponents(scene string, fn func(Components[string]) (Compo
handleComponentHook.Store(scene, v) handleComponentHook.Store(scene, v)
} }
var getComponentFn = reload.BuildMapFn[string]("scene-components", getComponent) var GetComponents = reload.BuildMapFn[string]("scene-components", getComponent)
var hookComponentFn = reload.BuildMapFn[string]("calComponents", hookComponent) var HookComponents = reload.BuildMapFnWithAnyParams[string]("calComponents", hookComponent)
type componentParam struct { func hookComponent(a ...any) []Components[string] {
components []Components[string] k := a[0].(string)
k string components := a[1].([]Components[string])
}
func hookComponent(p componentParam) []Components[string] {
mut := reload.GetGlobeMutex() mut := reload.GetGlobeMutex()
mut.Lock() mut.Lock()
allHooks := slice.FilterAndToMap(p.components, func(t Components[string], _ int) (string, []func(Components[string]) (Components[string], bool), bool) { allHooks := slice.FilterAndToMap(components, func(t Components[string], _ int) (string, []func(Components[string]) (Components[string], bool), bool) {
fn, ok := handleComponentHook.Load(p.k) fn, ok := handleComponentHook.Load(k)
return p.k, fn, ok return k, fn, ok
}) })
mut.Unlock() mut.Unlock()
r := slice.FilterAndMap(p.components, func(component Components[string]) (Components[string], bool) { r := slice.FilterAndMap(components, func(component Components[string]) (Components[string], bool) {
hooks, ok := allHooks[p.k] hooks, ok := allHooks[k]
if !ok { if !ok {
return component, true return component, true
} }
@ -100,10 +97,10 @@ func cacheComponentFn(a cacheComponentParm[string]) string {
} }
func CalComponents(h *Handle) { func CalComponents(h *Handle) {
allComponents := getComponentFn(str.Join("allScene-", h.scene), h) allComponents := GetComponents(str.Join("allScene-", h.scene), h)
for k, components := range allComponents { for k, components := range allComponents {
key := str.Join("calComponents-", h.scene, "-", k) key := str.Join("calComponents-", h.scene, "-", k)
hookedComponents := hookComponentFn(key, componentParam{components, k}) hookedComponents := HookComponents(key, k, components)
var s = make([]string, 0, len(hookedComponents)) var s = make([]string, 0, len(hookedComponents))
for _, component := range hookedComponents { for _, component := range hookedComponents {
if component.Val != "" { if component.Val != "" {

View File

@ -248,6 +248,7 @@ func (h *Handle) CommonComponents() {
h.PushCacheGroupHeadScript(constraints.AllScene, "siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss) h.PushCacheGroupHeadScript(constraints.AllScene, "siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
h.PushRender(constraints.AllStats, NewHandleFn(CalComponents, 10.001, "wp.CalComponents")) h.PushRender(constraints.AllStats, NewHandleFn(CalComponents, 10.001, "wp.CalComponents"))
h.PushRender(constraints.AllStats, NewHandleFn(PreRenderTemplate, 0, "wp.PreRenderTemplate")) h.PushRender(constraints.AllStats, NewHandleFn(PreRenderTemplate, 0, "wp.PreRenderTemplate"))
ReplyCommentJs(h)
AdditionScript(h) AdditionScript(h)
} }

View File

@ -132,7 +132,7 @@ func BuildMapFnWithAnyParams[K comparable, V any](namespace string, fn func(...a
if ok { if ok {
return v return v
} }
v = fn(a) v = fn(a...)
m.Val.Store(key, v) m.Val.Store(key, v)
return v return v
} }