♻️ 启动后只执行一次

This commit is contained in:
BTMuli
2023-10-09 17:52:28 +08:00
parent 5712d4b7fc
commit faa1832c1e
2 changed files with 26 additions and 13 deletions

View File

@@ -3,16 +3,24 @@
use tauri::Manager; use tauri::Manager;
// 放一个常数,用来判断是否注册deep link // 放一个常数,用来判断应用是否初始化
static mut DEEP_LINK_REGISTERED: bool = false; static mut APP_INITIALIZED: bool = false;
#[tauri::command] #[tauri::command]
async fn register_deep_link(app_handle: tauri::AppHandle) { async fn init_app(app_handle: tauri::AppHandle) {
unsafe { unsafe {
if DEEP_LINK_REGISTERED { if APP_INITIALIZED {
return; return;
} }
} }
app_handle.emit_all("initApp", ()).unwrap();
unsafe {
APP_INITIALIZED = true;
}
}
#[tauri::command]
async fn register_deep_link(app_handle: tauri::AppHandle) {
tauri_plugin_deep_link::register( tauri_plugin_deep_link::register(
"teyvatguide", "teyvatguide",
move |request| { move |request| {
@@ -21,9 +29,6 @@ async fn register_deep_link(app_handle: tauri::AppHandle) {
}, },
) )
.unwrap(); .unwrap();
unsafe {
DEEP_LINK_REGISTERED = true;
}
} }
fn main() { fn main() {
@@ -48,7 +53,7 @@ fn main() {
} }
}) })
.plugin(tauri_plugin_sql::Builder::default().build()) .plugin(tauri_plugin_sql::Builder::default().build())
.invoke_handler(tauri::generate_handler![register_deep_link]) .invoke_handler(tauri::generate_handler![register_deep_link, init_app])
.setup(|_app| { .setup(|_app| {
let _window = _app.get_window("TeyvatGuide").unwrap(); let _window = _app.get_window("TeyvatGuide").unwrap();
#[cfg(debug_assertions)] // only include this code on debug builds #[cfg(debug_assertions)] // only include this code on debug builds

View File

@@ -37,12 +37,9 @@ onBeforeMount(async () => {
isMain.value = win.label === "TeyvatGuide"; isMain.value = win.label === "TeyvatGuide";
if (isMain.value) { if (isMain.value) {
const title = "Teyvat Guide v" + (await app.getVersion()) + " Beta"; const title = "Teyvat Guide v" + (await app.getVersion()) + " Beta";
await tauri.invoke("register_deep_link");
await getDeepLink();
await win.setTitle(title); await win.setTitle(title);
await emojiLoad(); await listenOnInit();
await checkAppLoad(); await tauri.invoke("init_app");
await checkUserLoad();
} }
}); });
@@ -63,6 +60,17 @@ async function listenOnTheme(): Promise<void> {
}); });
} }
// 启动后只执行一次的监听
async function listenOnInit(): Promise<void> {
await event.listen("initApp", async () => {
await tauri.invoke("register_deep_link");
await getDeepLink();
await emojiLoad();
await checkAppLoad();
await checkUserLoad();
});
}
async function emojiLoad(): Promise<void> { async function emojiLoad(): Promise<void> {
const res = await getEmojis(); const res = await getEmojis();
if ("retcode" in res) { if ("retcode" in res) {