Open source CapCut alternative.
TypeScript
fix(cut): pin the backend to the open project's residency [rebuild]
David Lee94cfbf82981
diff --git a/site/src/app/cut/s/[token]/SharedProjectView.tsx b/site/src/app/cut/s/[token]/SharedProjectView.tsxindex 6a8853da..02e67b86 100644--- a/site/src/app/cut/s/[token]/SharedProjectView.tsx+++ b/site/src/app/cut/s/[token]/SharedProjectView.tsx@@ -6,7 +6,7 @@ import { Clapperboard, Loader2 } from "lucide-react";import { authHrefFor } from "@/app/_components/landing/useAppEntryHref";import { Button } from "@/components/ui/button";import { Editor } from "@/cut/components/Editor";-import { setCutMode } from "@/cut/lib/backend";+import { bindCutMode } from "@/cut/lib/backend";import { bindSharedBackend } from "@/cut/lib/backend/shared";import { useEditor } from "@/cut/lib/store";import type { ShareFeatures } from "@/cut/lib/types";@@ -41,7 +41,7 @@ export function SharedProject() {if (!res.ok) return setGate({ state: "missing" });const meta = (await res.json()) as { projectId: string; features: ShareFeatures };bindSharedBackend(token);- setCutMode("shared");+ bindCutMode("shared");useEditor.getState().setSharedView(meta.features);setGate({ state: "ready", projectId: meta.projectId });})diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsxindex 83997b67..673cd8a8 100644--- a/site/src/cut/components/Editor.tsx+++ b/site/src/cut/components/Editor.tsx@@ -4,7 +4,7 @@ import { useCallback, useEffect, useRef, useState } from "react";import Link from "next/link";import { Clapperboard, Loader2, X } from "lucide-react";import { Button } from "@/components/ui/button";-import { apiFetch, setCutMode } from "@/cut/lib/backend";+import { apiFetch, bindCutMode, releaseCutMode } from "@/cut/lib/backend";import { loadedDocVersion } from "@/cut/lib/backend/shared";import { refreshShareCard, renderPreviewProxy } from "@/cut/lib/exportClient";import { fileZoneAt, hasRefDrag } from "@/cut/lib/assetRef";@@ -72,14 +72,17 @@ export function Editor({// Load the project document, then enrich assets (thumbs/waveforms) lazily.// Residency is a fact about the project, not the link: resolve where the id- // lives (residency.ts) and bind the backend before anything else fetches.- // A share view skips that — its page bound the shared backend already.+ // lives (residency.ts) and pin the backend before anything else fetches.+ // Pinning, rather than setting the ambient mode, is what makes this stick —+ // the ConnectGate's engine probe finishes on its own schedule and would+ // otherwise flip an open cloud project onto the engine.+ // A share view skips that — its page pinned the shared backend already.useEffect(() => {let alive = true;const bind = viewer? Promise.resolve(): resolveProjectMode(projectId).then((mode) => {- if (alive) setCutMode(mode);+ if (alive) bindCutMode(mode);});void bind.then(() => {if (!alive) return;@@ -95,6 +98,7 @@ export function Editor({// project reconnect here.return () => {alive = false;+ releaseCutMode();};}, [projectId, viewer]);diff --git a/site/src/cut/lib/backend/index.test.ts b/site/src/cut/lib/backend/index.test.tsnew file mode 100644index 00000000..084a01d1--- /dev/null+++ b/site/src/cut/lib/backend/index.test.ts@@ -0,0 +1,50 @@+import { beforeEach, describe, expect, test } from "bun:test";+import { bindCutMode, cutMode, getBackend, releaseCutMode, setCutMode, subscribeCutMode } from "./index";++// The gate (ambient) and an open project's residency (pinned) both write the+// mode, asynchronously, on the same page load. These lock the precedence: a+// cloud project stays on the cloud backend even when the gate finds an engine+// on this Mac a moment later — the failure that sent a cloud project's saves+// and media requests to loopback, where neither exists.+describe("cut mode", () => {+ beforeEach(() => {+ releaseCutMode();+ setCutMode("local");+ });++ test("the ambient mode applies when no project is open", () => {+ expect(cutMode()).toBe("local");+ setCutMode("cloud");+ expect(cutMode()).toBe("cloud");+ expect(getBackend().kind).toBe("cloud");+ });++ test("a pinned project outranks a later ambient write", () => {+ bindCutMode("cloud");+ setCutMode("local"); // the gate's probe lands after the editor bound+ expect(cutMode()).toBe("cloud");+ expect(getBackend().kind).toBe("cloud");+ });++ test("releasing hands the mode back to the ambient value", () => {+ setCutMode("cloud");+ bindCutMode("local");+ expect(cutMode()).toBe("local");+ releaseCutMode();+ expect(cutMode()).toBe("cloud");+ });++ test("subscribers hear effective changes only", () => {+ let calls = 0;+ const stop = subscribeCutMode(() => calls++);+ bindCutMode("cloud");+ expect(calls).toBe(1);+ // Ambient churn under a pin changes nothing anyone can observe.+ setCutMode("cloud");+ setCutMode("local");+ expect(calls).toBe(1);+ releaseCutMode();+ expect(calls).toBe(2);+ stop();+ });+});diff --git a/site/src/cut/lib/backend/index.ts b/site/src/cut/lib/backend/index.tsindex f33c6115..07e4b8d0 100644--- a/site/src/cut/lib/backend/index.ts+++ b/site/src/cut/lib/backend/index.ts@@ -14,18 +14,48 @@ import type { CutBackend, CutCaps, CutMode } from "./types";export type { CutBackend, CutCaps, CutMode };export { apiJson } from "../api";-let mode: CutMode = "local";+// Two things decide the backend, and only one of them knows about a project.+// The gate probes this Mac for an engine and sets the *ambient* mode — what the+// projects home and library run against, before any project is open. A project+// that is open pins the mode to its own residency, which is a fact about that+// project and outranks the ambient guess. Both writers are async and race on+// load; pinning is what keeps the gate's later "an engine answered, so local"+// from dragging an open cloud project onto the engine, where its doc and media+// don't exist.+let ambient: CutMode = "local";+let pinned: CutMode | null = null;const listeners = new Set<() => void>();-/** Set by the ConnectGate before the app's data layer runs. */+function settle(apply: () => void) {+ const before = cutMode();+ apply();+ if (cutMode() !== before) listeners.forEach((l) => l());+}++/** Set by the ConnectGate: where the app runs when no project is open. */export function setCutMode(next: CutMode) {- if (mode === next) return;- mode = next;- listeners.forEach((l) => l());+ settle(() => {+ ambient = next;+ });+}++/** Pin the mode to an open project's residency, outranking the ambient mode+ * until `releaseCutMode`. */+export function bindCutMode(next: CutMode) {+ settle(() => {+ pinned = next;+ });+}++/** Drop the pin when the project closes; the ambient mode takes over again. */+export function releaseCutMode() {+ settle(() => {+ pinned = null;+ });}export function cutMode(): CutMode {- return mode;+ return pinned ?? ambient;}/** Subscription feed for ./hooks' useSyncExternalStore. */@@ -37,6 +67,7 @@ export function subscribeCutMode(onChange: () => void) {}export function getBackend(): CutBackend {+ const mode = cutMode();return mode === "cloud" ? cloudBackend : mode === "shared" ? sharedBackend : localBackend;}diff --git a/site/src/cut/lib/backend/shared.ts b/site/src/cut/lib/backend/shared.tsindex bdb29c4b..9ef62bee 100644--- a/site/src/cut/lib/backend/shared.ts+++ b/site/src/cut/lib/backend/shared.ts@@ -8,7 +8,7 @@ import type { CutBackend } from "./types";let token = "";-/** Bind the share link token before setCutMode("shared"). */+/** Bind the share link token before bindCutMode("shared"). */export function bindSharedBackend(next: string) {token = next;}