fix(data): 增加全局保存失败保护

This commit is contained in:
Script Generator
2026-07-03 19:36:18 +08:00
parent 516a5f1b63
commit b4a3c990b4
17 changed files with 727 additions and 132 deletions

View File

@@ -0,0 +1,28 @@
type OptimisticRollbackOptions<T> = {
save: () => Promise<void>;
expected: T;
getCurrent: () => T;
rollback: () => void;
};
export async function saveWithOptimisticRollback<T>({
save,
expected,
getCurrent,
rollback,
}: OptimisticRollbackOptions<T>): Promise<void> {
try {
await save();
} catch (error) {
if (Object.is(getCurrent(), expected)) {
rollback();
}
throw error;
}
}
export function scheduleSaveWithOptimisticRollback<T>(
options: OptimisticRollbackOptions<T>,
): void {
void saveWithOptimisticRollback(options).catch(() => undefined);
}