Skip to content

Commit b2a006f

Browse files
fix(workspace): avoid UI stalls when switching (anomalyco#489)
1 parent c45a6e2 commit b2a006f

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

‎packages/app/src/app/app.tsx‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import type {
6565
ResetOpenworkMode,
6666
SettingsTab,
6767
SkillCard,
68+
SidebarSessionItem,
6869
TodoItem,
6970
View,
7071
WorkspaceSessionGroup,
@@ -1426,7 +1427,7 @@ export default function App() {
14261427

14271428
type SidebarWorkspaceSessionsStatus = WorkspaceSessionGroup["status"];
14281429
const [sidebarSessionsByWorkspaceId, setSidebarSessionsByWorkspaceId] = createSignal<
1429-
Record<string, Session[]>
1430+
Record<string, SidebarSessionItem[]>
14301431
>({});
14311432
const [sidebarSessionStatusByWorkspaceId, setSidebarSessionStatusByWorkspaceId] = createSignal<
14321433
Record<string, SidebarWorkspaceSessionsStatus>
@@ -1438,7 +1439,7 @@ export default function App() {
14381439
const pruneSidebarSessionState = (workspaceIds: Set<string>) => {
14391440
setSidebarSessionsByWorkspaceId((prev) => {
14401441
let changed = false;
1441-
const next: Record<string, Session[]> = {};
1442+
const next: Record<string, SidebarSessionItem[]> = {};
14421443
for (const [id, list] of Object.entries(prev)) {
14431444
if (!workspaceIds.has(id)) {
14441445
changed = true;
@@ -1560,9 +1561,18 @@ export default function App() {
15601561
? list.filter((session) => normalizeDirectoryPath(session.directory) === root)
15611562
: list;
15621563

1564+
const sorted = sortSessionsByActivity(filtered);
1565+
const items: SidebarSessionItem[] = sorted.map((session) => ({
1566+
id: session.id,
1567+
title: session.title,
1568+
slug: session.slug,
1569+
time: session.time,
1570+
directory: session.directory,
1571+
}));
1572+
15631573
setSidebarSessionsByWorkspaceId((prev) => ({
15641574
...prev,
1565-
[id]: sortSessionsByActivity(filtered),
1575+
[id]: items,
15661576
}));
15671577
setSidebarSessionStatusByWorkspaceId((prev) => ({ ...prev, [id]: "ready" }));
15681578
} catch (error) {
@@ -1582,6 +1592,8 @@ export default function App() {
15821592
: list;
15831593
for (const ws of ordered) {
15841594
await refreshSidebarWorkspaceSessions(ws.id);
1595+
// Yield so long refresh passes don't block UI / timers.
1596+
await new Promise<void>((resolve) => setTimeout(resolve, 0));
15851597
}
15861598
};
15871599

@@ -1628,13 +1640,7 @@ export default function App() {
16281640
const groupSessions = sessionsById[workspace.id] ?? [];
16291641
return {
16301642
workspace,
1631-
sessions: groupSessions.map((session) => ({
1632-
id: session.id,
1633-
title: session.title,
1634-
slug: session.slug,
1635-
time: session.time,
1636-
directory: session.directory,
1637-
})),
1643+
sessions: groupSessions,
16381644
status: statusById[workspace.id] ?? "idle",
16391645
error: errorById[workspace.id] ?? null,
16401646
};

‎packages/app/src/app/context/workspace.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,12 @@ export function createWorkspaceStore(options: {
515515
setConnectingWorkspaceId(id);
516516
updateWorkspaceConnectionState(id, { status: "connecting", message: null });
517517

518+
// Allow the UI to paint the "switching" state before we kick off work that can
519+
// trigger expensive reactive updates (e.g. sidebar session refreshes).
520+
if (typeof window !== "undefined" && typeof window.requestAnimationFrame === "function") {
521+
await new Promise<void>((resolve) => window.requestAnimationFrame(() => resolve()));
522+
}
523+
518524
try {
519525
if (isRemote) {
520526
options.setStartupPreference("server");

0 commit comments

Comments
 (0)