perf: optimize profile switching logic with interrupt support to prevent freeze

This commit is contained in:
wonfen
2025-06-21 10:04:01 +08:00
parent b72f397369
commit 1a6454ee79
7 changed files with 496 additions and 73 deletions

View File

@@ -19,21 +19,29 @@ export const useProfiles = () => {
},
);
const patchProfiles = async (value: Partial<IProfilesConfig>) => {
// 立即更新本地状态
if (value.current && profiles) {
const optimisticUpdate = {
...profiles,
current: value.current,
};
mutateProfiles(optimisticUpdate, false); // 不重新验证
}
const patchProfiles = async (
value: Partial<IProfilesConfig>,
signal?: AbortSignal,
) => {
try {
await patchProfilesConfig(value);
mutateProfiles();
if (signal?.aborted) {
throw new DOMException("Operation was aborted", "AbortError");
}
const success = await patchProfilesConfig(value);
if (signal?.aborted) {
throw new DOMException("Operation was aborted", "AbortError");
}
await mutateProfiles();
return success;
} catch (error) {
mutateProfiles();
if (error instanceof DOMException && error.name === "AbortError") {
throw error;
}
await mutateProfiles();
throw error;
}
};