Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(console): opening a chat's timeline no longer updates other views mid-render

raullopez-sandbox138542df7b3e
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-store.ts b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-store.ts
index 644060c51..fe98831ac 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-store.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-store.ts
@@ -402,8 +402,14 @@ export class ChatTimeline {
* them.
*/
export class ChatTimelines {
- /** Observable, so what reads a room's messages through {@link peek} follows a timeline opened later. */
- private readonly timelines = observable.map<string, ChatTimeline>({}, { deep: false });
+ private readonly timelines = new Map<string, ChatTimeline>();
+ /**
+ * The keys opened, observable, so what reads a room's messages through
+ * {@link peek} follows a timeline opened later. Told after the call that opened
+ * it, which is a view rendering: changing what other views observe while one
+ * renders is what React refuses.
+ */
+ private readonly opened = observable.set<string>();
constructor(private readonly api: (serverId: string) => TimelineApi) {}
@@ -411,15 +417,17 @@ export class ChatTimelines {
const key = `${serverId}\u0000${roomId}`;
let timeline = this.timelines.get(key);
if (!timeline) {
- const created = new ChatTimeline(roomId, this.api(serverId));
- runInAction(() => this.timelines.set(key, created));
- timeline = created;
+ timeline = new ChatTimeline(roomId, this.api(serverId));
+ this.timelines.set(key, timeline);
+ queueMicrotask(() => runInAction(() => this.opened.add(key)));
}
return timeline;
}
peek(serverId: string, roomId: string): ChatTimeline | undefined {
- return this.timelines.get(`${serverId}\u0000${roomId}`);
+ const key = `${serverId}\u0000${roomId}`;
+ this.opened.has(key);
+ return this.timelines.get(key);
}
/** Load again each of the server's timelines that loaded, is loading or failed to, catching up to the live feed. */
@@ -436,5 +444,9 @@ export class ChatTimelines {
dropServer(serverId: string): void {
for (const key of [...this.timelines.keys()])
if (key.startsWith(`${serverId}\u0000`)) this.timelines.delete(key);
+ runInAction(() => {
+ for (const key of [...this.opened])
+ if (key.startsWith(`${serverId}\u0000`)) this.opened.delete(key);
+ });
}
}
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-timelines-render.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-timelines-render.test.tsx
new file mode 100644
index 000000000..a7d27d324
--- /dev/null
+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-timelines-render.test.tsx
@@ -0,0 +1,47 @@
+/**
+ * A view that opens a chat's timeline while it renders (the chat panel) must
+ * not change what another view on screen observes (a chat row peeking at it):
+ * React refuses an update to one component made while another renders.
+ */
+import { observer } from 'mobx-react-lite';
+import { act } from 'react';
+import { createRoot } from 'react-dom/client';
+import { expect, it, vi } from 'vitest';
+import {
+ ChatTimelines,
+ type TimelineApi,
+} from '@renderer/features/chats/stores/chat-timeline-store';
+
+it('opens a timeline while rendering without updating a view that peeks at it', async () => {
+ const timelines = new ChatTimelines(() => ({}) as TimelineApi);
+ const Row = observer(() => <span>{timelines.peek('s', 'r1') ? 'open' : 'closed'}</span>);
+ const Panel = observer(() => {
+ timelines.get('s', 'r1');
+ return null;
+ });
+ const error = vi.spyOn(console, 'error').mockImplementation(() => {});
+ const container = document.createElement('div');
+ document.body.appendChild(container);
+ const root = createRoot(container);
+ // The row is on screen, watching, before the panel opens the timeline.
+ await act(async () => root.render(<Row />));
+ await act(async () =>
+ root.render(
+ <>
+ <Row />
+ <Panel />
+ </>
+ )
+ );
+ await act(async () => await Promise.resolve());
+ expect(
+ error.mock.calls.some((call) =>
+ String(call[0]).includes('while rendering a different component')
+ )
+ ).toBe(false);
+ // The row still follows the timeline once it is open.
+ expect(container.textContent).toBe('open');
+ error.mockRestore();
+ await act(async () => root.unmount());
+ container.remove();
+});