Skip to content

The VS Code of video editing, built for agents.

TypeScript

Add @diffusionstudio/koota-solid and koota dependencies; refactor Engine class to manage running state with listeners

konstantin-paulusb750ee964db6
diff --git a/apps/web/package.json b/apps/web/package.json
index 22681993..d9bf9822 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -16,6 +16,7 @@
"@diffusionstudio/api-contract": "^0.1.0",
"@diffusionstudio/cli": "*",
"@diffusionstudio/jsx": "*",
+ "@diffusionstudio/koota-solid": "*",
"@diffusionstudio/runtime": "*",
"@kobalte/core": "^0.13.11",
"@sentry/solid": "^10.51.0",
@@ -32,6 +33,7 @@
"colord": "^2.9.3",
"cva": "^1.0.0-beta.4",
"idb": "^8.0.3",
+ "koota": "^0.6.6",
"mediabunny": "^1.32.2",
"solid-js": "^1.9.10",
"somoto": "^0.0.2",
diff --git a/apps/web/src/engine/context.tsx b/apps/web/src/engine/context.tsx
new file mode 100644
index 00000000..b147a648
--- /dev/null
+++ b/apps/web/src/engine/context.tsx
@@ -0,0 +1,47 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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 { createContext, onCleanup, useContext, type JSX } from 'solid-js';
+import { WorldProvider } from '@diffusionstudio/koota-solid';
+
+import { Engine, type EngineOptions } from './create-engine';
+
+const EngineContext = createContext<Engine>();
+
+export interface EngineProviderProps {
+ projectId: string;
+ options?: EngineOptions;
+ children?: JSX.Element;
+}
+
+/**
+ * Provides the Engine instance (lifecycle: mount/resize/start/stop) and,
+ * via koota-solid's WorldProvider, its koota world. Deliberately does not
+ * hand out a bag of engine internals — components read world-trait state
+ * with useTrait/useQuery (see ./hooks) and reach the Engine itself only for
+ * lifecycle calls (useEngineContext()), keeping each consumer's dependency
+ * explicit instead of importing one god object.
+ */
+export function EngineProvider(props: EngineProviderProps): JSX.Element {
+ const engine = new Engine(props.projectId, props.options);
+ onCleanup(() => engine.dispose());
+
+ return (
+ <EngineContext.Provider value={engine}>
+ <WorldProvider world={engine.world}>
+ {props.children}
+ </WorldProvider>
+ </EngineContext.Provider>
+ );
+}
+
+export function useEngineContext(): Engine {
+ const engine = useContext(EngineContext);
+
+ if (!engine) {
+ throw new Error('useEngineContext must be used within an EngineProvider');
+ }
+
+ return engine;
+}
diff --git a/apps/web/src/engine/create-engine.ts b/apps/web/src/engine/create-engine.ts
index f12f46ee..dd1b7cd3 100644
--- a/apps/web/src/engine/create-engine.ts
+++ b/apps/web/src/engine/create-engine.ts
@@ -24,13 +24,15 @@ export class Engine {
readonly world: RuntimeWorld;
private canvas: HTMLCanvasElement | null = null;
- private readonly audioContext: AudioContext;
- private readonly ownsAudioContext: boolean;
private _running = false;
private rafId: number | null = null;
- // null => the next step() produces delta 0 (first frame, or first frame after a resume).
private lastTimestamp: number | null = null;
+ private readonly audioContext: AudioContext;
+ private readonly ownsAudioContext: boolean;
+ private readonly runningListeners = new Set<(running: boolean) => void>();
+ private readonly boundLoop = (timestamp: number): void => this.loop(timestamp);
+
public constructor(projectId: string, options: EngineOptions = {}) {
this.world = createRuntimeWorld(projectId);
@@ -43,6 +45,19 @@ export class Engine {
return this._running;
}
+ /**
+ * Subscribe to start()/stop() transitions.
+ */
+ public onRunningChange(listener: (running: boolean) => void): () => void {
+ this.runningListeners.add(listener);
+ return () => this.runningListeners.delete(listener);
+ }
+
+ private setRunning(running: boolean): void {
+ this._running = running;
+ for (const listener of this.runningListeners) listener(running);
+ }
+
public mount(canvas: HTMLCanvasElement): void {
this.canvas = canvas;
this.world.set(RenderSurface, { canvas, ctx: canvas.getContext('2d') });
@@ -78,18 +93,18 @@ export class Engine {
private loop(timestamp: number): void {
this.step(timestamp);
- this.rafId = requestAnimationFrame(this.loop);
+ this.rafId = requestAnimationFrame(this.boundLoop);
}
public start(): void {
if (this._running) return;
- this._running = true;
+ this.setRunning(true);
this.lastTimestamp = null;
- this.rafId = requestAnimationFrame(this.loop);
+ this.rafId = requestAnimationFrame(this.boundLoop);
}
public stop(): void {
- this._running = false;
+ this.setRunning(false);
if (this.rafId !== null) {
cancelAnimationFrame(this.rafId);
this.rafId = null;
diff --git a/apps/web/src/engine/hooks/index.ts b/apps/web/src/engine/hooks/index.ts
new file mode 100644
index 00000000..ed2aa6c6
--- /dev/null
+++ b/apps/web/src/engine/hooks/index.ts
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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/. */
+
+export * from './use-active-scene';
+export * from './use-camera';
+export * from './use-running';
diff --git a/apps/web/src/engine/hooks/use-active-scene.ts b/apps/web/src/engine/hooks/use-active-scene.ts
new file mode 100644
index 00000000..a405676d
--- /dev/null
+++ b/apps/web/src/engine/hooks/use-active-scene.ts
@@ -0,0 +1,15 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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 { ActiveScene } from '@diffusionstudio/runtime';
+import { useTrait, useWorld } from '@diffusionstudio/koota-solid';
+
+import type { Entity } from 'koota';
+import type { Accessor } from 'solid-js';
+
+/** Reactive read of the world's ActiveScene trait. Write via world.set(ActiveScene, ...). */
+export function useActiveScene(): Accessor<Entity | null | undefined> {
+ const activeScene = useTrait(useWorld(), ActiveScene);
+ return () => activeScene()?.entity;
+}
diff --git a/apps/web/src/engine/hooks/use-camera.ts b/apps/web/src/engine/hooks/use-camera.ts
new file mode 100644
index 00000000..9a8ae40a
--- /dev/null
+++ b/apps/web/src/engine/hooks/use-camera.ts
@@ -0,0 +1,13 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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 { Camera, type Camera2D } from '@diffusionstudio/runtime';
+import { useTrait, useWorld } from '@diffusionstudio/koota-solid';
+
+import type { Accessor } from 'solid-js';
+
+/** Reactive read of the world's Camera trait. Write via world.set(Camera, ...). */
+export function useCamera(): Accessor<Camera2D | undefined> {
+ return useTrait(useWorld(), Camera);
+}
diff --git a/apps/web/src/engine/hooks/use-running.ts b/apps/web/src/engine/hooks/use-running.ts
new file mode 100644
index 00000000..57e22fd9
--- /dev/null
+++ b/apps/web/src/engine/hooks/use-running.ts
@@ -0,0 +1,17 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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 { createSignal, onCleanup, type Accessor } from 'solid-js';
+
+import { useEngineContext } from '../context';
+
+/** Reactive read of whether the engine's tick loop is running. */
+export function useRunning(): Accessor<boolean> {
+ const engine = useEngineContext();
+ const [running, setRunning] = createSignal(engine.running);
+
+ onCleanup(engine.onRunningChange(setRunning));
+
+ return running;
+}
diff --git a/apps/web/src/engine/index.ts b/apps/web/src/engine/index.ts
index 81b99404..04befd9d 100644
--- a/apps/web/src/engine/index.ts
+++ b/apps/web/src/engine/index.ts
@@ -3,3 +3,5 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
export * from './create-engine';
+export * from './context';
+export * from './hooks';
diff --git a/package-lock.json b/package-lock.json
index 4177542b..856e00c9 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -68,6 +68,7 @@
"@diffusionstudio/api-contract": "^0.1.0",
"@diffusionstudio/cli": "*",
"@diffusionstudio/jsx": "*",
+ "@diffusionstudio/koota-solid": "*",
"@diffusionstudio/runtime": "*",
"@kobalte/core": "^0.13.11",
"@sentry/solid": "^10.51.0",
@@ -84,6 +85,7 @@
"colord": "^2.9.3",
"cva": "^1.0.0-beta.4",
"idb": "^8.0.3",
+ "koota": "^0.6.6",
"mediabunny": "^1.32.2",
"solid-js": "^1.9.10",
"somoto": "^0.0.2",