Skip to content

Open source CapCut alternative.

TypeScript

perf(site): widen idle editor sync polls

David Lef34f480e47c1
diff --git a/site/src/cut/components/AiPanel.tsx b/site/src/cut/components/AiPanel.tsx
index 6f82c560..398cd9c0 100644
--- a/site/src/cut/components/AiPanel.tsx
+++ b/site/src/cut/components/AiPanel.tsx
@@ -112,7 +112,8 @@ import { resolvePlayheadTokens, syncTimes, type TimeMark } from "@/cut/lib/timeM
import { productionDeps } from "@/cut/lib/pi/prodDeps";
import { withChatProject } from "@/cut/lib/projectChatTools";
import { runAiTool } from "@/cut/lib/aiTools";
-import { holdEditorChat } from "@/cut/lib/editorWork";
+import { editorChatActive, holdEditorChat } from "@/cut/lib/editorWork";
+import { nextSyncMs } from "@/cut/lib/syncCadence";
import { recoverSceneCall } from "@/cut/lib/chatRecovery";
import { countChunks, replayChatStream } from "@/cut/lib/chatReplay";
import { INTERRUPTED_ERROR, isResumeMessage, journalAhead, RESUME_LIMIT, resumePrompt, scrubLostTurn, settledMark, unfinishedAsk, type JournalMark, type ResumeMetadata, type StreamedMark, type TurnSettled } from "@/cut/lib/chatResume";
@@ -603,16 +604,42 @@ export function AiPanel({
};
}, [projectId, readOnly]);
+ // Re-read the cloud copy of the project's chats: at the sync interval while
+ // they move or this tab's chat runs, widening while they hold still. The
+ // window coming back re-reads at once.
useEffect(() => {
let stopped = false;
+ let running = false;
let timer: ReturnType<typeof setTimeout>;
+ let waitMs: number = CHAT_RUNTIME.syncIntervalMs;
const refresh = async () => {
- if (!document.hidden && useEditor.getState().projectId === projectId)
- await ensureCloudThreads(projectId, true);
- if (!stopped) timer = setTimeout(() => void refresh(), CHAT_RUNTIME.syncIntervalMs);
+ if (stopped || running) return;
+ running = true;
+ try {
+ if (!document.hidden && useEditor.getState().projectId === projectId) {
+ const pulse = await ensureCloudThreads(projectId, true);
+ waitMs = nextSyncMs(waitMs, editorChatActive(projectId) ? "moved" : pulse);
+ }
+ } finally {
+ running = false;
+ if (!stopped) timer = setTimeout(() => void refresh(), waitMs);
+ }
+ };
+ const wake = () => {
+ if (document.hidden) return;
+ clearTimeout(timer);
+ waitMs = CHAT_RUNTIME.syncIntervalMs;
+ void refresh();
+ };
+ document.addEventListener("visibilitychange", wake);
+ window.addEventListener("focus", wake);
+ timer = setTimeout(() => void refresh(), waitMs);
+ return () => {
+ stopped = true;
+ clearTimeout(timer);
+ document.removeEventListener("visibilitychange", wake);
+ window.removeEventListener("focus", wake);
};
- timer = setTimeout(() => void refresh(), CHAT_RUNTIME.syncIntervalMs);
- return () => { stopped = true; clearTimeout(timer); };
}, [projectId]);
// A shared view opens on the owner's newest thread — the viewer has no chat
diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsx
index 9c0ea56e..357e8647 100644
--- a/site/src/cut/components/Editor.tsx
+++ b/site/src/cut/components/Editor.tsx
@@ -50,6 +50,7 @@ import { meterMainThread, meterPerf, stopMainThreadMeter, stopMeter } from "@/cu
import { isDragActive } from "@/cut/lib/drag";
import { track } from "@/lib/analytics";
import { ensureCloudThreads } from "@/cut/lib/chatCloud";
+import { nextSyncMs } from "@/cut/lib/syncCadence";
import { registerEditorSave, editorChatActive, editorToolsActive, finishEditorWork, holdEditorLoad } from "@/cut/lib/editorWork";
import { noteProjectRevision, projectRevision } from "@/cut/lib/projectRevision";
import { readProjectThreads, writeActiveChat } from "@/cut/lib/chatThreads";
@@ -352,10 +353,14 @@ export function Editor({
let stopped = false;
let polling = false;
let timer: ReturnType<typeof setTimeout> | null = null;
+ // The owner's wait between re-reads: it widens while the stored version
+ // holds still and snaps back on a change, on focus, and while this tab is
+ // busy with its own edits, playback or chat.
+ let ownerMs: number = CHAT_RUNTIME.syncIntervalMs;
const tick = async () => {
if (stopped || polling) return;
polling = true;
- let nextIn = document.hidden ? VIEWER_POLL_MAX_MS : (viewer ? VIEWER_POLL_MIN_MS : CHAT_RUNTIME.syncIntervalMs);
+ let nextIn = document.hidden ? VIEWER_POLL_MAX_MS : (viewer ? VIEWER_POLL_MIN_MS : ownerMs);
try {
const current = useEditor.getState();
if (!document.hidden && current.projectId === projectId && current.loaded &&
@@ -378,13 +383,22 @@ export function Editor({
// instead of the viewer settling on an older cut.
const version = res.headers.get("x-cut-doc-version");
const showing = viewer ? loadedDocVersion(projectId) : projectRevision(projectId);
- if (version && showing && version !== showing) {
+ const moved = !!version && !!showing && version !== showing;
+ if (!viewer) {
+ ownerMs = nextSyncMs(ownerMs, moved ? "moved" : "still");
+ nextIn = ownerMs;
+ }
+ if (moved) {
if (!viewer && (useEditor.getState().saveState !== "saved" || editorToolsActive() || editorChatActive(projectId))) return;
const at = playheadAt();
await useEditor.getState().loadProject(projectId, { inPlace: true, viewer });
if (!stopped && useEditor.getState().projectId === projectId) useEditor.getState().seek(at);
}
}
+ } else if (!viewer && !document.hidden) {
+ // Busy with its own work: the first quiet tick re-reads at the floor.
+ ownerMs = CHAT_RUNTIME.syncIntervalMs;
+ nextIn = ownerMs;
}
} catch {
// Transient network failure — the next tick retries.
@@ -396,6 +410,7 @@ export function Editor({
const focus = () => {
if (document.hidden) return;
if (timer) clearTimeout(timer);
+ ownerMs = CHAT_RUNTIME.syncIntervalMs;
void tick();
};
document.addEventListener("visibilitychange", focus);
diff --git a/site/src/cut/lib/chatCloud.test.ts b/site/src/cut/lib/chatCloud.test.ts
index d909b0c5..b41bbaa3 100644
--- a/site/src/cut/lib/chatCloud.test.ts
+++ b/site/src/cut/lib/chatCloud.test.ts
@@ -32,10 +32,10 @@ test("unchanged polls read metadata without loading transcripts or local history
bodyReads++;
return Response.json([{ id: "a", updatedAt: 1 }]);
});
- await ensureCloudThreads("unchanged", true);
+ expect(await ensureCloudThreads("unchanged", true)).toBe("moved");
expect(bodyReads).toBe(1);
storageReads = 0;
- await ensureCloudThreads("unchanged", true);
+ expect(await ensureCloudThreads("unchanged", true)).toBe("still");
expect(bodyReads).toBe(1);
expect(storageReads).toBe(0);
});
diff --git a/site/src/cut/lib/chatCloud.ts b/site/src/cut/lib/chatCloud.ts
index 06740fb5..e8d6767c 100644
--- a/site/src/cut/lib/chatCloud.ts
+++ b/site/src/cut/lib/chatCloud.ts
@@ -6,6 +6,7 @@
import { cutMode, getBackend, type CutBackend } from "./backend";
import { cloudBackend } from "./backend/cloud";
import { compactThreadForCloud } from "./chatCompact";
+import type { SyncPulse } from "./syncCadence";
import {
chatThreadDeleted,
cloudThreadSaved,
@@ -23,7 +24,7 @@ let syncState: {
projectId: string;
backend: CutBackend;
versions: Map<string, string>;
- pending?: Promise<void>;
+ pending?: Promise<SyncPulse>;
seeded: boolean;
dirty: boolean;
} | undefined;
@@ -87,24 +88,28 @@ async function sendCloudThread(backend: CutBackend, projectId: string, thread: u
cloudThreadSaved();
}
-/** Merge changed conversations and retry local saves, including offline deletions. */
-export function ensureCloudThreads(projectId: string, refresh = false): Promise<void> {
+/** Merge changed conversations and retry local saves, including offline
+ * deletions. Resolves "moved" when a conversation came down or went up, so a
+ * poller can widen its wait while nothing does. */
+export function ensureCloudThreads(projectId: string, refresh = false): Promise<SyncPulse> {
const backend = getBackend();
// The engine and browser stores keep no copy of their own: the history this
// browser holds is all there is, once it has loaded.
- if (backend.kind === "local" || backend.kind === "browser") return loadProjectThreads(projectId);
+ if (backend.kind === "local" || backend.kind === "browser") return loadProjectThreads(projectId).then(() => "still");
if (!syncState || syncState.projectId !== projectId || syncState.backend !== backend)
syncState = { projectId, backend, versions: new Map(), seeded: false, dirty: true };
const state = syncState;
if (state.pending) return state.pending;
- if (state.seeded && !refresh) return Promise.resolve();
- state.pending = syncThreads(state).then(() => { state.seeded = true; }).catch(() => {
+ if (state.seeded && !refresh) return Promise.resolve("still");
+ // A failed read keeps the poller at the floor, so the retry comes soon.
+ state.pending = syncThreads(state).then((pulse) => { state.seeded = true; return pulse; }).catch((): SyncPulse => {
state.seeded = false;
+ return "moved";
}).finally(() => { state.pending = undefined; });
return state.pending;
}
-async function syncThreads(state: NonNullable<typeof syncState>): Promise<void> {
+async function syncThreads(state: NonNullable<typeof syncState>): Promise<SyncPulse> {
const { projectId, backend, versions } = state;
// Merge into what this browser stored, never into a not-yet-read empty list.
await loadProjectThreads(projectId);
@@ -113,8 +118,10 @@ async function syncThreads(state: NonNullable<typeof syncState>): Promise<void>
if (!response.ok) throw new Error("Could not read the project's chats.");
const index = await response.json() as { id: string; revision: string }[];
const remoteIds = new Set(index.map((row) => row.id));
+ let pulse: SyncPulse = "still";
for (const row of index) {
if (versions.get(row.id) === row.revision) continue;
+ pulse = "moved";
const res = await backend.fetch(`${path}?id=${encodeURIComponent(row.id)}&revision=${encodeURIComponent(row.revision)}`);
if (!res.ok) throw new Error("Could not read the conversation.");
const remote = (await res.json() as unknown[]).filter(isStoredThread);
@@ -131,10 +138,13 @@ async function syncThreads(state: NonNullable<typeof syncState>): Promise<void>
}
if (backend.kind === "cloud" && state.dirty) {
for (const thread of readProjectThreads(projectId)) {
- if (!remoteIds.has(thread.id)) await saveCloudThread(backend, projectId, thread);
+ if (remoteIds.has(thread.id)) continue;
+ await saveCloudThread(backend, projectId, thread);
+ pulse = "moved";
}
state.dirty = false;
}
+ return pulse;
}
async function saveCloudThread(backend: CutBackend, projectId: string, thread: StoredThread): Promise<void> {
diff --git a/site/src/cut/lib/syncCadence.test.ts b/site/src/cut/lib/syncCadence.test.ts
new file mode 100644
index 00000000..0a44e9a6
--- /dev/null
+++ b/site/src/cut/lib/syncCadence.test.ts
@@ -0,0 +1,17 @@
+import { expect, test } from "bun:test";
+import { CHAT_RUNTIME } from "./chatRuntime";
+import { nextSyncMs, SYNC_IDLE_MAX_MS } from "./syncCadence";
+
+test("a still project widens the wait up to the idle ceiling", () => {
+ let wait: number = CHAT_RUNTIME.syncIntervalMs;
+ const waits: number[] = [];
+ for (let i = 0; i < 5; i++) {
+ wait = nextSyncMs(wait, "still");
+ waits.push(wait);
+ }
+ expect(waits).toEqual([10_000, 20_000, SYNC_IDLE_MAX_MS, SYNC_IDLE_MAX_MS, SYNC_IDLE_MAX_MS]);
+});
+
+test("a change snaps the wait back to the sync interval", () => {
+ expect(nextSyncMs(SYNC_IDLE_MAX_MS, "moved")).toBe(CHAT_RUNTIME.syncIntervalMs);
+});
diff --git a/site/src/cut/lib/syncCadence.ts b/site/src/cut/lib/syncCadence.ts
new file mode 100644
index 00000000..1cd9781c
--- /dev/null
+++ b/site/src/cut/lib/syncCadence.ts
@@ -0,0 +1,19 @@
+import { CHAT_RUNTIME } from "./chatRuntime";
+
+// How an open editor paces its re-reads of a project and its chats. Each
+// re-read of a cloud project is a hosted request, so an idle tab widens toward
+// the ceiling; a change, the window coming back, or this tab's own chat snaps
+// it back to the sync interval.
+export const SYNC_IDLE_MAX_MS = 30_000;
+
+// What the last re-read found: the project changed, or nothing did.
+export type SyncPulse = "moved" | "still";
+
+/** The wait before the next re-read: 10s, 20s, then 30s while still; the sync
+ * interval once anything moves. */
+export function nextSyncMs(lastMs: number, pulse: SyncPulse): number {
+ if (pulse === "moved") {
+ return CHAT_RUNTIME.syncIntervalMs;
+ }
+ return Math.min(lastMs * 2, SYNC_IDLE_MAX_MS);
+}