mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-26 21:41:48 +02:00
feat(store): expose Store to Rust code (#108)
* feat(store): expose Store to Rust code in a meaningful way * manually implement Default to improve generic inference * update readme * rename method * make error Send and Sync * make `use_store` more ergonomic * export JsonValue * fix readme example * fmt --------- Co-authored-by: FabianLars <fabianlars@fabianlars.de>
This commit is contained in:
co-authored by
FabianLars
parent
6d7b985b46
commit
d4223f6fd2
@@ -61,6 +61,39 @@ const val = await store.get("some-key");
|
||||
assert(val, { value: 5 });
|
||||
```
|
||||
|
||||
## Usage from Rust
|
||||
|
||||
You can also access Stores from Rust, you can create new stores:
|
||||
|
||||
```rust
|
||||
use tauri_plugin_store::store::StoreBuilder;
|
||||
use serde_json::json;
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_store::Builder::default().build())
|
||||
.setup(|app| {
|
||||
let mut store = StoreBuilder::new(app.handle(), "path/to/store.bin".parse()?).build();
|
||||
|
||||
store.insert("a".to_string(), json!("b")) // note that values must be serd_json::Value to be compatible with JS
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
```
|
||||
|
||||
As you may have noticed, the Store crated above isn't accessible to the frontend. To interoperate with stores created by JS use the exported `with_store` method:
|
||||
|
||||
```rust
|
||||
use tauri::Wry;
|
||||
use tauri_plugin_store::with_store;
|
||||
|
||||
let stores = app.state::<StoreCollection<Wry>>();
|
||||
let path = PathBuf::from("path/to/the/storefile");
|
||||
|
||||
with_store(app_handle, stores, path, |store| store.set("a".to_string(), json!("b")))
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
PRs accepted. Please make sure to read the Contributing Guide before making a pull request.
|
||||
|
||||
Reference in New Issue
Block a user