fix: use atomic for ids (#634)

closes #622

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Ville Valkonen <weezel@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker
2024-10-07 15:26:12 -03:00
committed by GitHub
co-authored by Ville Valkonen
parent 4382fdf1c6
commit 9a262e9b0f
5 changed files with 15 additions and 48 deletions
+3 -10
View File
@@ -7,7 +7,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
@@ -15,17 +15,10 @@ import (
"github.com/dustin/go-humanize"
)
var (
lastID int
idMtx sync.Mutex
)
var lastID int64
// Return the next ID we should use on the Model.
func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
lastID++
return lastID
return int(atomic.AddInt64(&lastID, 1))
}
// New returns a new filepicker model with default styling and key bindings.
+3 -10
View File
@@ -4,7 +4,7 @@ import (
"fmt"
"math"
"strings"
"sync"
"sync/atomic"
"time"
tea "github.com/charmbracelet/bubbletea"
@@ -17,17 +17,10 @@ import (
// Internal ID management. Used during animating to assure that frame messages
// can only be received by progress components that sent them.
var (
lastID int
idMtx sync.Mutex
)
var lastID int64
// Return the next ID we should use on the model.
func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
lastID++
return lastID
return int(atomic.AddInt64(&lastID, 1))
}
const (
+3 -10
View File
@@ -1,7 +1,7 @@
package spinner
import (
"sync"
"sync/atomic"
"time"
tea "github.com/charmbracelet/bubbletea"
@@ -10,17 +10,10 @@ import (
// Internal ID management. Used during animating to ensure that frame messages
// are received only by spinner components that sent them.
var (
lastID int
idMtx sync.Mutex
)
var lastID int64
// Return the next ID we should use on the Model.
func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
lastID++
return lastID
return int(atomic.AddInt64(&lastID, 1))
}
// Spinner is a set of frames used in animating the spinner.
+3 -9
View File
@@ -2,22 +2,16 @@
package stopwatch
import (
"sync"
"sync/atomic"
"time"
tea "github.com/charmbracelet/bubbletea"
)
var (
lastID int
idMtx sync.Mutex
)
var lastID int64
func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
lastID++
return lastID
return int(atomic.AddInt64(&lastID, 1))
}
// TickMsg is a message that is sent on every timer tick.
+3 -9
View File
@@ -2,22 +2,16 @@
package timer
import (
"sync"
"sync/atomic"
"time"
tea "github.com/charmbracelet/bubbletea"
)
var (
lastID int
idMtx sync.Mutex
)
var lastID int64
func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
lastID++
return lastID
return int(atomic.AddInt64(&lastID, 1))
}
// Authors note with regard to start and stop commands: