fix(project): stabilize project deletion
- Guard renderer project list/get against stale initial loads - Retry project zip/cache removal to handle transient Windows locks - Surface deletion failures in UI and add regression tests Made-with: Cursor
This commit is contained in:
@@ -917,7 +917,13 @@ function ProjectPicker({ projects, onCreate, onOpen, onDelete }: ProjectPickerPr
|
||||
) {
|
||||
return;
|
||||
}
|
||||
void onDelete(id);
|
||||
void (async () => {
|
||||
try {
|
||||
await onDelete(id);
|
||||
} catch (e) {
|
||||
window.alert(e instanceof Error ? e.message : String(e));
|
||||
}
|
||||
})();
|
||||
}}
|
||||
>
|
||||
Удалить
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import test from 'node:test';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const here = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
void test('projectState: list/get after delete invalidates in-flight initial load (epoch guard)', () => {
|
||||
const src = fs.readFileSync(path.join(here, 'projectState.ts'), 'utf8');
|
||||
assert.match(src, /projectDataEpochRef/);
|
||||
assert.match(src, /const epoch = projectDataEpochRef\.current/);
|
||||
assert.match(src, /if \(projectDataEpochRef\.current !== epoch\) return/);
|
||||
assert.match(
|
||||
src,
|
||||
/const deleteProject = async[\s\S]+?projectDataEpochRef\.current \+= 1[\s\S]+?await api\.invoke/,
|
||||
);
|
||||
assert.match(
|
||||
src,
|
||||
/const openProject = async[\s\S]+?projectDataEpochRef\.current \+= 1[\s\S]+?await api\.invoke/,
|
||||
);
|
||||
assert.match(src, /const refreshProjects = async \(\) => \{[\s\S]+?projectDataEpochRef\.current \+= 1/);
|
||||
});
|
||||
@@ -58,12 +58,15 @@ export function useProjectState(licenseActive: boolean): readonly [State, Action
|
||||
const api = getDndApi();
|
||||
const [state, setState] = useState<State>({ projects: [], project: null, selectedSceneId: null });
|
||||
const projectRef = useRef<Project | null>(null);
|
||||
/** Bumps on mutations / refresh; initial license load only applies if still current (avoids racing late list/get over newer state). */
|
||||
const projectDataEpochRef = useRef(0);
|
||||
useEffect(() => {
|
||||
projectRef.current = state.project;
|
||||
}, [state.project]);
|
||||
|
||||
const actions = useMemo<Actions>(() => {
|
||||
const refreshProjects = async () => {
|
||||
projectDataEpochRef.current += 1;
|
||||
const res = await api.invoke(ipcChannels.project.list, {});
|
||||
setState((s) => ({ ...s, projects: res.projects }));
|
||||
};
|
||||
@@ -75,6 +78,7 @@ export function useProjectState(licenseActive: boolean): readonly [State, Action
|
||||
};
|
||||
|
||||
const openProject = async (id: ProjectId) => {
|
||||
projectDataEpochRef.current += 1;
|
||||
const res = await api.invoke(ipcChannels.project.open, { projectId: id });
|
||||
setState((s) => ({ ...s, project: res.project, selectedSceneId: res.project.currentSceneId }));
|
||||
};
|
||||
@@ -276,6 +280,7 @@ export function useProjectState(licenseActive: boolean): readonly [State, Action
|
||||
};
|
||||
|
||||
const deleteProject = async (projectId: ProjectId) => {
|
||||
projectDataEpochRef.current += 1;
|
||||
await api.invoke(ipcChannels.project.deleteProject, { projectId });
|
||||
const listRes = await api.invoke(ipcChannels.project.list, {});
|
||||
const res = await api.invoke(ipcChannels.project.get, {});
|
||||
@@ -316,14 +321,18 @@ export function useProjectState(licenseActive: boolean): readonly [State, Action
|
||||
useEffect(() => {
|
||||
if (!licenseActive) {
|
||||
queueMicrotask(() => {
|
||||
projectDataEpochRef.current += 1;
|
||||
setState({ projects: [], project: null, selectedSceneId: null });
|
||||
});
|
||||
return;
|
||||
}
|
||||
void (async () => {
|
||||
const epoch = projectDataEpochRef.current;
|
||||
const listRes = await api.invoke(ipcChannels.project.list, {});
|
||||
if (projectDataEpochRef.current !== epoch) return;
|
||||
setState((s) => ({ ...s, projects: listRes.projects }));
|
||||
const res = await api.invoke(ipcChannels.project.get, {});
|
||||
if (projectDataEpochRef.current !== epoch) return;
|
||||
setState((s) => ({ ...s, project: res.project, selectedSceneId: res.project?.currentSceneId ?? null }));
|
||||
})();
|
||||
}, [licenseActive, api]);
|
||||
|
||||
Reference in New Issue
Block a user