优化: 资源消耗优化,发布 v0.2.1
This commit is contained in:
@@ -2,6 +2,8 @@ package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"charm.land/bubbletea/v2"
|
||||
)
|
||||
@@ -22,6 +24,11 @@ type Model struct {
|
||||
wsTabCur int // tabs 列光标 (0..len(Groups), 最后一个是历史入口)
|
||||
forkMode bool // 分叉输入模式
|
||||
forkBuf string // 分叉方向提示输入
|
||||
|
||||
// 性能缓存
|
||||
allSessionsCache []*Session // "全部" tab 会话切片缓存
|
||||
allSessionsDirty bool // 缓存是否需要重建
|
||||
cacheDirty bool // session-cache.json 是否需要写盘
|
||||
}
|
||||
|
||||
func NewModel() *Model {
|
||||
@@ -52,6 +59,8 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.history.Cache = msg.Cache
|
||||
m.history.Loaded = true
|
||||
m.history.Scanning = false
|
||||
m.allSessionsDirty = true
|
||||
m.cacheDirty = true
|
||||
if m.history.Favorites == nil {
|
||||
m.history.Favorites = loadFavorites()
|
||||
}
|
||||
@@ -77,7 +86,9 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return m, nil
|
||||
case SummaryResultMsg:
|
||||
m.applySummary(msg.SessionID, msg.Summary, msg.Completed, msg.Pending)
|
||||
return m, m.nextSummaryCmd()
|
||||
m.cacheDirty = true
|
||||
cmds := []tea.Cmd{m.nextSummaryCmd(), cacheFlushTick()}
|
||||
return m, tea.Batch(cmds...)
|
||||
case UpdateAvailableMsg:
|
||||
m.update.Checking = false
|
||||
m.update.Available = true
|
||||
@@ -94,6 +105,12 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.update.Updating = false
|
||||
m.update.Error = msg.Err
|
||||
m.update.Checking = false
|
||||
case cacheFlushMsg:
|
||||
m.flushCache()
|
||||
return m, nil
|
||||
case tea.QuitMsg:
|
||||
m.flushCache()
|
||||
return m, tea.Quit
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
@@ -101,3 +118,21 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
func (m *Model) GetPendingCmd() string {
|
||||
return m.pendingCmd
|
||||
}
|
||||
|
||||
func (m *Model) flushCache() {
|
||||
if !m.cacheDirty || m.history.Cache == nil {
|
||||
return
|
||||
}
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
saveCache(home, m.history.Cache)
|
||||
m.cacheDirty = false
|
||||
}
|
||||
|
||||
func cacheFlushTick() tea.Cmd {
|
||||
return tea.Tick(3*time.Second, func(_ time.Time) tea.Msg {
|
||||
return cacheFlushMsg{}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user