@@ -53,17 +53,36 @@ enum AutoSave {
53
53
Bool ( bool ) ,
54
54
}
55
55
56
- fn builder < R : Runtime > (
57
- app : AppHandle < R > ,
58
- store_state : State < ' _ , StoreState > ,
59
- path : PathBuf ,
56
+ # [ derive ( Serialize , Deserialize ) ]
57
+ # [ serde ( rename_all = "camelCase" ) ]
58
+ struct LoadStoreOptions {
59
+ defaults : Option < HashMap < String , JsonValue > > ,
60
60
auto_save : Option < AutoSave > ,
61
61
serialize_fn_name : Option < String > ,
62
62
deserialize_fn_name : Option < String > ,
63
+ #[ serde( default ) ]
63
64
create_new : bool ,
65
+ #[ serde( default ) ]
66
+ override_defaults : bool ,
67
+ }
68
+
69
+ fn builder < R : Runtime > (
70
+ app : AppHandle < R > ,
71
+ store_state : State < ' _ , StoreState > ,
72
+ path : PathBuf ,
73
+ options : Option < LoadStoreOptions > ,
64
74
) -> Result < StoreBuilder < R > > {
65
75
let mut builder = app. store_builder ( path) ;
66
- if let Some ( auto_save) = auto_save {
76
+
77
+ let Some ( options) = options else {
78
+ return Ok ( builder) ;
79
+ } ;
80
+
81
+ if let Some ( defaults) = options. defaults {
82
+ builder = builder. defaults ( defaults) ;
83
+ }
84
+
85
+ if let Some ( auto_save) = options. auto_save {
67
86
match auto_save {
68
87
AutoSave :: DebounceDuration ( duration) => {
69
88
builder = builder. auto_save ( Duration :: from_millis ( duration) ) ;
@@ -75,26 +94,30 @@ fn builder<R: Runtime>(
75
94
}
76
95
}
77
96
78
- if let Some ( serialize_fn_name) = serialize_fn_name {
97
+ if let Some ( serialize_fn_name) = options . serialize_fn_name {
79
98
let serialize_fn = store_state
80
99
. serialize_fns
81
100
. get ( & serialize_fn_name)
82
101
. ok_or_else ( || crate :: Error :: SerializeFunctionNotFound ( serialize_fn_name) ) ?;
83
102
builder = builder. serialize ( * serialize_fn) ;
84
103
}
85
104
86
- if let Some ( deserialize_fn_name) = deserialize_fn_name {
105
+ if let Some ( deserialize_fn_name) = options . deserialize_fn_name {
87
106
let deserialize_fn = store_state
88
107
. deserialize_fns
89
108
. get ( & deserialize_fn_name)
90
109
. ok_or_else ( || crate :: Error :: DeserializeFunctionNotFound ( deserialize_fn_name) ) ?;
91
110
builder = builder. deserialize ( * deserialize_fn) ;
92
111
}
93
112
94
- if create_new {
113
+ if options . create_new {
95
114
builder = builder. create_new ( ) ;
96
115
}
97
116
117
+ if options. override_defaults {
118
+ builder = builder. override_defaults ( ) ;
119
+ }
120
+
98
121
Ok ( builder)
99
122
}
100
123
@@ -103,20 +126,9 @@ async fn load<R: Runtime>(
103
126
app : AppHandle < R > ,
104
127
store_state : State < ' _ , StoreState > ,
105
128
path : PathBuf ,
106
- auto_save : Option < AutoSave > ,
107
- serialize_fn_name : Option < String > ,
108
- deserialize_fn_name : Option < String > ,
109
- create_new : Option < bool > ,
129
+ options : Option < LoadStoreOptions > ,
110
130
) -> Result < ResourceId > {
111
- let builder = builder (
112
- app,
113
- store_state,
114
- path,
115
- auto_save,
116
- serialize_fn_name,
117
- deserialize_fn_name,
118
- create_new. unwrap_or_default ( ) ,
119
- ) ?;
131
+ let builder = builder ( app, store_state, path, options) ?;
120
132
let ( _, rid) = builder. build_inner ( ) ?;
121
133
Ok ( rid)
122
134
}
@@ -209,9 +221,17 @@ async fn length<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<usize>
209
221
}
210
222
211
223
#[ tauri:: command]
212
- async fn reload < R : Runtime > ( app : AppHandle < R > , rid : ResourceId ) -> Result < ( ) > {
224
+ async fn reload < R : Runtime > (
225
+ app : AppHandle < R > ,
226
+ rid : ResourceId ,
227
+ ignore_defaults : Option < bool > ,
228
+ ) -> Result < ( ) > {
213
229
let store = app. resources_table ( ) . get :: < Store < R > > ( rid) ?;
214
- store. reload ( )
230
+ if ignore_defaults. unwrap_or_default ( ) {
231
+ store. reload_ignore_defaults ( )
232
+ } else {
233
+ store. reload ( )
234
+ }
215
235
}
216
236
217
237
#[ tauri:: command]
0 commit comments