refactor
This commit is contained in:
@@ -45,7 +45,7 @@ fn hash256sum(hash_input: String) -> String {
|
|||||||
// Example command using managed state
|
// Example command using managed state
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
#[specta::specta]
|
#[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);
|
store.add_key_val(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -25,10 +25,9 @@ fn main() {
|
|||||||
// Run the app
|
// Run the app
|
||||||
app.build(tauri::generate_context!())
|
app.build(tauri::generate_context!())
|
||||||
.expect("error while running tauri application")
|
.expect("error while running tauri application")
|
||||||
.run(|_, e| match e {
|
.run(|_, e| {
|
||||||
RunEvent::Ready => {
|
if let RunEvent::Ready = e {
|
||||||
println!("Window is ready");
|
println!("Window is ready");
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -4,6 +4,7 @@ use std::{collections::HashMap, sync::Mutex};
|
|||||||
|
|
||||||
use tauri::{Builder, Wry};
|
use tauri::{Builder, Wry};
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
pub struct Store {
|
pub struct Store {
|
||||||
store: Mutex<HashMap<String, String>>,
|
store: Mutex<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
@@ -15,18 +16,17 @@ impl Store {
|
|||||||
.insert(key, val);
|
.insert(key, val);
|
||||||
}
|
}
|
||||||
pub fn read_key(&self, key: &String) -> Option<String> {
|
pub fn read_key(&self, key: &String) -> Option<String> {
|
||||||
match self.store.lock().expect("cannot lock store").get(key) {
|
self.store
|
||||||
Some(s) => Some(s.to_string()),
|
.lock()
|
||||||
None => None,
|
.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.
|
// 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> {
|
pub fn register_managed_state(builder: Builder<Wry>) -> Builder<Wry> {
|
||||||
let store = Store {
|
let store = Store::default();
|
||||||
store: Mutex::from(HashMap::new()),
|
|
||||||
};
|
|
||||||
|
|
||||||
builder.manage(store)
|
builder.manage(store)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user