Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:fyne-io/fyne into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Mar 4, 2021
2 parents 40cdc9c + cf6a318 commit c253ac6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion app/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"sync"
"time"

"fyne.io/fyne/v2"
Expand All @@ -12,6 +13,8 @@ import (

type preferences struct {
*internal.InMemoryPreferences

prefLock sync.RWMutex
ignoreChange bool

app *fyneApp
Expand All @@ -23,7 +26,9 @@ var _ fyne.Preferences = (*preferences)(nil)
func (p *preferences) resetIgnore() {
go func() {
time.Sleep(time.Millisecond * 100) // writes are not always atomic. 10ms worked, 100 is safer.
p.prefLock.Lock()
p.ignoreChange = false
p.prefLock.Unlock()
}()
}

Expand All @@ -32,7 +37,9 @@ func (p *preferences) save() error {
}

func (p *preferences) saveToFile(path string) error {
p.prefLock.Lock()
p.ignoreChange = true
p.prefLock.Unlock()
defer p.resetIgnore()
err := os.MkdirAll(filepath.Dir(path), 0700)
if err != nil { // this is not an exists error according to docs
Expand Down Expand Up @@ -99,7 +106,10 @@ func newPreferences(app *fyneApp) *preferences {
}

p.AddChangeListener(func() {
if p.ignoreChange { // callback after loading, no need to save
p.prefLock.RLock()
shouldIgnoreChange := p.ignoreChange
p.prefLock.RUnlock()
if shouldIgnoreChange { // callback after loading, no need to save
return
}

Expand Down
5 changes: 4 additions & 1 deletion app/preferences_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ func (a *fyneApp) storageRoot() string {

func (p *preferences) watch() {
watchFile(p.storagePath(), func() {
if p.ignoreChange {
p.prefLock.RLock()
shouldIgnoreChange := p.ignoreChange
p.prefLock.RUnlock()
if shouldIgnoreChange {
return
}

Expand Down

0 comments on commit c253ac6

Please sign in to comment.