fix(platform): avoid duplicate creations of template cache

This commit is contained in:
L. Yeung 2023-05-20 10:46:50 +08:00 committed by Jan De Dobbeleer
parent c3c7c9e6e0
commit 61fa281894

View file

@ -190,6 +190,7 @@ type TemplateCache struct {
SHLVL int SHLVL int
Segments SegmentsCache Segments SegmentsCache
initialized bool
sync.RWMutex sync.RWMutex
} }
@ -312,6 +313,7 @@ func (env *Shell) Init() {
env.cmdCache = &commandCache{ env.cmdCache = &commandCache{
commands: NewConcurrentMap(), commands: NewConcurrentMap(),
} }
env.tmplCache = &TemplateCache{}
env.SetPromptCount() env.SetPromptCount()
} }
@ -776,6 +778,7 @@ func (env *Shell) LoadTemplateCache() {
env.Error(err) env.Error(err)
return return
} }
templateCache.initialized = true
env.tmplCache = &templateCache env.tmplCache = &templateCache
} }
@ -785,19 +788,21 @@ func (env *Shell) Logs() string {
func (env *Shell) TemplateCache() *TemplateCache { func (env *Shell) TemplateCache() *TemplateCache {
defer env.Trace(time.Now()) defer env.Trace(time.Now())
if env.tmplCache != nil { tmplCache := env.tmplCache
return env.tmplCache tmplCache.Lock()
defer tmplCache.Unlock()
if tmplCache.initialized {
return tmplCache
} }
tmplCache := &TemplateCache{ tmplCache.Root = env.Root()
Root: env.Root(), tmplCache.Shell = env.Shell()
Shell: env.Shell(), tmplCache.ShellVersion = env.CmdFlags.ShellVersion
ShellVersion: env.CmdFlags.ShellVersion, tmplCache.Code = env.ErrorCode()
Code: env.ErrorCode(), tmplCache.WSL = env.IsWsl()
WSL: env.IsWsl(), tmplCache.Segments = make(map[string]interface{})
Segments: make(map[string]interface{}), tmplCache.PromptCount = env.CmdFlags.PromptCount
PromptCount: env.CmdFlags.PromptCount,
}
tmplCache.Env = make(map[string]string) tmplCache.Env = make(map[string]string)
tmplCache.Var = make(map[string]interface{}) tmplCache.Var = make(map[string]interface{})
@ -838,7 +843,7 @@ func (env *Shell) TemplateCache() *TemplateCache {
tmplCache.SHLVL = shlvl tmplCache.SHLVL = shlvl
} }
env.tmplCache = tmplCache tmplCache.initialized = true
return tmplCache return tmplCache
} }