Skip to content

Commit 9f48f48

Browse files
authored
fix: Break circular dependency between authStore and session service (#913)
1 parent ec25c69 commit 9f48f48

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

apps/twig/src/renderer/features/auth/stores/authStore.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { PostHogAPIClient } from "@api/posthogClient";
2-
import { resetSessionService } from "@features/sessions/service/service";
32
import { identifyUser, resetUser, track } from "@renderer/lib/analytics";
43
import { electronStorage } from "@renderer/lib/electronStorage";
54
import { logger } from "@renderer/lib/logger";
@@ -21,6 +20,12 @@ const log = logger.scope("auth-store");
2120
let refreshPromise: Promise<void> | null = null;
2221
let initializePromise: Promise<boolean> | null = null;
2322

23+
let sessionResetCallback: (() => void) | null = null;
24+
25+
export function setSessionResetCallback(callback: () => void) {
26+
sessionResetCallback = callback;
27+
}
28+
2429
const REFRESH_MAX_RETRIES = 3;
2530
const REFRESH_INITIAL_DELAY_MS = 1000;
2631

@@ -644,7 +649,7 @@ export const useAuthStore = create<AuthState>()(
644649
}
645650

646651
// Clean up all existing sessions before switching projects
647-
resetSessionService();
652+
sessionResetCallback?.();
648653

649654
const apiHost = getCloudUrlFromRegion(cloudRegion);
650655

@@ -706,7 +711,7 @@ export const useAuthStore = create<AuthState>()(
706711
resetUser();
707712

708713
// Clean up session service subscriptions before clearing auth state
709-
resetSessionService();
714+
sessionResetCallback?.();
710715

711716
trpcVanilla.analytics.resetUser.mutate();
712717

apps/twig/src/renderer/features/sessions/service/service.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const mockAuthStore = vi.hoisted(() => ({
6464
},
6565
})),
6666
},
67+
setSessionResetCallback: vi.fn(),
6768
}));
6869

6970
vi.mock("@features/auth/stores/authStore", () => mockAuthStore);

apps/twig/src/renderer/features/sessions/service/service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import type {
33
RequestPermissionRequest,
44
SessionConfigOption,
55
} from "@agentclientprotocol/sdk";
6-
import { useAuthStore } from "@features/auth/stores/authStore";
6+
import {
7+
setSessionResetCallback,
8+
useAuthStore,
9+
} from "@features/auth/stores/authStore";
710
import { useModelsStore } from "@features/sessions/stores/modelsStore";
811
import { useSessionAdapterStore } from "@features/sessions/stores/sessionAdapterStore";
912
import {
@@ -89,6 +92,8 @@ export function resetSessionService(): void {
8992
});
9093
}
9194

95+
setSessionResetCallback(resetSessionService);
96+
9297
export class SessionService {
9398
private connectingTasks = new Map<string, Promise<void>>();
9499
private subscriptions = new Map<

0 commit comments

Comments
 (0)