refactor: hooked up a backend-driven profile switch flow

This commit is contained in:
Slinetrac
2025-10-25 15:06:46 +08:00
parent cc39dcdc09
commit 49926b4823
7 changed files with 252 additions and 159 deletions
+108 -61
View File
@@ -34,7 +34,6 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { useLocation } from "react-router";
import useSWR, { mutate } from "swr";
import { closeAllConnections } from "tauri-plugin-mihomo-api";
import { BasePage, DialogRef } from "@/components/base";
import { BaseStyledTextField } from "@/components/base/base-styled-text-field";
@@ -57,14 +56,9 @@ import {
importProfile,
reorderProfile,
updateProfile,
switchProfileCommand,
} from "@/services/cmds";
import { showNotice } from "@/services/noticeService";
import {
enqueueSwitchTask,
subscribeSwitchWorker,
getSwitchWorkerSnapshot,
type SwitchWorkerEvent,
} from "@/services/profile-switch-worker";
import { useSetLoadingCache, useThemeMode } from "@/services/states";
// 记录profile切换状态
@@ -76,6 +70,16 @@ const debugProfileSwitch = (action: string, profile: string, extra?: any) => {
);
};
type ProfileSwitchFinishedPayload = {
profileId: string;
success: boolean;
};
type RustPanicPayload = {
message: string;
location: string;
};
const normalizeProfileUrl = (value?: string) => {
if (!value) return "";
const trimmed = value.trim();
@@ -208,7 +212,6 @@ const ProfilePage = () => {
const {
profiles = {},
activateSelected,
patchProfiles,
mutateProfiles,
error,
isStale,
@@ -379,34 +382,8 @@ const ProfilePage = () => {
}
};
const closeConnections = useCallback(async () => {
await closeAllConnections();
}, []);
const currentProfileId = profiles.current ?? null;
const [switchingProfileId, setSwitchingProfileId] = useState<string | null>(
() => getSwitchWorkerSnapshot().switching,
);
useEffect(() => {
const unsubscribe = subscribeSwitchWorker((event: SwitchWorkerEvent) => {
if (event.type === "success" || event.type === "error") {
setActivatings((prev) => prev.filter((id) => id !== event.profile));
}
setSwitchingProfileId(getSwitchWorkerSnapshot().switching);
});
return unsubscribe;
}, [setActivatings]);
useEffect(() => {
if (!switchingProfileId) return;
setActivatings((prev) => {
if (prev.includes(switchingProfileId)) return prev;
return [...prev, switchingProfileId];
});
}, [setActivatings, switchingProfileId]);
// 强化的刷新策略
const performRobustRefresh = async (
importVerifier: ImportLandingVerifier,
@@ -485,57 +462,127 @@ const ProfilePage = () => {
const onDragEnd = async (event: DragEndEvent) => {
const { active, over } = event;
if (over) {
if (active.id !== over.id) {
await reorderProfile(active.id.toString(), over.id.toString());
mutateProfiles();
}
if (over && active.id !== over.id) {
await reorderProfile(active.id.toString(), over.id.toString());
mutateProfiles();
}
};
const enqueueProfileSwitch = useCallback(
(targetProfile: string, notifySuccess: boolean) => {
const runSwitch = async () => {
await patchProfiles({ current: targetProfile });
const [switchingProfileId, setSwitchingProfileId] = useState<string | null>(
null,
);
const pendingProfileRef = useRef<string | null>(null);
const isSwitching = switchingProfileId != null;
useEffect(() => {
let mounted = true;
const unlistenPromise = listen<ProfileSwitchFinishedPayload>(
"profile-switch-finished",
(event) => {
if (!mounted) return;
const payload = event.payload;
if (!payload) return;
setActivatings((prev) => prev.filter((id) => id !== payload.profileId));
setSwitchingProfileId((prev) =>
prev === payload.profileId ? null : prev,
);
if (!payload.success) {
showNotice("error", t("Profile switch failed"));
}
},
);
return () => {
mounted = false;
unlistenPromise.then((unlisten) => unlisten()).catch(() => {});
};
}, [setActivatings, t]);
const executeSwitch = useCallback(
async function run(targetProfile: string, notifySuccess: boolean) {
setSwitchingProfileId(targetProfile);
setActivatings((prev) => {
if (prev.includes(targetProfile)) return prev;
return [...prev, targetProfile];
});
try {
const success = await switchProfileCommand(
targetProfile,
notifySuccess,
);
if (!success) {
showNotice("error", t("Profile switch failed"));
return;
}
await mutateLogs();
await closeConnections();
await new Promise((resolve) => setTimeout(resolve, 50));
await activateSelected();
if (notifySuccess) {
showNotice("success", t("Profile Switched"), 1000);
}
};
enqueueSwitchTask({
profile: targetProfile,
notifySuccess,
run: runSwitch,
});
} catch (error: any) {
showNotice(
"error",
error?.message || error?.toString?.() || String(error),
);
} finally {
setActivatings((prev) => prev.filter((id) => id !== targetProfile));
setSwitchingProfileId(null);
await mutateProfiles();
const next = pendingProfileRef.current;
pendingProfileRef.current = null;
if (next && next !== targetProfile) {
await run(next, true);
}
}
},
[activateSelected, closeConnections, mutateLogs, patchProfiles, t],
[activateSelected, mutateLogs, mutateProfiles, setActivatings, t],
);
const onSelect = useCallback(
(targetProfile: string, force: boolean) => {
if (switchingProfileId === targetProfile) {
debugProfileSwitch("DUPLICATE_CLICK_IGNORED", targetProfile);
if (isSwitching) {
pendingProfileRef.current = targetProfile;
return;
}
if (!force && targetProfile === currentProfileId) {
debugProfileSwitch("ALREADY_CURRENT_IGNORED", targetProfile);
return;
}
enqueueProfileSwitch(targetProfile, true);
executeSwitch(targetProfile, true);
},
[enqueueProfileSwitch, currentProfileId, switchingProfileId],
[currentProfileId, executeSwitch, isSwitching],
);
useEffect(() => {
if (current) {
mutateProfiles();
enqueueProfileSwitch(current, false);
if (!current) return;
if (isSwitching) {
pendingProfileRef.current = current;
return;
}
}, [current, enqueueProfileSwitch, mutateProfiles]);
if (current === currentProfileId) return;
executeSwitch(current, false);
}, [current, currentProfileId, executeSwitch, isSwitching]);
useEffect(() => {
let mounted = true;
const panicListener = listen<RustPanicPayload>("rust-panic", (event) => {
if (!mounted) return;
const payload = event.payload;
if (!payload) return;
showNotice(
"error",
`Rust panic: ${payload.message} @ ${payload.location}`,
);
console.error("Rust panic reported from backend:", payload);
});
return () => {
mounted = false;
panicListener.then((unlisten) => unlisten()).catch(() => {});
};
}, [t]);
const onEnhance = useLockFn(async (notifySuccess: boolean) => {
if (switchingProfileId) {
+7
View File
@@ -33,6 +33,13 @@ export async function patchProfilesConfig(profiles: IProfilesConfig) {
return invoke<void>("patch_profiles_config", { profiles });
}
export async function switchProfileCommand(
profileIndex: string,
notifySuccess: boolean,
) {
return invoke<boolean>("switch_profile", { profileIndex, notifySuccess });
}
export async function createProfile(
item: Partial<IProfileItem>,
fileData?: string | null,
-87
View File
@@ -1,87 +0,0 @@
type SwitchTask = {
profile: string;
notifySuccess: boolean;
run: () => Promise<void>;
};
export type SwitchWorkerEvent =
| { type: "start"; profile: string; notifySuccess: boolean }
| { type: "queued"; profile: string; notifySuccess: boolean }
| { type: "success"; profile: string; notifySuccess: boolean }
| { type: "error"; profile: string; error: string }
| { type: "idle" };
type Listener = (event: SwitchWorkerEvent) => void;
const listeners = new Set<Listener>();
let currentTask: SwitchTask | null = null;
let pendingTask: SwitchTask | null = null;
let running = false;
const emit = (event: SwitchWorkerEvent) => {
listeners.forEach((listener) => {
try {
listener(event);
} catch (error) {
console.error("[ProfileSwitchWorker] Listener error:", error);
}
});
};
const runCurrentTask = async () => {
if (!currentTask) {
emit({ type: "idle" });
return;
}
running = true;
const { profile, notifySuccess, run } = currentTask;
emit({ type: "start", profile, notifySuccess });
try {
await run();
emit({ type: "success", profile, notifySuccess });
} catch (error: any) {
const message = error?.message || String(error);
emit({ type: "error", profile, error: message });
} finally {
running = false;
currentTask = null;
if (pendingTask) {
currentTask = pendingTask;
pendingTask = null;
queueMicrotask(runCurrentTask);
} else {
emit({ type: "idle" });
}
}
};
export const enqueueSwitchTask = (task: SwitchTask) => {
if (running) {
pendingTask = task;
emit({
type: "queued",
profile: task.profile,
notifySuccess: task.notifySuccess,
});
return;
}
currentTask = task;
runCurrentTask();
};
export const subscribeSwitchWorker = (listener: Listener): (() => void) => {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
};
export const getSwitchWorkerSnapshot = () => ({
switching: currentTask?.profile ?? null,
queued: pendingTask?.profile ?? null,
running,
});