perf: optimize profile switching logic with interrupt support to prevent freeze
This commit is contained in:
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user