mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-19 10:29:29 +08:00
Re-added Store.Set() using deep copy
This commit is contained in:
parent
0e24f75753
commit
ceaacc7ba9
@ -71,19 +71,16 @@ func (p *StoreProvider) New(name string, defaultValue interface{}) *Store {
|
|||||||
golog.Fatal("Cannot initialise a store with nil")
|
golog.Fatal("Cannot initialise a store with nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
dataCopy := deepcopy.Copy(defaultValue)
|
|
||||||
dataType := reflect.TypeOf(dataCopy)
|
|
||||||
|
|
||||||
result := Store{
|
result := Store{
|
||||||
name: name,
|
name: name,
|
||||||
runtime: p.runtime,
|
runtime: p.runtime,
|
||||||
data: reflect.ValueOf(dataCopy),
|
|
||||||
dataType: dataType,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup the sync listener
|
// Setup the sync listener
|
||||||
result.setupListener()
|
result.setupListener()
|
||||||
|
|
||||||
|
result.Set(defaultValue)
|
||||||
|
|
||||||
return &result
|
return &result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,13 +212,20 @@ 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 {
|
||||||
|
|
||||||
|
if data == nil {
|
||||||
|
return fmt.Errorf("cannot set store to nil")
|
||||||
|
}
|
||||||
|
|
||||||
s.lock()
|
s.lock()
|
||||||
|
|
||||||
if data != nil {
|
dataCopy := deepcopy.Copy(data)
|
||||||
inType := reflect.TypeOf(data)
|
|
||||||
|
if dataCopy != nil {
|
||||||
|
inType := reflect.TypeOf(dataCopy)
|
||||||
|
|
||||||
if inType != s.dataType && s.data.IsValid() {
|
if inType != s.dataType && s.data.IsValid() {
|
||||||
s.unlock()
|
s.unlock()
|
||||||
@ -230,11 +234,11 @@ func (s *Store) set(data interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s.dataType == nil {
|
if s.dataType == nil {
|
||||||
s.dataType = reflect.TypeOf(data)
|
s.dataType = reflect.TypeOf(dataCopy)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save data
|
// Save data
|
||||||
s.data = reflect.ValueOf(data)
|
s.data = reflect.ValueOf(dataCopy)
|
||||||
|
|
||||||
s.unlock()
|
s.unlock()
|
||||||
|
|
||||||
@ -347,7 +351,7 @@ func (s *Store) Update(updater interface{}) {
|
|||||||
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
|
||||||
|
@ -14,6 +14,33 @@ import (
|
|||||||
is2 "github.com/matryer/is"
|
is2 "github.com/matryer/is"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestStoreProvider_NewWithNilDefault(t *testing.T) {
|
||||||
|
is := is2.New(t)
|
||||||
|
|
||||||
|
defaultLogger := logger.NewDefaultLogger()
|
||||||
|
testLogger := internallogger.New(defaultLogger)
|
||||||
|
//testLogger.SetLogLevel(logger.TRACE)
|
||||||
|
serviceBus := servicebus.New(testLogger)
|
||||||
|
err := serviceBus.Start()
|
||||||
|
is.NoErr(err)
|
||||||
|
defer serviceBus.Stop()
|
||||||
|
|
||||||
|
testRuntime := New(serviceBus)
|
||||||
|
storeProvider := newStore(testRuntime)
|
||||||
|
|
||||||
|
testStore := storeProvider.New("test", 0)
|
||||||
|
|
||||||
|
// You should be able to write a new value into a
|
||||||
|
// store initialised with nil
|
||||||
|
err = testStore.Set(100)
|
||||||
|
is.NoErr(err)
|
||||||
|
|
||||||
|
// You shouldn't be able to write different types to the
|
||||||
|
// store
|
||||||
|
err = testStore.Set(false)
|
||||||
|
is.True(err != nil)
|
||||||
|
}
|
||||||
|
|
||||||
func TestStoreProvider_NewWithScalarDefault(t *testing.T) {
|
func TestStoreProvider_NewWithScalarDefault(t *testing.T) {
|
||||||
is := is2.New(t)
|
is := is2.New(t)
|
||||||
|
|
||||||
@ -58,9 +85,8 @@ func TestStoreProvider_NewWithStructDefault(t *testing.T) {
|
|||||||
|
|
||||||
testStore := storeProvider.New("test", testValue)
|
testStore := storeProvider.New("test", testValue)
|
||||||
|
|
||||||
testStore.Update(func(current *TestValue) *TestValue {
|
err = testStore.Set(testValue)
|
||||||
return testValue
|
is.NoErr(err)
|
||||||
})
|
|
||||||
testStore.resync()
|
testStore.resync()
|
||||||
value := testStore.Get()
|
value := testStore.Get()
|
||||||
is.Equal(value, testValue)
|
is.Equal(value, testValue)
|
||||||
@ -69,9 +95,8 @@ func TestStoreProvider_NewWithStructDefault(t *testing.T) {
|
|||||||
testValue = &TestValue{
|
testValue = &TestValue{
|
||||||
Name: "there",
|
Name: "there",
|
||||||
}
|
}
|
||||||
testStore.Update(func(current *TestValue) *TestValue {
|
err = testStore.Set(testValue)
|
||||||
return testValue
|
is.NoErr(err)
|
||||||
})
|
|
||||||
testStore.resync()
|
testStore.resync()
|
||||||
value = testStore.Get()
|
value = testStore.Get()
|
||||||
is.Equal(value, testValue)
|
is.Equal(value, testValue)
|
||||||
@ -128,9 +153,8 @@ func TestStoreProvider_RapidReadWrite(t *testing.T) {
|
|||||||
wg.Done()
|
wg.Done()
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
store.Update(func(current int) int {
|
err := store.Set(rand.Int())
|
||||||
return rand.Int()
|
is.NoErr(err)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(testStore, ctx, writerCount)
|
}(testStore, ctx, writerCount)
|
||||||
|
Loading…
Reference in New Issue
Block a user