Skip to content

Commit efaaf73

Browse files
SteveLauCayangweb
andauthored
fix: settings window rendering/loading issue (#889)
This commit fixes(I guess?) the issue that the Settings window may not be rendered or loaded, you will see that the whole window is gray in that case. Background, aka, why this issue exists ============================================================= In commit [1], we wrapped all the backend setup routines in a tauri command, so that frontend code can call it before invoking any other backend interfaces to guarantee that these interfaces won't be called until the data/state they rely on is ready. The implementation in [1] had an issue that it didn't work with window reloading. To fix this issue, we made another commit [2]. Commit [2] fixed the refresh issue, but it also caused the settings window issue that this commit tries to fix. The backend setup tauri command needs a state to track whether the setup has completed. In the previous implementation, this was done in the frontend. In this commit, it is moved to the backend. Why didn't you guys move that state to backend in previous commits, e.g., commit [2]? ============================================================= We tried, but failed. In the previous tries, the backend would send an event to the frontend, but the frontend couldn't receive it, for reasons we still don’t understand. And this weird issue still exists, we just happen to find a way to work around it. [1]: f93c527 [2]: 993da9a Co-authored-by: ayang <473033518@qq.com>
1 parent 86540ad commit efaaf73

File tree

4 files changed

+22
-37
lines changed

4 files changed

+22
-37
lines changed

docs/content.en/docs/release-notes/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Information about release notes of Coco App is provided here.
3434
- fix: set up hotkey on main thread or Windows will complain #879
3535
- fix: resolve deeplink login issue #881
3636
- fix: use kill_on_drop() to avoid zombie proc in error case #887
37+
- fix: settings window rendering/loading issue 889
3738

3839
### ✈️ Improvements
3940

src-tauri/src/setup/mod.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use crate::GLOBAL_TAURI_APP_HANDLE;
22
use crate::autostart;
33
use crate::common::register::SearchSourceRegistry;
44
use crate::util::app_lang::update_app_lang;
5-
use std::sync::OnceLock;
5+
use std::sync::atomic::AtomicBool;
6+
use std::sync::atomic::Ordering;
67
use tauri::{AppHandle, Manager, WebviewWindow};
78

89
#[cfg(target_os = "macos")]
@@ -41,9 +42,11 @@ pub fn default(
4142
);
4243
}
4344

44-
/// Use this variable to track if tauri command `backend_setup()` gets called
45-
/// by the frontend.
46-
pub(super) static BACKEND_SETUP_FUNC_INVOKED: OnceLock<()> = OnceLock::new();
45+
/// Indicates if the setup job is completed.
46+
static BACKEND_SETUP_COMPLETED: AtomicBool = AtomicBool::new(false);
47+
/// The function `backup_setup()` may be called concurrently, use this lock to
48+
/// synchronize that only 1 async task can do the actual setup job.
49+
static MUTEX_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
4750

4851
/// This function includes the setup job that has to be coordinated with the
4952
/// frontend, or the App will panic due to races[1]. The way we coordinate is to
@@ -60,9 +63,17 @@ pub(super) static BACKEND_SETUP_FUNC_INVOKED: OnceLock<()> = OnceLock::new();
6063
/// called. If the frontend code invokes `list_extensions()` before `init_extension()`
6164
/// gets executed, we get a panic.
6265
#[tauri::command]
63-
#[function_name::named]
6466
pub(crate) async fn backend_setup(tauri_app_handle: AppHandle, app_lang: String) {
65-
if BACKEND_SETUP_FUNC_INVOKED.get().is_some() {
67+
if BACKEND_SETUP_COMPLETED.load(Ordering::Relaxed) {
68+
return;
69+
}
70+
71+
// Race to let one async task do the setup job
72+
let _guard = MUTEX_LOCK.lock().await;
73+
74+
// Re-check in case the current async task is not the first one that acquires
75+
// the lock
76+
if BACKEND_SETUP_COMPLETED.load(Ordering::Relaxed) {
6677
return;
6778
}
6879

@@ -102,7 +113,5 @@ pub(crate) async fn backend_setup(tauri_app_handle: AppHandle, app_lang: String)
102113
update_app_lang(app_lang).await;
103114

104115
// Invoked, now update the state
105-
BACKEND_SETUP_FUNC_INVOKED
106-
.set(())
107-
.unwrap_or_else(|_| panic!("tauri command {}() gets called twice!", function_name!()));
116+
BACKEND_SETUP_COMPLETED.store(true, Ordering::Relaxed);
108117
}

src/routes/layout.tsx

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,22 @@
1-
import { useMount, useSessionStorageState } from "ahooks";
2-
import { useEffect } from "react";
1+
import { useMount } from "ahooks";
2+
import { useState } from "react";
33
import { invoke } from "@tauri-apps/api/core";
44

55
import LayoutOutlet from "./outlet";
66
import { useAppStore } from "@/stores/appStore";
7-
import platformAdapter from "@/utils/platformAdapter";
8-
import { CHAT_WINDOW_LABEL, MAIN_WINDOW_LABEL } from "@/constants";
97

108
const Layout = () => {
119
const { language } = useAppStore();
12-
const [ready, setReady] = useSessionStorageState("rust_ready", {
13-
defaultValue: false,
14-
});
10+
const [ready, setReady] = useState(false);
1511

1612
useMount(async () => {
17-
const label = await platformAdapter.getCurrentWindowLabel();
18-
19-
if (label === CHAT_WINDOW_LABEL) {
20-
setReady(true);
21-
}
22-
23-
if (ready || label !== MAIN_WINDOW_LABEL) return;
24-
2513
await invoke("backend_setup", {
2614
appLang: language,
2715
});
2816

2917
setReady(true);
30-
31-
platformAdapter.emitEvent("rust_ready");
3218
});
3319

34-
useEffect(() => {
35-
const unlisten = platformAdapter.listenEvent("rust_ready", () => {
36-
setReady(true);
37-
});
38-
39-
return () => {
40-
unlisten.then((fn) => fn());
41-
};
42-
}, []);
43-
4420
return ready && <LayoutOutlet />;
4521
};
4622

src/types/platform.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export interface EventPayloads {
4747
"check-update": any;
4848
oauth_success: any;
4949
extension_install_success: any;
50-
rust_ready: boolean;
5150
}
5251

5352
// Window operation interface

0 commit comments

Comments
 (0)