Skip to content

The VS Code of video editing, built for agents.

TypeScript

Add project readiness handling to engine context

konstantin-paulus2da322775ca4
diff --git a/apps/web/src/context/editor-api/api.tsx b/apps/web/src/context/editor-api/api.tsx
index a1c374c0..21e3aafd 100644
--- a/apps/web/src/context/editor-api/api.tsx
+++ b/apps/web/src/context/editor-api/api.tsx
@@ -2,9 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { createEffect, createContext, useContext, onCleanup, createResource } from "solid-js";
+import { createEffect, createContext, useContext, onCleanup, onMount, createResource } from "solid-js";
import { useSearchParams } from "@solidjs/router";
import { useEngine } from '@/context/engine';
+import { useProjectId } from '@/hooks/use-project-id';
import { useAuth } from '@/context/auth';
import { t, q, m, q0, m0 } from "@/lib/cli-rpc";
import { handleContextGet } from "./context";
@@ -40,6 +41,7 @@ type EditorApiContextValue = {
const EditorApiContext = createContext<EditorApiContextValue>();
export function EditorApiProvider(props: EditorApiProviderProps) {
+ const projectId = useProjectId();
const engine = useEngine();
const auth = useAuth();
const [, setParams] = useSearchParams();
@@ -60,17 +62,19 @@ export function EditorApiProvider(props: EditorApiProviderProps) {
createEffect(() => {
document.documentElement.dataset.fullscreen = String(isFullscreen());
+ });
+ onMount(() => {
if (!window.desktop) return;
+ onCleanup(mainBridge.handle(MAIN_CHANNELS.WINDOW_FULLSCREEN_CHANGE, handleWindowFullscreenChange(mutate)));
+ });
+
+ createEffect(() => {
+ if (!window.desktop || !engine.initialized() || projectId() !== engine.world.projectId) return;
- const router = createAppRouter({ getEngine, getUser, requireAuth, setParams });
- const unsubRouter = cliBridge.register(createRouterCaller(router));
- const unsubScreenChange = mainBridge.handle(MAIN_CHANNELS.WINDOW_FULLSCREEN_CHANGE, handleWindowFullscreenChange(mutate));
- onCleanup(() => {
- unsubRouter();
- unsubScreenChange();
- });
+ const router = createAppRouter({ getEngine, getUser, requireAuth, setParams });
+ onCleanup(cliBridge.register(createRouterCaller(router)));
});
return (
diff --git a/apps/web/src/context/editor-api/project.ts b/apps/web/src/context/editor-api/project.ts
index 4712075c..6751db1e 100644
--- a/apps/web/src/context/editor-api/project.ts
+++ b/apps/web/src/context/editor-api/project.ts
@@ -6,6 +6,7 @@ import type { Engine } from "@/components/engine";
import type { Accessor } from "solid-js";
import { createProject, deleteProject, getProject, getProjectName, listRecentProjects, DEFAULT_PROJECT_ID } from "@/components/engine/db";
+import { whenProjectReady } from "@/context/engine";
import { useSearchParams } from "@solidjs/router";
export function handleProjectActive(engine: Accessor<Engine>) {
@@ -34,6 +35,10 @@ export function handleProjectCreate(_engine: Accessor<Engine>, setParams: Return
return async ({ name }: { name?: string }) => {
const entry = await createProject(name);
setParams({ project: entry.id, dashboard: undefined }, { replace: true });
+ // Opening is a search param write; the engine for the new project mounts
+ // and restores after it. Hold the reply until that lands so the next CLI
+ // command can't run against the outgoing project's world.
+ await whenProjectReady(entry.id);
return { id: entry.id, name: entry.name };
}
}
@@ -58,6 +63,7 @@ export function handleProjectOpen(_engine: Accessor<Engine>, setParams: ReturnTy
const entry = await getProject(id);
if (!entry) return null;
setParams({ project: entry.id, dashboard: undefined }, { replace: true });
+ await whenProjectReady(entry.id);
return { id: entry.id, name: entry.name };
}
}
diff --git a/apps/web/src/context/engine.tsx b/apps/web/src/context/engine.tsx
index b44287d4..826398e5 100644
--- a/apps/web/src/context/engine.tsx
+++ b/apps/web/src/context/engine.tsx
@@ -37,6 +37,37 @@ type EngineProviderProps = {
type GrantableHandle = FileSystemDirectoryHandle | FileSystemFileHandle | ElectronFileHandle;;
+// Project-swap readiness barrier.
+const readyWaiters = new Map<string, Set<() => void>>();
+let readyProjectId: string | null = null;
+
+function markProjectReady(id: string): void {
+ readyProjectId = id;
+ const waiters = readyWaiters.get(id);
+ if (!waiters) return;
+ readyWaiters.delete(id);
+ for (const resolve of waiters) resolve();
+}
+
+export function whenProjectReady(id: string, timeoutMs = 30_000): Promise<void> {
+ if (readyProjectId === id) return Promise.resolve();
+
+ const { promise, resolve } = Promise.withResolvers<void>();
+ const waiters = readyWaiters.get(id) ?? new Set<() => void>();
+ waiters.add(resolve);
+ readyWaiters.set(id, waiters);
+
+ // Best-effort: a project whose engine never initializes (init threw) must
+ // not hang the caller forever.
+ const timer = setTimeout(() => {
+ readyWaiters.get(id)?.delete(resolve);
+ console.warn(`[engine] timed out waiting for project ${id} to become ready`);
+ resolve();
+ }, timeoutMs);
+
+ return promise.then(() => clearTimeout(timer));
+}
+
export function EngineProvider(props: EngineProviderProps) {
const engine = createEngine(props.projectId);
const resizeObserver = new ResizeObserver(() => engine.resize());
@@ -86,6 +117,8 @@ export function EngineProvider(props: EngineProviderProps) {
await engine.init(canvas);
+ markProjectReady(props.projectId);
+
// Detect filesystem handles whose permission was lost across reloads, then
// open a dialog so the user can re-grant in a real click handler — the
// requestPermission API requires a transient user activation.
@@ -121,6 +154,8 @@ export function EngineProvider(props: EngineProviderProps) {
});
onCleanup(() => {
+ if (readyProjectId === props.projectId) readyProjectId = null;
+
// Capture thumbnail before disposing the engine
const canvas = engine.world.canvas;
captureCanvasThumbnail(canvas).then((blob) => {