Update readme

pull/1860/head
Tony 8 months ago
parent 73f365d3f0
commit 2718722422
No known key found for this signature in database
GPG Key ID: 34BDD3EA27824956

@ -68,9 +68,9 @@ fn main() {
Afterwards all the plugin's APIs are available through the JavaScript guest bindings: Afterwards all the plugin's APIs are available through the JavaScript guest bindings:
```typescript ```typescript
import { Store } from '@tauri-apps/plugin-store' import { LazyStore } from '@tauri-apps/plugin-store'
const store = new Store('.settings.dat') const store = new LazyStore('.settings.dat')
await store.set('some-key', { value: 5 }) await store.set('some-key', { value: 5 })
@ -81,14 +81,11 @@ if (val) {
} else { } else {
console.log('val is null') console.log('val is null')
} }
// This manually saves the store.
await store.save()
``` ```
### Persisting Values ### Persisting Values
As seen above, values added to the store are not persisted between application loads unless the application is closed gracefully. Modifications mode to the store are automatically saved by defaut
You can manually save a store with: You can manually save a store with:
@ -108,60 +105,28 @@ await store.load()
You can also create `Store` instances directly in Rust: You can also create `Store` instances directly in Rust:
```rust ```rust
use tauri_plugin_store::StoreBuilder; use tauri_plugin_store::StoreExt;
use serde_json::json; use serde_json::json;
fn main() { fn main() {
tauri::Builder::default() tauri::Builder::default()
.plugin(tauri_plugin_store::Builder::default().build()) .plugin(tauri_plugin_store::Builder::default().build())
.setup(|app| { .setup(|app| {
let mut store = StoreBuilder::new("app_data.bin").build(app.handle().clone()); // This loads the store from disk
let store = app.store_builder("app_data.bin").build()?;
// Attempt to load the store, if it's saved already.
store.load().expect("Failed to load store from disk");
// Note that values must be serde_json::Value instances, // Note that values must be serde_json::Value instances,
// otherwise, they will not be compatible with the JavaScript bindings. // otherwise, they will not be compatible with the JavaScript bindings.
store.insert("a".to_string(), json!("b")); store.insert("a".to_string(), json!("b"));
// You can manually save the store after making changes.
// Otherwise, it will save upon graceful exit as described above.
store.save()
}) })
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }
``` ```
### Loading Gracefully
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.
For example, this would cause a panic if the store has not yet been created:
```rust
store.load().unwrap();
```
Rather than silently continuing like you may expect.
You should always handle the error appropriately rather than unwrapping, or you may experience unexpected app crashes:
```rust
store.load().expect("Failed to load store from disk");
```
### Frontend Interoperability ### Frontend Interoperability
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
```rust
use tauri::Wry;
use tauri_plugin_store::StoreExt;
let store = app.store_builder("app_data.bin").build();
store.insert("key", "value");
```
## Contributing ## Contributing

Loading…
Cancel
Save