Skip to content

Commit

Permalink
fix(template): store segment data synchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Nov 9, 2024
1 parent 665b487 commit 1bebcd1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/config/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
50 changes: 36 additions & 14 deletions src/maps/concurrent.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 2 additions & 4 deletions src/maps/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/runtime/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 1bebcd1

Please sign in to comment.