5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-19 10:29:29 +08:00

Unexport set

This commit is contained in:
Lea Anthony 2021-01-30 20:47:10 +11:00
parent cfa40b797f
commit 82b9deeee4
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405

View File

@ -79,6 +79,14 @@ func (p *StoreProvider) New(name string, defaultValue interface{}) *Store {
return &result return &result
} }
func (s *Store) lock() {
s.mux.Lock()
}
func (s *Store) unlock() {
s.mux.Unlock()
}
// OnError takes a function that will be called // OnError takes a function that will be called
// whenever an error occurs // whenever an error occurs
func (s *Store) OnError(callback func(error)) { func (s *Store) OnError(callback func(error)) {
@ -105,7 +113,7 @@ func (s *Store) processUpdatedData(data string) error {
} }
// Lock mutex for writing // Lock mutex for writing
s.mux.Lock() s.lock()
// Handle nulls // Handle nulls
if newData == nil { if newData == nil {
@ -116,7 +124,7 @@ func (s *Store) processUpdatedData(data string) error {
} }
// Unlock mutex // Unlock mutex
s.mux.Unlock() s.unlock()
return nil return nil
} }
@ -154,8 +162,8 @@ func (s *Store) setupListener() {
func (s *Store) resync() { func (s *Store) resync() {
// Lock // Lock
s.mux.Lock() s.lock()
defer s.mux.Unlock() defer s.unlock()
var result string var result string
@ -164,7 +172,6 @@ func (s *Store) resync() {
if err != nil { if err != nil {
if s.errorHandler != nil { if s.errorHandler != nil {
s.errorHandler(err) s.errorHandler(err)
s.mux.Unlock()
return return
} }
} }
@ -187,7 +194,9 @@ func (s *Store) notify() {
for _, callback := range s.callbacks { for _, callback := range s.callbacks {
// Build args // Build args
s.lock()
args := []reflect.Value{s.data} args := []reflect.Value{s.data}
s.unlock()
if s.notifySynchronously { if s.notifySynchronously {
callback.Call(args) callback.Call(args)
@ -198,16 +207,16 @@ func (s *Store) notify() {
} }
} }
// Set will update the data held by the store // set will update the data held by the store
// and notify listeners of the change // and notify listeners of the change
func (s *Store) Set(data interface{}) error { func (s *Store) set(data interface{}) error {
s.mux.Lock() s.lock()
if data != nil { if data != nil {
inType := reflect.TypeOf(data) inType := reflect.TypeOf(data)
if inType != s.dataType && s.data.IsValid() { if inType != s.dataType && s.data.IsValid() {
s.mux.Unlock() s.unlock()
return fmt.Errorf("invalid data given in Store.Set(). Expected %s, got %s", s.dataType.String(), inType.String()) return fmt.Errorf("invalid data given in Store.Set(). Expected %s, got %s", s.dataType.String(), inType.String())
} }
} }
@ -219,7 +228,7 @@ func (s *Store) Set(data interface{}) error {
// Save data // Save data
s.data = reflect.ValueOf(data) s.data = reflect.ValueOf(data)
s.mux.Unlock() s.unlock()
// Resync with subscribers // Resync with subscribers
s.resync() s.resync()
@ -270,9 +279,9 @@ func (s *Store) Subscribe(callback interface{}) {
callbackFunc := reflect.ValueOf(callback) callbackFunc := reflect.ValueOf(callback)
s.mux.Lock() s.lock()
s.callbacks = append(s.callbacks, callbackFunc) s.callbacks = append(s.callbacks, callbackFunc)
s.mux.Unlock() s.unlock()
} }
// updaterCheck ensures the given function to Update() is // updaterCheck ensures the given function to Update() is
@ -322,21 +331,21 @@ func (s *Store) Update(updater interface{}) {
} }
// Build args // Build args
s.mux.Lock() s.lock()
args := []reflect.Value{s.data} args := []reflect.Value{s.data}
s.mux.Unlock() s.unlock()
// Make call // Make call
results := reflect.ValueOf(updater).Call(args) results := reflect.ValueOf(updater).Call(args)
// We will only have 1 result. Set the store to it // We will only have 1 result. Set the store to it
s.Set(results[0].Interface()) s.set(results[0].Interface())
} }
// Get returns the value of the data that's kept in the current state / Store // Get returns the value of the data that's kept in the current state / Store
func (s *Store) Get() interface{} { func (s *Store) Get() interface{} {
s.mux.Lock() s.lock()
defer s.mux.Unlock() defer s.unlock()
if !s.data.IsValid() { if !s.data.IsValid() {
return nil return nil