Skip to content

Commit

Permalink
lock the cmdQueue to avoid losing quickly entered commands
Browse files Browse the repository at this point in the history
  • Loading branch information
psethwick committed Feb 17, 2024
1 parent dd0ecbb commit 4abe6b9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"sync"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
Expand Down Expand Up @@ -68,7 +69,8 @@ type mainModel struct {
sub chan struct{}
gMenu bool

cmdQueue *todoist.Commands
cmdQueue *todoist.Commands
syncMutex sync.Mutex

projectId string
sectionId string
Expand Down Expand Up @@ -102,8 +104,10 @@ func initialModel() *mainModel {
m.statusBarModel = status.New()
m.taskList = tasklist.New(func(t string) { m.statusBarModel.SetTitle(t) }, dbg)
m.inputModel = input.New(func() { m.state = viewInput }, func() { m.state = viewTasks }, keys.InputKeys)

m.local = new(todoist.Store)
m.cmdQueue = new(todoist.Commands)
m.syncMutex = sync.Mutex{}

m.sub = make(chan struct{})
return &m
Expand Down Expand Up @@ -365,7 +369,7 @@ func main() {
}
defer f.Close()
}
p := tea.NewProgram(initialModel(), tea.WithAltScreen())
p := tea.NewProgram(initialModel()) //, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
Expand Down
6 changes: 6 additions & 0 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ func (m *mainModel) sync(cmds ...todoist.Command) tea.Cmd {
m.statusBarModel.SetSyncStatus(status.Syncing)
if len(cmds) > 0 {
m.applyCmds(cmds) // only 'new' ones
m.syncMutex.Lock()
*m.cmdQueue = append(*m.cmdQueue, cmds...)
m.syncMutex.Unlock()
}
return func() tea.Msg {
if len(*m.cmdQueue) > 0 {
Expand All @@ -209,7 +211,9 @@ func (m *mainModel) sync(cmds ...todoist.Command) tea.Cmd {
if err != nil {
dbg(err)
// the cache is the 'real' store and any unflushed commands
m.syncMutex.Lock()
err = client.WriteCache(m.client.Store, m.cmdQueue)
m.syncMutex.Unlock()
dbg(err)
return nil // no reason to sync if api calls aren't working
}
Expand Down Expand Up @@ -248,6 +252,8 @@ func (m *mainModel) sync(cmds ...todoist.Command) tea.Cmd {
dbg(err)
}
} else {
m.syncMutex.Lock()
defer m.syncMutex.Unlock()
syncToken := m.client.Store.SyncToken
incStore, err := m.client.IncrementalSync(m.ctx, syncToken)
if err != nil {
Expand Down

0 comments on commit 4abe6b9

Please sign in to comment.