Skip to content

The VS Code of video editing, built for agents.

TypeScript

Implement ticker system for playback management

konstantin-paulus9369730c9a3c
diff --git a/packages/reconciler/src/document.ts b/packages/reconciler/src/document.ts
index f7622915..9b921beb 100644
--- a/packages/reconciler/src/document.ts
+++ b/packages/reconciler/src/document.ts
@@ -3,8 +3,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { Active, AdjustmentLayer, Animation, AnimationPhase, AnimationType, appendChild, AssetId, Audio, Background, bindAsset, BlendMode, BlendModeType, Blur, Caption, CaptionAlign, CaptionType, Chars, ClipHeight, ClipsContent, CornerRadius, createEntity, DEFAULT_BACKGROUND, Color, ColorStop, Delay, Effect, EffectType, Expanded, FontStyle, FrameRate, Generating, GenerationRequest, Loop, LoadRequest, Geometry, GeometryType, getEntityTree, getParentEntity, getParentNode, Hidden, Host, IsMask, isText, ItemIndex, Keyframe, KeyframeTrack, MixedCornerRadius, Muted, Name, Offset, Opacity, Paint, PaintType, parseColor, PendingSource, Playback, PlaybackRate, Position, removeChild, RenderSurface, resizeEntity, Scale, ScaleMode, ScaleModeType, secondsToFrames, getAsset, getEntityChildren, Group, Sequential, Shader, Size, Stage, Root, Rotation, Scene, Selected, Shadow, Source, SourceError, SourceFrameRate, SourceModifiers, hasModifier, setCameraMatrix, Stroke, StrokeCap, StrokeJoin, StrokeStyle, TextAlign, TextBaseline, TextCase, TextRange, TextStyle, TranscriptionRequest, Transition, TransitionType, Trim, UniformScale, Volume, Workarea } from '@diffusionstudio/runtime';
+import { Active, AdjustmentLayer, Animation, AnimationPhase, AnimationType, appendChild, AssetId, Audio, Background, bindAsset, BlendMode, BlendModeType, Blur, Caption, CaptionAlign, CaptionType, Chars, ClipHeight, ClipsContent, Computed, CornerRadius, createEntity, DEFAULT_BACKGROUND, Color, ColorStop, Delay, Effect, EffectType, Expanded, FontStyle, FrameRate, Generating, GenerationRequest, getActiveEntity, Loop, LoadRequest, Geometry, GeometryType, getEntityTree, getParentEntity, getParentNode, Hidden, Host, IsMask, isText, ItemIndex, Keyframe, KeyframeTrack, MixedCornerRadius, Muted, Name, Offset, Opacity, Paint, PaintType, parseColor, PendingSource, Playback, PlaybackRate, Position, removeChild, RenderSurface, resizeEntity, Scale, ScaleMode, ScaleModeType, secondsToFrames, getAsset, getEntityChildren, Group, Sequential, Shader, Size, Stage, Root, Rotation, Scene, Selected, Shadow, Source, SourceError, SourceFrameRate, SourceModifiers, hasModifier, setCameraMatrix, Stroke, StrokeCap, StrokeJoin, StrokeStyle, TextAlign, TextBaseline, TextCase, TextRange, TextStyle, TranscriptionRequest, Transition, TransitionType, Trim, UniformScale, Volume, Workarea } from '@diffusionstudio/runtime';
import { LOOP_ATTR, parseTime, SOURCE_ATTR } from '@diffusionstudio/jsx';
+import { createSignal } from 'solid-js';
import { SVGElements } from 'solid-js/web';
import { IsExcluded } from 'koota';
@@ -12,7 +13,7 @@ import type { CameraMatrix, PropertyPath, SceneNode } from '@diffusionstudio/run
import type { AnimatableProperty, AssetRef } from '@diffusionstudio/jsx';
import type { Entity, World } from 'koota';
-import type { ProjectDocument } from './host';
+import type { ProjectDocument, ProjectTick } from './host';
/** Props that address or wire an element rather than describe it. */
const UNAUTHORED_PROPS: ReadonlySet<string> = new Set([SOURCE_ATTR, LOOP_ATTR, 'children', 'ref']);
@@ -1571,6 +1572,52 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {
this.destroySubtree(node);
}
+ // The timeline clock behind `useTicker`. One Solid signal per document;
+ // the equality check keeps a paused scene from propagating at all.
+ private readonly ticker = createSignal<ProjectTick>(
+ { time: 0, frame: 0, delta: 0, playing: false },
+ { equals: (a, b) => a.time === b.time && a.frame === b.frame && a.delta === b.delta && a.playing === b.playing },
+ );
+ private lastTickTime: number | null = null;
+
+ public tick(): ProjectTick {
+ return this.ticker[0]();
+ }
+
+ /**
+ * Reads the playhead into the ticker signal. Called by the playback system
+ * once per tick (through the world's `Tickers` set — see `mount`), so
+ * a project's memos re-run before motion and render look at the world.
+ */
+ public advanceTicker(): void {
+ const target = this.tickTarget();
+ const computed = target?.get(Computed);
+ const time = computed?.localTimeInSeconds ?? 0;
+ const delta = this.lastTickTime === null ? 0 : time - this.lastTickTime;
+ this.lastTickTime = time;
+ this.ticker[1]({
+ time,
+ frame: computed?.localTime ?? 0,
+ delta,
+ playing: target?.get(Playback)?.playing ?? false,
+ });
+ }
+
+ /**
+ * The entity whose playhead the ticker reads: the nearest Playback carrier
+ * at or above the active scene. Resolved per tick rather than cached — the
+ * active scene changes with the view, and a capture world (which activates
+ * nothing) answers with its one Playback root instead.
+ */
+ private tickTarget(): Entity | null {
+ let current = getActiveEntity(this.world);
+ while (current?.isAlive()) {
+ if (current.has(Playback)) return current;
+ current = getParentEntity(current);
+ }
+ return this.world.queryFirst(Playback) ?? null;
+ }
+
public getParentNode(node: SceneNode): SceneNode | undefined {
return node.parent ?? undefined;
}
diff --git a/packages/reconciler/src/mount.ts b/packages/reconciler/src/mount.ts
index 11fe66d9..2d89d281 100644
--- a/packages/reconciler/src/mount.ts
+++ b/packages/reconciler/src/mount.ts
@@ -2,6 +2,8 @@
* 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 { Tickers } from '@diffusionstudio/runtime';
+
import { createRuntimeDocument } from './document';
import { evaluate } from './evaluate';
import { renderProject } from './renderer';
@@ -19,6 +21,10 @@ export interface Mount {
* mounted project (dispose that one first — the stage is a singleton, so
* two mounts would render into each other); nothing is left behind in the
* world when it throws.
+ *
+ * The mount stays live: it puts the document's ticker advance into the
+ * world's `Tickers`, and the playback system fires it once per tick, which
+ * is what `useTicker` subscribes to.
*/
export function mount(code: string, world: World): Mount {
const component = evaluate(code);
@@ -32,8 +38,12 @@ export function mount(code: string, world: World): Mount {
throw error;
}
+ const advance = () => document.advanceTicker();
+ world.get(Tickers)?.add(advance);
+
return {
dispose() {
+ world.get(Tickers)?.delete(advance);
// Solid's universal render disposer only drops the reactive graph;
// the document owns the entities.
dispose();
diff --git a/packages/runtime/src/systems/playback.ts b/packages/runtime/src/systems/playback.ts
index 32b8cf4f..09b0c3c6 100644
--- a/packages/runtime/src/systems/playback.ts
+++ b/packages/runtime/src/systems/playback.ts
@@ -17,7 +17,7 @@ import {
Sequential, Transition, Playback, Workarea,
AudioPlayback, Computed,
AudioDecoderHandle, AudioBusHandle, Host,
- Mode, FrameRate, Time, AudioEngine, FramePromises, Mounts,
+ Mode, FrameRate, Time, AudioEngine, FramePromises, Tickers,
Root,
} from '../traits';
import { getParentNode } from '../queries/hierarchy';
@@ -436,5 +436,5 @@ export function playbackSystem(world: World): void {
}
}
- world.get(Mounts)?.advance();
+ for (const tick of world.get(Tickers) ?? []) tick();
}
diff --git a/packages/runtime/src/traits/world.ts b/packages/runtime/src/traits/world.ts
index e15325c0..356f5269 100644
--- a/packages/runtime/src/traits/world.ts
+++ b/packages/runtime/src/traits/world.ts
@@ -4,8 +4,6 @@
import { trait, type Entity, type World } from 'koota';
-import { createLiveMounts } from '../utils/live-mounts';
-
import type { FontSource } from '../fonts/types';
import type { AssetLibrary } from '@diffusionstudio/assets';
import type { GenAi } from '../ai';
@@ -72,8 +70,11 @@ export const Library = trait(() => null as AssetLibrary | null);
// without one, such sources stay pending and never resolve.
export const Ai = trait(() => null as GenAi | null);
-// Live JSX mount registry; running graphs advance once per frame.
-export const Mounts = trait(() => createLiveMounts());
+// Per-tick callbacks. A mounted project adds one to feed its `useTicker`
+// clock; the playback system fires the set once per tick, after playheads
+// advance. Held on the world so anything that runs the systems (the editor
+// loop, the encoder) drives the mounts without knowing them.
+export const Tickers = trait(() => new Set<() => void>());
// Async barrier for offline capture: systems push decoder/host readiness
// promises here; the encoder awaits and clears them before sampling a frame.
diff --git a/packages/runtime/src/utils/index.ts b/packages/runtime/src/utils/index.ts
index 91e2e026..74206988 100644
--- a/packages/runtime/src/utils/index.ts
+++ b/packages/runtime/src/utils/index.ts
@@ -4,7 +4,6 @@ export * from './async';
export * from './color';
export * from './generating';
export * from './text';
-export * from './live-mounts';
export * from './sort';
export * from './stroke';
export * from './text-motion';
diff --git a/packages/runtime/src/utils/live-mounts.ts b/packages/runtime/src/utils/live-mounts.ts
deleted file mode 100644
index 7caadf68..00000000
--- a/packages/runtime/src/utils/live-mounts.ts
+++ /dev/null
@@ -1,54 +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/. */
-
-/**
- * JSX mounts keep their Solid reactive graph running after
- * the mount lands, so signals, effects, and timers keep driving the mounted
- * entities. A run stays alive until the world is
- * disposed or a later mount claims one of its root keys: the keyed root
- * replacement already deletes its entities, so the superseded graph is
- * disposed with them. Root keys are read live rather than snapshotted, so
- * roots a running graph adds or removes later still reconcile.
- */
-export type LiveMount = {
- /** Identity shared by every entity of this mount; unique per running graph. */
- mountId: string;
- rootKeys: () => string[];
- advance: () => void;
- dispose: () => void;
-};
-
-export type LiveMounts = ReturnType<typeof createLiveMounts>;
-
-export function createLiveMounts() {
- const mounts = new Set<LiveMount>();
- return {
- advance(): void {
- for (const mount of mounts) {
- mount.advance();
- }
- },
- hasMount(mountId: string): boolean {
- for (const mount of mounts) {
- if (mount.mountId === mountId) return true;
- }
- return false;
- },
- supersede(keys: string[]): void {
- for (const mount of mounts) {
- if (keys.some((key) => mount.rootKeys().includes(key))) {
- mount.dispose();
- mounts.delete(mount);
- }
- }
- },
- register(mount: LiveMount): void {
- mounts.add(mount);
- },
- disposeAll(): void {
- for (const mount of mounts) mount.dispose();
- mounts.clear();
- },
- };
-}
diff --git a/packages/runtime/src/world/create-world.ts b/packages/runtime/src/world/create-world.ts
index 98771a21..425ad6b4 100644
--- a/packages/runtime/src/world/create-world.ts
+++ b/packages/runtime/src/world/create-world.ts
@@ -19,7 +19,7 @@ import {
Fonts,
Library,
Ai,
- Mounts,
+ Tickers,
FramePromises,
HitRegions,
} from '../traits/world';
@@ -42,7 +42,7 @@ export function createRuntimeWorld(projectId: string) {
Fonts,
Library,
Ai,
- Mounts,
+ Tickers,
FramePromises,
HitRegions,
Tool,