This commit is contained in:
Fractal-Tess
2023-05-19 02:29:40 +03:00
parent 038b899f3d
commit 9436a2633d
3 changed files with 10 additions and 11 deletions

View File

@@ -45,7 +45,7 @@ fn hash256sum(hash_input: String) -> String {
// Example command using managed state
#[tauri::command]
#[specta::specta]
fn store_set_key(key: String, value: String, store: State<Store>) -> () {
fn store_set_key(key: String, value: String, store: State<Store>) {
store.add_key_val(key, value);
}

View File

@@ -25,10 +25,9 @@ fn main() {
// Run the app
app.build(tauri::generate_context!())
.expect("error while running tauri application")
.run(|_, e| match e {
RunEvent::Ready => {
.run(|_, e| {
if let RunEvent::Ready = e {
println!("Window is ready");
}
_ => {}
})
}

View File

@@ -4,6 +4,7 @@ use std::{collections::HashMap, sync::Mutex};
use tauri::{Builder, Wry};
#[derive(Default)]
pub struct Store {
store: Mutex<HashMap<String, String>>,
}
@@ -15,18 +16,17 @@ impl Store {
.insert(key, val);
}
pub fn read_key(&self, key: &String) -> Option<String> {
match self.store.lock().expect("cannot lock store").get(key) {
Some(s) => Some(s.to_string()),
None => None,
}
self.store
.lock()
.expect("cannot lock store")
.get(key)
.map(|val| val.to_string())
}
}
// Exports a function for the tauri app instance to use and register all commands defined as frontend IPC command handlers.
pub fn register_managed_state(builder: Builder<Wry>) -> Builder<Wry> {
let store = Store {
store: Mutex::from(HashMap::new()),
};
let store = Store::default();
builder.manage(store)
}