diff --git a/internal/theme/wp/bodyclass.go b/internal/theme/wp/bodyclass.go index 9881f40..f06d6c7 100644 --- a/internal/theme/wp/bodyclass.go +++ b/internal/theme/wp/bodyclass.go @@ -12,7 +12,7 @@ import ( ) func CalBodyClass(h *Handle) { - h.ginH["bodyClass"] = h.BodyClass(h.class...) + h.ginH["bodyClass"] = h.BodyClass(h.bodyClass...) } func (h *Handle) BodyClass(class ...string) string { diff --git a/internal/theme/wp/wp.go b/internal/theme/wp/wp.go index bb7412d..b1e3a4c 100644 --- a/internal/theme/wp/wp.go +++ b/internal/theme/wp/wp.go @@ -12,6 +12,7 @@ import ( "github.com/gin-gonic/gin" "net/http" "strings" + "sync" ) type Handle struct { @@ -26,7 +27,7 @@ type Handle struct { Code int Stats int templ string - class []string + bodyClass []string components map[string][]Components[string] themeMods wpconfig.ThemeMods handleFns map[int][]HandleCall @@ -36,6 +37,9 @@ type Handle struct { componentFilterFn map[string][]func(*Handle, string, ...any) string } +var configHandle = reload.Vars(&Handle{}) +var configHandleMux = sync.Mutex{} + type HandlePlugins map[string]HandleFn[*Handle] // Components Order 为执行顺序,降序执行 @@ -55,6 +59,24 @@ type HandleCall struct { Order int } +func InitThemeArgAndConfig(fn func(*Handle) *Handle, h *Handle) { + hh := configHandle.Load() + if len(hh.handleFns) < 1 { + configHandleMux.Lock() + hh = configHandle.Load() + if len(hh.handleFns) < 1 { + hh = fn(h) + configHandle.Store(hh) + } + configHandleMux.Unlock() + } + h.components = hh.components + h.handleFns = hh.handleFns + h.componentsArgs = hh.componentsArgs + h.componentFilterFn = hh.componentFilterFn + h.ginH = maps.Copy(hh.ginH) +} + func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) { fn, ok := h.componentFilterFn[name] return fn, ok @@ -109,7 +131,7 @@ func (h *Handle) SetData(k string, v any) { } func (h *Handle) PushClass(class ...string) { - h.class = append(h.class, class...) + h.bodyClass = append(h.bodyClass, class...) } func GetComponentsArgs[T any](h *Handle, k string, defaults T) T { @@ -202,7 +224,7 @@ func (h *Handle) PushGroupHandleFn(statsOrScene, order int, fns ...HandleFn[*Han } func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) { - h.ginH[name] = reload.GetAnyValBys(name, h, fn) + h.components[name] = append(h.components[name], h.NewCacheComponent(name, 10, fn)) } func (h *Handle) PushHeadScript(fn ...Components[string]) {