fix(template): store segment data synchronous

This commit is contained in:
Jan De Dobbeleer 2024-11-08 21:37:25 +01:00 committed by Jan De Dobbeleer
parent 665b487727
commit 1bebcd1f5d
4 changed files with 41 additions and 20 deletions

View file

@ -138,8 +138,9 @@ func (segment *Segment) Render() {
return return
} }
segment.writer.SetText(text) segment.SetText(text)
segment.setCache() segment.setCache()
segment.env.TemplateCache().AddSegmentData(segment.Name(), segment.writer)
} }
func (segment *Segment) Text() string { func (segment *Segment) Text() string {

View file

@ -1,36 +1,58 @@
package maps package maps
import "sync" import (
"sync"
)
func NewConcurrent() *Concurrent { func NewConcurrent() *Concurrent {
var cm Concurrent return &Concurrent{
return &cm data: make(map[string]any),
}
} }
type Concurrent sync.Map type Concurrent struct {
data map[string]any
sync.RWMutex
}
func (cm *Concurrent) Set(key string, value any) { func (cm *Concurrent) Set(key string, value any) {
(*sync.Map)(cm).Store(key, value) cm.Lock()
defer cm.Unlock()
if cm.data == nil {
cm.data = make(map[string]any)
}
cm.data[key] = value
} }
func (cm *Concurrent) Get(key string) (any, bool) { func (cm *Concurrent) Get(key string) (any, bool) {
return (*sync.Map)(cm).Load(key) cm.RLock()
defer cm.RUnlock()
if cm.data == nil {
return nil, false
}
value, ok := cm.data[key]
return value, ok
} }
func (cm *Concurrent) Delete(key string) { func (cm *Concurrent) Delete(key string) {
(*sync.Map)(cm).Delete(key) cm.Lock()
defer cm.Unlock()
delete(cm.data, key)
} }
func (cm *Concurrent) Contains(key string) bool { func (cm *Concurrent) Contains(key string) bool {
_, ok := (*sync.Map)(cm).Load(key) _, ok := cm.Get(key)
return ok return ok
} }
func (cm *Concurrent) ToSimple() Simple { func (cm *Concurrent) ToSimple() Simple {
list := make(map[string]any) cm.RLock()
(*sync.Map)(cm).Range(func(key, value any) bool { defer cm.RUnlock()
list[key.(string)] = value
return true return cm.data
})
return list
} }

View file

@ -3,9 +3,7 @@ package maps
type Simple map[string]any type Simple map[string]any
func (m Simple) ToConcurrent() *Concurrent { func (m Simple) ToConcurrent() *Concurrent {
var cm Concurrent return &Concurrent{
for k, v := range m { data: m,
cm.Set(k, v)
} }
return &cm
} }

View file

@ -37,10 +37,10 @@ type Terminal struct {
deviceCache *cache.File deviceCache *cache.File
sessionCache *cache.File sessionCache *cache.File
tmplCache *cache.Template tmplCache *cache.Template
lsDirMap maps.Concurrent
cwd string cwd string
host string host string
networks []*Connection networks []*Connection
lsDirMap maps.Concurrent
} }
func (term *Terminal) Init() { func (term *Terminal) Init() {