再优化reload机制

This commit is contained in:
xing 2023-02-19 00:14:33 +08:00
parent 5ca1a2c592
commit a18616f50f
10 changed files with 36 additions and 83 deletions

View File

@ -4,6 +4,15 @@ import "github.com/fthvgb1/wp-go/safety"
var calls []func() var calls []func()
var str = safety.NewMap[string, string]()
func GetStr(name string) (string, bool) {
return str.Load(name)
}
func SetStr(name, val string) {
str.Store(name, val)
}
func Vars[T any](defaults T) *safety.Var[T] { func Vars[T any](defaults T) *safety.Var[T] {
ss := safety.NewVar(defaults) ss := safety.NewVar(defaults)
calls = append(calls, func() { calls = append(calls, func() {
@ -27,4 +36,5 @@ func Reload() {
for _, call := range calls { for _, call := range calls {
call() call()
} }
str.Flush()
} }

View File

@ -6,6 +6,7 @@ import (
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/logs"
@ -53,6 +54,15 @@ func (h *Handle) GetPassword() {
} }
} }
func (h *Handle) AutoCal(name string, fn func() string) {
v, ok := reload.GetStr(name)
if !ok {
v = fn()
reload.SetStr(name, v)
}
h.GinH[name] = v
}
func (i *IndexHandle) ExecListPagePlugin(m map[string]Plugin[models.Posts, *Handle], calls ...func(*models.Posts)) { func (i *IndexHandle) ExecListPagePlugin(m map[string]Plugin[models.Posts, *Handle], calls ...func(*models.Posts)) {
pluginConf := config.GetConfig().ListPagePlugins pluginConf := config.GetConfig().ListPagePlugins

View File

@ -3,13 +3,9 @@ package common
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/html" "github.com/fthvgb1/wp-go/helper/html"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
) )
var css = reload.Vars(constraints.Defaults)
func (h *Handle) CalCustomCss() (r string) { func (h *Handle) CalCustomCss() (r string) {
if h.ThemeMods.CustomCssPostId < 1 { if h.ThemeMods.CustomCssPostId < 1 {
return return
@ -21,12 +17,3 @@ func (h *Handle) CalCustomCss() (r string) {
r = fmt.Sprintf(`<style id="wp-custom-css">%s</style>`, html.StripTags(post.PostContent, "")) r = fmt.Sprintf(`<style id="wp-custom-css">%s</style>`, html.StripTags(post.PostContent, ""))
return return
} }
func (h *Handle) CustomCss() {
cs := css.Load()
if cs == constraints.Defaults {
cs = h.CalCustomCss()
css.Store(cs)
}
h.GinH["customCss"] = cs
}

View File

@ -4,14 +4,10 @@ import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
) )
var logo = reload.Vars(constraints.Defaults)
func (h *Handle) CalCustomLogo() (r string) { func (h *Handle) CalCustomLogo() (r string) {
id := uint64(h.ThemeMods.CustomLogo) id := uint64(h.ThemeMods.CustomLogo)
if id < 1 { if id < 1 {
@ -44,12 +40,3 @@ func (h *Handle) CalCustomLogo() (r string) {
r = fmt.Sprintf(`<a href="%s" class="custom-logo-link" rel="home"%s>%s</a>`, "/", ` aria-current="page"`, r) r = fmt.Sprintf(`<a href="%s" class="custom-logo-link" rel="home"%s>%s</a>`, "/", ` aria-current="page"`, r)
return return
} }
func (h *Handle) CustomLogo() {
s := logo.Load()
if s == constraints.Defaults {
s = h.CalCustomLogo()
logo.Store(s)
}
h.GinH["customLogo"] = s
}

View File

@ -97,9 +97,10 @@ func (d *DetailHandle) Render() {
if d.CommentRender == nil { if d.CommentRender == nil {
d.CommentRender = plugins.CommentRender() d.CommentRender = plugins.CommentRender()
} }
d.SiteIcon() d.CalBodyClass()
d.CustomLogo() d.AutoCal("siteIcon", d.CalSiteIcon)
d.CustomCss() d.AutoCal("customLogo", d.CalCustomLogo)
d.AutoCal("customCss", d.CalCustomCss)
d.RenderComment() d.RenderComment()
d.CalBodyClass() d.CalBodyClass()
if d.Templ == "" { if d.Templ == "" {

View File

@ -112,9 +112,9 @@ func (i *IndexHandle) Render() {
i.PageEle = plugins.TwentyFifteenPagination() i.PageEle = plugins.TwentyFifteenPagination()
} }
i.Pagination() i.Pagination()
i.SiteIcon() i.AutoCal("siteIcon", i.CalSiteIcon)
i.CustomLogo() i.AutoCal("customLogo", i.CalCustomLogo)
i.CustomCss() i.AutoCal("customCss", i.CalCustomCss)
i.CalBodyClass() i.CalBodyClass()
if i.Templ == "" { if i.Templ == "" {
i.Templ = fmt.Sprintf("%s/posts/index.gohtml", i.Theme) i.Templ = fmt.Sprintf("%s/posts/index.gohtml", i.Theme)

View File

@ -4,14 +4,11 @@ import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"strings" "strings"
) )
var icon = reload.Vars(constraints.Defaults)
var sizes = []string{"site_icon-270", "site_icon-32", "site_icon-192", "site_icon-180"} var sizes = []string{"site_icon-270", "site_icon-32", "site_icon-192", "site_icon-180"}
func (h *Handle) CalSiteIcon() (r string) { func (h *Handle) CalSiteIcon() (r string) {
@ -42,12 +39,3 @@ func (h *Handle) CalSiteIcon() (r string) {
r = strings.Join(size, "\n") r = strings.Join(size, "\n")
return return
} }
func (h *Handle) SiteIcon() {
s := icon.Load()
if s == constraints.Defaults {
s = h.CalSiteIcon()
icon.Store(s)
}
h.GinH["siteIcon"] = s
}

View File

@ -3,24 +3,15 @@ package twentyfifteen
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"strconv" "strconv"
"strings" "strings"
) )
var colorCss = reload.Vars("default") func (h *handle) colorSchemeCss() string {
s := slice.Filter([]string{h.calColorSchemeCss(), h.calSidebarTextColorCss(), h.calHeaderBackgroundColorCss()}, func(s string) bool {
func (h *handle) colorSchemeCss() { return s != ""
color := colorCss.Load() })
if color == constraints.Defaults { return fmt.Sprintf(`<style id='%s-inline-css'%s>\n%s\n</style>\n`, "twentyfifteen-style", "", strings.Join(s, "\n"))
s := slice.Filter([]string{h.calColorSchemeCss(), h.calSidebarTextColorCss(), h.calHeaderBackgroundColorCss()}, func(s string) bool {
return s != ""
})
color = fmt.Sprintf(`<style id='%s-inline-css'%s>\n%s\n</style>\n`, "twentyfifteen-style", "", strings.Join(s, "\n"))
colorCss.Store(color)
}
h.IndexHandle.GinH["colorScheme"] = color
} }
func (h *handle) calColorSchemeCss() (r string) { func (h *handle) calColorSchemeCss() (r string) {
color := h.getColorScheme() color := h.getColorScheme()

View File

@ -5,8 +5,6 @@ import (
"github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
) )
var postx = map[string]string{ var postx = map[string]string{
@ -31,17 +29,6 @@ var repeat = map[string]string{
"no-repeat": "no-repeat", "no-repeat": "no-repeat",
} }
var background = reload.Vars(constraints.Defaults)
func (h *handle) CustomBackGround() {
b := background.Load()
if b == constraints.Defaults {
b = h.CalCustomBackGround()
background.Store(b)
}
h.IndexHandle.GinH["customBackground"] = b
}
func (h *handle) CalCustomBackGround() (r string) { func (h *handle) CalCustomBackGround() (r string) {
if h.IndexHandle.ThemeMods.BackgroundImage == "" && h.IndexHandle.ThemeMods.BackgroundColor == themesupport.CustomBackground.DefaultColor { if h.IndexHandle.ThemeMods.BackgroundImage == "" && h.IndexHandle.ThemeMods.BackgroundColor == themesupport.CustomBackground.DefaultColor {
return return

View File

@ -37,14 +37,6 @@ func newHandle(iHandle *common.IndexHandle, dHandle *common.DetailHandle) *handl
return &handle{iHandle, dHandle} return &handle{iHandle, dHandle}
} }
type detailHandle struct {
*common.DetailHandle
}
func newDetailHandle(dHandle *common.DetailHandle) *detailHandle {
return &detailHandle{DetailHandle: dHandle}
}
func Hook(h *common.Handle) { func Hook(h *common.Handle) {
h.WidgetAreaData() h.WidgetAreaData()
h.GetPassword() h.GetPassword()
@ -58,14 +50,14 @@ func Hook(h *common.Handle) {
func (h *handle) Index() { func (h *handle) Index() {
h.CustomHeader() h.CustomHeader()
h.colorSchemeCss() h.IndexHandle.AutoCal("colorScheme", h.colorSchemeCss)
h.CustomBackGround() h.IndexHandle.AutoCal("customBackground", h.CalCustomBackGround)
h.Indexs() h.Indexs()
} }
func (h *handle) Detail() { func (h *handle) Detail() {
h.CustomHeader() h.CustomHeader()
h.colorSchemeCss() h.IndexHandle.AutoCal("colorScheme", h.colorSchemeCss)
h.CustomBackGround() h.IndexHandle.AutoCal("customBackground", h.CalCustomBackGround)
h.Details() h.Details()
} }