♻️ 启动后只执行一次

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;
// 放一个常数,用来判断是否注册deep link
static mut DEEP_LINK_REGISTERED: bool = false;
// 放一个常数,用来判断应用是否初始化
static mut APP_INITIALIZED: bool = false;
#[tauri::command]
async fn register_deep_link(app_handle: tauri::AppHandle) {
async fn init_app(app_handle: tauri::AppHandle) {
unsafe {
if DEEP_LINK_REGISTERED {
if APP_INITIALIZED {
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(
"teyvatguide",
move |request| {
@@ -21,9 +29,6 @@ async fn register_deep_link(app_handle: tauri::AppHandle) {
},
)
.unwrap();
unsafe {
DEEP_LINK_REGISTERED = true;
}
}
fn main() {
@@ -48,7 +53,7 @@ fn main() {
}
})
.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| {
let _window = _app.get_window("TeyvatGuide").unwrap();
#[cfg(debug_assertions)] // only include this code on debug builds

View File

@@ -37,12 +37,9 @@ onBeforeMount(async () => {
isMain.value = win.label === "TeyvatGuide";
if (isMain.value) {
const title = "Teyvat Guide v" + (await app.getVersion()) + " Beta";
await tauri.invoke("register_deep_link");
await getDeepLink();
await win.setTitle(title);
await emojiLoad();
await checkAppLoad();
await checkUserLoad();
await listenOnInit();
await tauri.invoke("init_app");
}
});
@@ -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> {
const res = await getEmojis();
if ("retcode" in res) {