Add mutex to store

This commit is contained in:
Lea Anthony
2020-09-04 20:08:31 +10:00
parent 0474f15c05
commit d49b146eaa

View File

@@ -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)