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
}
segment.writer.SetText(text)
segment.SetText(text)
segment.setCache()
segment.env.TemplateCache().AddSegmentData(segment.Name(), segment.writer)
}
func (segment *Segment) Text() string {

View file

@ -1,36 +1,58 @@
package maps
import "sync"
import (
"sync"
)
func NewConcurrent() *Concurrent {
var cm Concurrent
return &cm
return &Concurrent{
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) {
(*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) {
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) {
(*sync.Map)(cm).Delete(key)
cm.Lock()
defer cm.Unlock()
delete(cm.data, key)
}
func (cm *Concurrent) Contains(key string) bool {
_, ok := (*sync.Map)(cm).Load(key)
_, ok := cm.Get(key)
return ok
}
func (cm *Concurrent) ToSimple() Simple {
list := make(map[string]any)
(*sync.Map)(cm).Range(func(key, value any) bool {
list[key.(string)] = value
return true
})
return list
cm.RLock()
defer cm.RUnlock()
return cm.data
}

View file

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

View file

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