Skip to content

Commit

Permalink
Another cleanup solution for better comparison on Go 1.19
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Jan 15, 2025
1 parent 12868be commit 20edc98
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 53 deletions.
24 changes: 24 additions & 0 deletions data/binding/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package binding

import (
"sync/atomic"

"fyne.io/fyne/v2"
)

func newBaseItem[T bool | float64 | int | rune | string]() *baseItem[T] {
Expand Down Expand Up @@ -105,3 +107,25 @@ func (b *prefBoundBase[T]) checkForChange() {
}
b.trigger()
}

type genericItem[T any] interface {
DataItem
Get() (T, error)
Set(T) error
}

func lookupExistingBinding[T any](key string, p fyne.Preferences) (genericItem[T], bool) {
binds := prefBinds.getBindings(p)
if binds == nil {
return nil, false
}

if listen, ok := binds.Load(key); listen != nil && ok {
if l, ok := listen.(genericItem[T]); ok {
return l, ok
}
fyne.LogError(keyTypeMismatchError+key, nil)
}

return nil, false
}
75 changes: 24 additions & 51 deletions data/binding/preference.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ type prefBoundBool struct {
//
// Since: 2.0
func BindPreferenceBool(key string, p fyne.Preferences) Bool {
if found, ok := lookupExistingBinding(key, p); ok {
if l, ok := found.(Bool); ok {
return l
}
fyne.LogError(keyTypeMismatchError+key, nil)
if found, ok := lookupExistingBinding[bool](key, p); ok {
return found
}

listen := &prefBoundBool{}
setupPrefItem(listen, key, p)
listen.setKey(key)
listen.replaceProvider(p)
binds := prefBinds.ensurePreferencesAttached(p)
binds.Store(key, listen)
return listen
}

Expand All @@ -39,15 +39,15 @@ type prefBoundFloat struct {
//
// Since: 2.0
func BindPreferenceFloat(key string, p fyne.Preferences) Float {
if found, ok := lookupExistingBinding(key, p); ok {
if l, ok := found.(Float); ok {
return l
}
fyne.LogError(keyTypeMismatchError+key, nil)
if found, ok := lookupExistingBinding[float64](key, p); ok {
return found
}

listen := &prefBoundFloat{}
setupPrefItem(listen, key, p)
listen.setKey(key)
listen.replaceProvider(p)
binds := prefBinds.ensurePreferencesAttached(p)
binds.Store(key, listen)
return listen
}

Expand All @@ -65,15 +65,15 @@ type prefBoundInt struct {
//
// Since: 2.0
func BindPreferenceInt(key string, p fyne.Preferences) Int {
if found, ok := lookupExistingBinding(key, p); ok {
if l, ok := found.(Int); ok {
return l
}
fyne.LogError(keyTypeMismatchError+key, nil)
if found, ok := lookupExistingBinding[int](key, p); ok {
return found
}

listen := &prefBoundInt{}
setupPrefItem(listen, key, p)
listen.setKey(key)
listen.replaceProvider(p)
binds := prefBinds.ensurePreferencesAttached(p)
binds.Store(key, listen)
return listen
}

Expand All @@ -91,46 +91,19 @@ type prefBoundString struct {
//
// Since: 2.0
func BindPreferenceString(key string, p fyne.Preferences) String {
if found, ok := lookupExistingBinding(key, p); ok {
if l, ok := found.(String); ok {
return l
}
fyne.LogError(keyTypeMismatchError+key, nil)
if found, ok := lookupExistingBinding[string](key, p); ok {
return found
}

listen := &prefBoundString{}
setupPrefItem(listen, key, p)
listen.setKey(key)
listen.replaceProvider(p)
binds := prefBinds.ensurePreferencesAttached(p)
binds.Store(key, listen)
return listen
}

func (b *prefBoundString) replaceProvider(p fyne.Preferences) {
b.get = p.String
b.set = p.SetString
}

func lookupExistingBinding(key string, p fyne.Preferences) (preferenceItem, bool) {
binds := prefBinds.getBindings(p)
if binds == nil {
return nil, false
}

if listen, ok := binds.Load(key); listen != nil && ok {
return listen, true
}

return nil, false
}

type prefBoundItem interface {
preferenceItem
setKey(string)
replaceProvider(fyne.Preferences)
}

func setupPrefItem(listen prefBoundItem, key string, p fyne.Preferences) preferenceItem {
listen.setKey(key)
listen.replaceProvider(p)
binds := prefBinds.ensurePreferencesAttached(p)
binds.Store(key, listen)
return listen
}
5 changes: 3 additions & 2 deletions data/binding/preference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ func TestBindPreference_DataRace(t *testing.T) {
a := test.NewApp()
p := a.Preferences()
key := "test-key"
const n = 100

const n = 100
var wg sync.WaitGroup
wg.Add(n)

binds := make([]Int, n)
for i := 0; i < n; i++ {
wg.Add(1)
go func(index int) {
bind := BindPreferenceInt(key, p)
binds[index] = bind
Expand Down

0 comments on commit 20edc98

Please sign in to comment.