You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* refactor(store): more reworks
* Enable auto save by default
* Store to resource table by default
* Remove share store
* Clean up
* Add close store
* Add store function
* Add lazy store
* Add init to lazy store
* refresh cache in example
* Add get-or-create-store
* Revert "Add get-or-create-store"
This reverts commit 7ffd769.
* try get first
* Docs
* Use absolute path for store
* more docs
* Allow js to use pre-stored (de)serialize functions
* Fix js get and close store
* Show case how to use pretty json
* Update readme
* Use store instead of `store_builder` in example
* Build
* Fix example
* More docs for StoreBuilder::build
* Add default (de)serialize fn
* Use pretty json by default
* Use `undefined` for empty value in get
* Change files
* Differentiate json null from no value for events
* Add create or existing
* Build
* Rename inner store's inset method to set
* Update readme
* Apply suggestions from code review
* Use close instead
* Update breaking changes
* Return result in resolve_store_path
* Change to close_resource and take &self
* Clean up
* Apply suggestions from code review
Co-authored-by: Amr Bashir <[email protected]>
* Remove unused pub(crate)
* Update change file
* Expose resolve_store_path
* Remove with_store
* Remove StoreInner from pub and expose is_empty
* Fix wrong jsdoc param
* Update readme
* rename createOrExistingStore to createOrLoad
* make api consistent with the JS implementation, add examples
* fmt
* reintroduce "get existing store" behavior for create_or_load
* rename createOrLoad to newOrExisting
* keep store lock throughout whole new_or_existing_inner
* Remove load
* Missed if load
* Don't make StoreState public
* Remove R: Runtime from Builder
* rename newOrExisting to load, load to reload
* update docs
* rename missing reload fn
* rename builder fn to build()
* fix default permission
* Fix description and create_new logic
* Clippy
* Update docs
* Update docs
* remove create_store command
* remove close_store command since we extend from Resource
* Revert "remove close_store command since we extend from Resource"
This reverts commit 4a29fc8.
* Reapply "remove close_store command since we extend from Resource"
This reverts commit 70a1830.
---------
Co-authored-by: Amr Bashir <[email protected]>
Co-authored-by: Lucas Nogueira <[email protected]>
Copy file name to clipboardExpand all lines: plugins/store/README.md
+17-42Lines changed: 17 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,7 @@ Afterwards all the plugin's APIs are available through the JavaScript guest bind
70
70
```typescript
71
71
import { Store } from'@tauri-apps/plugin-store'
72
72
73
-
const store =newStore('.settings.dat')
73
+
const store =awaitStore.load('settings.json')
74
74
75
75
awaitstore.set('some-key', { value: 5 })
76
76
@@ -81,14 +81,11 @@ if (val) {
81
81
} else {
82
82
console.log('val is null')
83
83
}
84
-
85
-
// This manually saves the store.
86
-
awaitstore.save()
87
84
```
88
85
89
86
### Persisting Values
90
87
91
-
As seen above, values added to the store are not persisted between application loads unless the application is closed gracefully.
88
+
Modifications made to the store are automatically saved by default
92
89
93
90
You can manually save a store with:
94
91
@@ -103,65 +100,43 @@ However, you can also load them manually later like so:
103
100
awaitstore.load()
104
101
```
105
102
103
+
### LazyStore
104
+
105
+
There's also a high level API `LazyStore` which only loads the store on first access, note that the options will be ignored if a `Store` with that path has already been created
// Attempt to load the store, if it's saved already.
121
-
store.load().expect("Failed to load store from disk");
125
+
// This loads the store from disk
126
+
letstore=app.store("app_data.json")?;
122
127
123
128
// Note that values must be serde_json::Value instances,
124
129
// otherwise, they will not be compatible with the JavaScript bindings.
125
-
store.insert("a".to_string(), json!("b"));
126
-
127
-
// You can manually save the store after making changes.
128
-
// Otherwise, it will save upon graceful exit as described above.
129
-
store.save()
130
+
store.set("a".to_string(), json!("b"));
130
131
})
131
132
.run(tauri::generate_context!())
132
133
.expect("error while running tauri application");
133
134
}
134
135
```
135
136
136
-
### Loading Gracefully
137
-
138
-
If you call `load` on a `Store` that hasn't yet been written to the disk, it will return an error. You must handle this error if you want to gracefully continue and use the default store until you save it to the disk. The example above shows how to do this.
139
-
140
-
For example, this would cause a panic if the store has not yet been created:
141
-
142
-
```rust
143
-
store.load().unwrap();
144
-
```
145
-
146
-
Rather than silently continuing like you may expect.
147
-
148
-
You should always handle the error appropriately rather than unwrapping, or you may experience unexpected app crashes:
149
-
150
-
```rust
151
-
store.load().expect("Failed to load store from disk");
152
-
```
153
-
154
137
### Frontend Interoperability
155
138
156
-
As you may have noticed, the `Store` crated above isn't accessible to the frontend. To interoperate with stores created by JavaScript use the exported `with_store` method:
The store created from both Rust side and JavaScript side are stored in the app's resource table and can be accessed by both sides, you can access it by using the same path, with `getStore` and `LazyStore` in the JavaScript side and `get_store` and `store` in the Rust side
0 commit comments