Skip to content

Open source CapCut alternative.

TypeScript

fix(site): background project writes wait out loads and fall back to the doc when a load fails

David Ledd1db65cbf26
diff --git a/site/src/cut/lib/genScene.ts b/site/src/cut/lib/genScene.ts
index 627e5bb4..f635e20d 100644
--- a/site/src/cut/lib/genScene.ts
+++ b/site/src/cut/lib/genScene.ts
@@ -7,7 +7,7 @@ import { realSuite } from "./genvideo/adapters";
import { GEN_FPS, StoreEditorBridge } from "./genvideo/editorBridge";
import { VideoOrchestrator } from "./genvideo/orchestrator";
import { onActivity } from "./genvideo/activity";
-import { withProjectDoc } from "./genvideo/docWriter";
+import { projectWriteMode, withProjectDoc } from "./genvideo/docWriter";
import type { RefAsset, Shot, VideoEvent, VideoPhase, VideoProject } from "./genvideo/types";
import { useEditor } from "./store";
import type { Aspect } from "./types";
@@ -446,11 +446,11 @@ function buildOrchestrator(projectId: string, project: VideoProject): VideoOrche
emit: (e) => applyEvent(projectId, e),
// Persist through the open store when this run's project is on screen
// (autosave carries it), else straight into the project's doc — a run
- // keeps its plan durable wherever the user is.
- persist: (p) => {
- const ed = useEditor.getState();
- if (ed.projectId === projectId && ed.loaded) {
- ed.setGenvideo(p);
+ // keeps its plan durable wherever the user is. Routed through
+ // projectWriteMode so a plan save never races a load's doc fetch.
+ persist: async (p) => {
+ if ((await projectWriteMode(projectId)) === "store") {
+ useEditor.getState().setGenvideo(p);
return;
}
return withProjectDoc(projectId, (doc) => {
diff --git a/site/src/cut/lib/genvideo/docWriter.ts b/site/src/cut/lib/genvideo/docWriter.ts
index 72d866cf..345f7ace 100644
--- a/site/src/cut/lib/genvideo/docWriter.ts
+++ b/site/src/cut/lib/genvideo/docWriter.ts
@@ -49,11 +49,30 @@ export function withProjectDoc(
}
/** Resolves once every queued background write for the project has settled —
- * the editor's project load awaits this so it never reads mid-write. */
+ * the editor's project load awaits this so it never reads mid-write. The load
+ * sets `projectId` (with `loaded` false) BEFORE awaiting, so a write that
+ * hasn't queued yet routes through `projectWriteMode` and waits for the load
+ * instead — between the two, no doc write can race the load's fetch. */
export function docWriterIdle(projectId: string): Promise<void> {
return (chains.get(projectId) ?? Promise.resolve()).catch(() => {});
}
+/** Where a background write for this project must land right now: the live
+ * store when the project is open and loaded, its persisted doc otherwise. A
+ * load in progress is waited out so a write never races the load's doc fetch —
+ * it either queued before the load began (docWriterIdle drains it) or lands in
+ * the store after. A failed load falls through to the doc: the store never
+ * loaded, autosave never runs, so the doc stays the source of truth. */
+export async function projectWriteMode(projectId: string): Promise<"store" | "doc"> {
+ for (;;) {
+ const s = useEditor.getState();
+ if (s.projectId !== projectId) return "doc";
+ if (s.loaded) return "store";
+ if (s.loadError) return "doc";
+ await new Promise((r) => setTimeout(r, 200));
+ }
+}
+
/** Register a run-created asset in a closed project's doc (the open-project
* path stocks the store instead and autosave persists it). Idempotent. */
export function stockAssetInDoc(projectId: string, asset: MediaAsset): Promise<void> {
diff --git a/site/src/cut/lib/genvideo/editorBridge.ts b/site/src/cut/lib/genvideo/editorBridge.ts
index 0e6debab..bbc3cc18 100644
--- a/site/src/cut/lib/genvideo/editorBridge.ts
+++ b/site/src/cut/lib/genvideo/editorBridge.ts
@@ -22,7 +22,7 @@
import { useEditor } from "../store";
import type { TransitionStyle } from "../types";
-import { docPlaceGenAudio, docPlaceGenClip, withProjectDoc } from "./docWriter";
+import { docPlaceGenAudio, docPlaceGenClip, projectWriteMode, withProjectDoc } from "./docWriter";
import type { AudioClipInfo, EditorBridge, TimelineInfo } from "./editor";
/** The rate the plan runs at. Fixed to the export frame rate so a shot's frame
@@ -52,16 +52,10 @@ export class StoreEditorBridge implements EditorBridge {
}
/** Where this call must write: the live store when the run's project is
- * open, its persisted doc when the user has moved on. A load in progress is
- * waited out so a write never races the doc fetch — either it lands in the
- * doc before the load reads it, or in the store after. */
- private async mode(): Promise<"store" | "doc"> {
- for (;;) {
- const s = useEditor.getState();
- if (s.projectId !== this.projectId) return "doc";
- if (s.loaded) return "store";
- await new Promise((r) => setTimeout(r, 200));
- }
+ * open, its persisted doc when the user has moved on (see projectWriteMode
+ * for the load-race and failed-load rules). */
+ private mode(): Promise<"store" | "doc"> {
+ return projectWriteMode(this.projectId);
}
getTimeline(): TimelineInfo {
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index 5eb402a5..e80ef6dd 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -542,11 +542,6 @@ export const useEditor = create<EditorState>((set, get) => {
genvideo: undefined,
loadProject: async (id) => {
- // A background scene run may still be writing this project's doc — wait
- // out its queued writes so the load never reads a half-written doc (and
- // never later autosaves over a placement it didn't see). Lazy import:
- // docWriter reads store helpers, so a static import would be a cycle.
- await import("./genvideo/docWriter").then((m) => m.docWriterIdle(id)).catch(() => {});
history.length = 0;
future.length = 0;
pending = null;
@@ -577,6 +572,14 @@ export const useEditor = create<EditorState>((set, get) => {
exportOpen: false,
genvideo: undefined,
});
+ // A background scene run may still be writing this project's doc — drain
+ // its queued writes so the load never reads a half-written doc. Ordering
+ // matters: projectId is set (loaded false) BEFORE this await, so a write
+ // arriving during the drain waits for the load (projectWriteMode) instead
+ // of queueing a doc write the drain would miss — nothing can land between
+ // the drain and the fetch below. Lazy import: docWriter reads store
+ // helpers, so a static import would be a cycle.
+ await import("./genvideo/docWriter").then((m) => m.docWriterIdle(id)).catch(() => {});
try {
const [res, ui] = await Promise.all([apiFetch(`/api/cut/projects/${id}`), loadUiState(id)]);
if (!res.ok) throw new Error("This project no longer exists.");