mirror of
https://github.com/taigrr/wails.git
synced 2026-04-02 05:08:54 -07:00
Add mutex to store
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Options defines the optional data that may be used
|
||||
@@ -48,6 +49,9 @@ type Store struct {
|
||||
runtime *Runtime
|
||||
notifySynchronously bool
|
||||
|
||||
// Lock
|
||||
mux sync.Mutex
|
||||
|
||||
// Error handler
|
||||
errorHandler func(error)
|
||||
}
|
||||
@@ -95,6 +99,9 @@ func (s *Store) processUpdatedData(data string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Lock mutex for writing
|
||||
s.mux.Lock()
|
||||
|
||||
// Handle nulls
|
||||
if newData == nil {
|
||||
s.data = reflect.Zero(s.dataType)
|
||||
@@ -103,6 +110,9 @@ func (s *Store) processUpdatedData(data string) error {
|
||||
s.data = reflect.ValueOf(newData).Elem()
|
||||
}
|
||||
|
||||
// Unlock mutex
|
||||
s.mux.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -153,7 +163,9 @@ func (s *Store) Set(data interface{}) error {
|
||||
}
|
||||
|
||||
// Save data
|
||||
s.mux.Lock()
|
||||
s.data = reflect.ValueOf(data)
|
||||
s.mux.Unlock()
|
||||
|
||||
// Stringify data
|
||||
newdata, err := json.Marshal(data)
|
||||
|
||||
Reference in New Issue
Block a user