store config in database, macro for state access

This commit is contained in:
Joseph Montanaro
2022-12-22 16:36:32 -08:00
parent 398916fe10
commit 61d674199f
10 changed files with 144 additions and 38 deletions

View File

@ -3,7 +3,8 @@
windows_subsystem = "windows"
)]
use tauri::Manager;
use tauri::{AppHandle, Manager};
use once_cell::sync::OnceCell;
mod config;
mod errors;
@ -16,6 +17,8 @@ mod tray;
use state::AppState;
pub static APP: OnceCell<AppHandle> = OnceCell::new();
fn main() {
let initial_state = match state::AppState::new() {
Ok(state) => state,
@ -31,8 +34,10 @@ fn main() {
ipc::respond,
ipc::get_session_status,
ipc::save_credentials,
ipc::get_config,
])
.setup(|app| {
APP.set(app.handle()).unwrap();
let state = app.state::<AppState>();
let config = state.config.read().unwrap();
let addr = std::net::SocketAddrV4::new(config.listen_addr, config.listen_port);
@ -51,4 +56,38 @@ fn main() {
}
_ => ()
})
}
}
macro_rules! get_state {
($prop:ident as $name:ident) => {
use tauri::Manager;
let app = crate::APP.get().unwrap(); // as long as the app is running, this is fine
let state = app.state::<crate::state::AppState>();
let $name = state.$prop.read().unwrap(); // only panics if another thread has already panicked
};
(config.$prop:ident as $name:ident) => {
use tauri::Manager;
let app = crate::APP.get().unwrap();
let state = app.state::<crate::state::AppState>();
let config = state.config.read().unwrap();
let $name = config.$prop;
};
(mut $prop:ident as $name:ident) => {
use tauri::Manager;
let app = crate::APP.get().unwrap();
let state = app.state::<crate::state::AppState>();
let $name = state.$prop.write().unwrap();
};
(mut config.$prop:ident as $name:ident) => {
use tauri::Manager;
let app = crate::APP.get().unwrap();
let state = app.state::<crate::state::AppState>();
let config = state.config.write().unwrap();
let $name = config.$prop;
}
}
pub(crate) use get_state;