Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor engine creation and system management

konstantin-paulus8bc0693a36da
diff --git a/apps/web/src/engine/context.tsx b/apps/web/src/engine/context.tsx
index b147a648..47d1934a 100644
--- a/apps/web/src/engine/context.tsx
+++ b/apps/web/src/engine/context.tsx
@@ -5,7 +5,7 @@
import { createContext, onCleanup, useContext, type JSX } from 'solid-js';
import { WorldProvider } from '@diffusionstudio/koota-solid';
-import { Engine, type EngineOptions } from './create-engine';
+import { createEngine, type Engine, type EngineOptions } from './create-engine';
const EngineContext = createContext<Engine>();
@@ -24,7 +24,7 @@ export interface EngineProviderProps {
* explicit instead of importing one god object.
*/
export function EngineProvider(props: EngineProviderProps): JSX.Element {
- const engine = new Engine(props.projectId, props.options);
+ const engine = createEngine(props.projectId, props.options);
onCleanup(() => engine.dispose());
return (
diff --git a/apps/web/src/engine/create-engine.ts b/apps/web/src/engine/create-engine.ts
index dd1b7cd3..428d746b 100644
--- a/apps/web/src/engine/create-engine.ts
+++ b/apps/web/src/engine/create-engine.ts
@@ -2,7 +2,7 @@
* 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 { AudioEngine, createRuntimeWorld, RenderSurface, runSystems, Time } from '@diffusionstudio/runtime';
+import { renderSystem, transformSystem, playbackSystem, motionSystem, AudioEngine, createRuntimeWorld, RenderSurface, Time } from '@diffusionstudio/runtime';
import type { RuntimeWorld } from '@diffusionstudio/runtime';
@@ -20,7 +20,7 @@ export interface EngineOptions {
* Input, HUD, and DOM-sync systems are app-side concerns layered on top by
* the host, not part of this shell.
*/
-export class Engine {
+class Engine {
readonly world: RuntimeWorld;
private canvas: HTMLCanvasElement | null = null;
@@ -88,7 +88,11 @@ export class Engine {
const delta = this.lastTimestamp === null ? 0 : timestamp - this.lastTimestamp;
this.lastTimestamp = timestamp;
this.world.set(Time, { now: timestamp, delta });
- runSystems(this.world);
+
+ playbackSystem(this.world);
+ motionSystem(this.world);
+ transformSystem(this.world);
+ renderSystem(this.world);
}
private loop(timestamp: number): void {
@@ -113,7 +117,15 @@ export class Engine {
public dispose(): void {
this.stop();
- if (this.ownsAudioContext) void this.audioContext.close();
+ if (this.ownsAudioContext) {
+ this.audioContext.close();
+ }
this.world.destroy();
}
}
+
+export function createEngine(projectId: string, options: EngineOptions = {}): Engine {
+ return new Engine(projectId, options);
+}
+
+export type { Engine };
diff --git a/packages/reconciler/src/document.ts b/packages/reconciler/src/document.ts
index 687f8a4b..9ee16901 100644
--- a/packages/reconciler/src/document.ts
+++ b/packages/reconciler/src/document.ts
@@ -23,6 +23,7 @@ import {
isText,
ItemIndex,
Name,
+ Offset,
parseColor,
Playback,
Position,
@@ -32,6 +33,7 @@ import {
getEntityChildren,
Stage,
Root,
+ Rotation,
Scene,
Selected,
Source,
@@ -237,6 +239,17 @@ export class RuntimeDocument implements ProjectDocument<HostNode> {
entity.set(Position, { [name]: toNumber(value) ?? 0 });
return;
}
+ case 'offsetX':
+ case 'offsetY': {
+ entity.add(Offset);
+ entity.set(Offset, { [name === 'offsetX' ? 'x' : 'y']: toNumber(value) ?? 0 });
+ return;
+ }
+ case 'rotation': {
+ entity.add(Rotation);
+ entity.set(Rotation, { value: toNumber(value) ?? 0 });
+ return;
+ }
case 'width':
case 'height': {
const size = toNumber(value);
diff --git a/packages/runtime/MIGRATION.md b/packages/runtime/MIGRATION.md
index c46a1726..fc19afab 100644
--- a/packages/runtime/MIGRATION.md
+++ b/packages/runtime/MIGRATION.md
@@ -45,6 +45,8 @@ Working doc for migrating the editor engine from bitecs (in `apps/web/src/compon
| Audio sync | `src/media/audio-sync.ts` | Moved near-verbatim (FFT cross-correlation over RMS envelopes; pure math + mediabunny) |
| Transcode + previews | `src/media/transcode.ts` | transcodeForTranscription/ForAnalysis, filmstripAsset, waveformAsset, stampTimestampLabel. Preview canvases became OffscreenCanvas + convertToBlob (worker-capable) instead of document.createElement + toDataURL |
| Contact sheets | `packages/encoder/src/contact-sheet.ts` | Sheets compose image-encoder captures, so they live in the encoder package. It now OWNS `MAX_FRAMES_PER_SHEET` (was `@diffusionstudio/cli/channels`; the CLI imports it from here once rewired). composeSheet is async (OffscreenCanvas) |
+| Interaction reads | `src/queries/interaction.ts` | `entityWorldMat`/`entityLocalMat`/`entityQuad`/`entityAnchor`/`entityOffset` (device pixels, from the SoA stores), `isPointerInEntity`, `getSelectionMask`/`getMaskSelection` (was `utils/interaction.ts` + `utils/coordinates.ts`). `entityWorldMat` of the stage (a top-level node's `getParentEntity`, where bitecs had null) is the view matrix, so parent-space math for roots works unchanged |
+| Interactive upkeep | `src/actions/interactive.ts` | `syncInteractiveState` (was `api/utils.ts`; roots = `ChildOf(stage)`, diffed against the current tags so it fires no redundant events), `enterEntity` (double-click drill-down), `findChildAt` |
| Seeds (types only, rest follows) | `src/utils/text.ts` (Token/Line/TokenOptions), `src/capture/format.ts` (ContainerFormat) | The remaining engine files merge into these on their slice |
Verification style used so far: `npx tsc --noEmit` in the package, plus throwaway `smoke-test.tmp.ts` in the package root run with `npm exec --yes tsx@latest -- smoke-test.tmp.ts`, deleted afterwards (no test infra in the package yet). koota caps live worlds at 16; call `world.destroy()` in test loops.
@@ -143,7 +145,7 @@ apps/web now depends on `@diffusionstudio/runtime` and `@diffusionstudio/koota-s
## Remaining work (suggested order)
-1. **App rewrite** (`apps/web`): app-side world traits (input, HUD, selection tool, store, timelineIndex signal), history via koota events (undo/redo; persistence to IndexedDB is explicitly not required for now, see ground rules, so skip the Dexie-writes/font-persistence wrapping that would otherwise imply), re-implementing `syncInteractiveState` over `ChildOf(document)` queries (`Not(ChildOf('*'))` throws in koota), re-adding the editing-surface `syncKeyframeTrack` calls the observers slice dropped, UI to `packages/koota-solid` hooks (only `useCamera`/`useActiveScene`/`useRunning` exist so far), timeline UI consumes new `TimelineNode.entity`. `utils/captions.ts` (trpc/upload/encoder-driven) stays app-side, rewired to `@diffusionstudio/encoder`. Once real content loads into the koota world, delete the now-hidden bitecs canvas and `engine/components`, `engine/api` progressively.
+1. **App rewrite** (`apps/web`): app-side world traits (store, timelineIndex signal; input/HUD are done), history via koota events (undo/redo; persistence to IndexedDB is explicitly not required for now, see ground rules, so skip the Dexie-writes/font-persistence wrapping that would otherwise imply), re-adding the editing-surface `syncKeyframeTrack` calls the observers slice dropped, UI to `packages/koota-solid` hooks (only `useCamera`/`useActiveScene`/`useRunning` exist so far), timeline UI consumes new `TimelineNode.entity`. `utils/captions.ts` (trpc/upload/encoder-driven) stays app-side, rewired to `@diffusionstudio/encoder`. Once real content loads into the koota world, delete the now-hidden bitecs canvas and `engine/components`, `engine/api` progressively.
2. **CLI**: point node capture at the runtime + encoder packages (incl. importing `MAX_FRAMES_PER_SHEET` from the encoder instead of defining it in `cli-channels.ts`).
## Layout reminders
diff --git a/packages/runtime/src/systems/index.ts b/packages/runtime/src/systems/index.ts
index 15f0ebb2..626c0744 100644
--- a/packages/runtime/src/systems/index.ts
+++ b/packages/runtime/src/systems/index.ts
@@ -4,4 +4,3 @@ export * from './motion';
export * from './gradients';
export * from './playback';
export * from './render';
-export * from './schedule';
diff --git a/packages/runtime/src/systems/schedule.ts b/packages/runtime/src/systems/schedule.ts
deleted file mode 100644
index 10929567..00000000
--- a/packages/runtime/src/systems/schedule.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-/* 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/. */
-
-// Deterministic system order for one frame, shared by the realtime tick and
-// the capture loop (which sets Time/playheads explicitly, runs this, then
-// awaits FramePromises before sampling). The app's tick wraps this with its
-// browser-only systems (input before, HUD/dom sync after).
-
-import { playbackSystem } from './playback';
-import { motionSystem } from './motion';
-import { transformSystem } from './transform';
-import { renderSystem } from './render';
-
-import type { World } from 'koota';
-
-export function runSystems(world: World): void {
- playbackSystem(world);
- motionSystem(world);
- transformSystem(world);
- renderSystem(world);
-}