refactor: reduce webview lock contention (#6271)

* refactor: replace handle::Handle::get_window() with WindowManager::get_main_window() in multiple files

* refactor: enhance WindowManager to return window state alongside the main window instance

* refactor: update useProfiles and ProfilePage to support profile overrides and improve patchProfilesConfig return type

* refactor: enhance handle_success to check main window existence before notifying profile changes

* refactor: simplify get_main_window_with_state by using pattern matching and improve window state handling

* refactor: fix window activation by removing unnecessary reference in activate_existing_main_window

* refactor: remove redundant macOS conditional for window_manager import
This commit is contained in:
Tunglies
2026-02-07 05:45:15 +00:00
committed by GitHub
parent 6c6e0812b8
commit c30eaa3678
11 changed files with 83 additions and 72 deletions
+9 -8
View File
@@ -2,11 +2,11 @@ import useSWR, { mutate } from "swr";
import { selectNodeForGroup } from "tauri-plugin-mihomo-api";
import {
calcuProxies,
getProfiles,
patchProfile,
patchProfilesConfig,
} from "@/services/cmds";
import { calcuProxies } from "@/services/cmds";
import { debugLog } from "@/utils/debug";
export const useProfiles = () => {
@@ -36,6 +36,7 @@ export const useProfiles = () => {
const patchProfiles = async (
value: Partial<IProfilesConfig>,
signal?: AbortSignal,
options?: { deferRefreshOnSuccess?: boolean },
) => {
try {
if (signal?.aborted) {
@@ -47,7 +48,9 @@ export const useProfiles = () => {
throw new DOMException("Operation was aborted", "AbortError");
}
await mutateProfiles();
if (!options?.deferRefreshOnSuccess || !success) {
await mutateProfiles();
}
return success;
} catch (error) {
@@ -70,16 +73,14 @@ export const useProfiles = () => {
};
// 根据selected的节点选择
const activateSelected = async () => {
const activateSelected = async (profileOverride?: IProfilesConfig) => {
try {
debugLog("[ActivateSelected] 开始处理代理选择");
const [proxiesData, profileData] = await Promise.all([
calcuProxies(),
getProfiles(),
]);
const proxiesData = await calcuProxies();
const profileData = profileOverride ?? profiles;
if (!profileData || !proxiesData) {
if (!profileData || !proxiesData || !profileData.items) {
debugLog("[ActivateSelected] 代理或配置数据不可用,跳过处理");
return;
}
+3 -2
View File
@@ -389,7 +389,7 @@ const ProfilePage = () => {
switchingProfileRef.current === profile &&
!abortController.signal.aborted
) {
await activateSelected();
await activateSelected(profiles);
debugLog(`[Profile] 后台处理完成,序列号: ${sequence}`);
} else {
debugProfileSwitch(
@@ -402,7 +402,7 @@ const ProfilePage = () => {
console.warn("Failed to activate selected proxies:", err);
}
},
[activateSelected],
[activateSelected, profiles],
);
const activateProfile = useCallback(
@@ -456,6 +456,7 @@ const ProfilePage = () => {
const requestPromise = patchProfiles(
{ current: profile },
currentAbortController.signal,
{ deferRefreshOnSuccess: true },
);
pendingRequestRef.current = requestPromise;
+1 -1
View File
@@ -18,7 +18,7 @@ export async function enhanceProfiles() {
}
export async function patchProfilesConfig(profiles: IProfilesConfig) {
return invoke<void>("patch_profiles_config", { profiles });
return invoke<boolean>("patch_profiles_config", { profiles });
}
export async function createProfile(