perf: optimize app startup and background lifecycles

This commit is contained in:
inman
2026-08-19 00:56:15 +08:00
parent 31332f6d76
commit 927e13349b
121 changed files with 4359 additions and 1400 deletions

View File

@@ -90,7 +90,7 @@ describe('project progress compliance synchronization', () => {
);
});
it('watches every registered project, maps identities, debounces changes, and skips duplicate content', async () => {
it('watches only the active project, maps identities, debounces changes, and skips duplicate content', async () => {
const serverPath = await createProjectDirectory('server');
const localPath = await createProjectDirectory('local');
await writeGameDocuments(serverPath, '# server gdd', '# server tasks');
@@ -98,6 +98,7 @@ describe('project progress compliance synchronization', () => {
const projectStore = createProjectStore(createMemoryProjectStorage());
const serverProject = await projectStore.rememberProject(serverPath);
const localProject = await projectStore.rememberProject(localPath);
await projectStore.setActiveProject(localProject.id);
await writeFile(
join(serverPath, 'works-cloud-deploy.json'),
createServerBindingFile(serverProject.id, 'server-game'),
@@ -115,9 +116,13 @@ describe('project progress compliance synchronization', () => {
synchronizers.push(sync);
await sync.start();
await Promise.all([serverProject.id, localProject.id].map((projectId) => sync.flushProject(projectId)));
await sync.flushProject(localProject.id);
expect(sync.getWatchedProjectIds()).toEqual(expect.arrayContaining([serverProject.id, localProject.id]));
expect(sync.getWatchedProjectIds()).toEqual([localProject.id]);
expect(fetchMock).toHaveBeenCalledTimes(1);
await sync.activate(serverProject.id);
await sync.flushProject(serverProject.id);
expect(sync.getWatchedProjectIds()).toEqual([serverProject.id]);
expect(fetchMock).toHaveBeenCalledTimes(2);
const payloads = fetchMock.mock.calls.map((_, index) => parsePayload(fetchMock, index));
const serverPayload = payloads.find((payload) => payload.project_key === 'server-game');
@@ -377,7 +382,7 @@ describe('project progress compliance synchronization', () => {
expect(parsePayload(fetchMock, 1).progress_text).toContain('# second tasks');
});
it('attaches a watcher when a project is registered after startup', async () => {
it('attaches a watcher only after a project becomes active', async () => {
const projectStore = createProjectStore(createMemoryProjectStorage());
const harness = createWatcherHarness();
const fetchMock = vi.fn().mockResolvedValue(new Response('{}', { status: 200 }));
@@ -394,6 +399,9 @@ describe('project progress compliance synchronization', () => {
await writeGameDocuments(projectPath, '# late gdd', '# late tasks');
const project = await projectStore.rememberProject(projectPath);
expect(sync.getWatchedProjectIds()).toEqual([]);
await projectStore.setActiveProject(project.id);
await sync.activate(project.id);
expect(sync.getWatchedProjectIds()).toContain(project.id);
await sync.flushProject(project.id);
expect(fetchMock).toHaveBeenCalledTimes(1);