Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor error handling and generation checks in rendering system

konstantin-paulus9b53426ea107
diff --git a/apps/web/src/engine/hud/hud-system.ts b/apps/web/src/engine/hud/hud-system.ts
index ff439c4b..e0e48970 100644
--- a/apps/web/src/engine/hud/hud-system.ts
+++ b/apps/web/src/engine/hud/hud-system.ts
@@ -4,10 +4,11 @@
import { Not, Or } from 'koota';
import {
- Active, Computed, Culled, FrameRate, Generating, Geometry, Group, Hidden,
+ Active, Computed, Culled, FrameRate, Geometry, Group, Hidden,
HitRegions, Hovering, Name, Playback, RenderSurface, Root, Selected,
- Sequential, SourceError, ChildOf,
- entityQuad, entityWorldMat, getMaskSelection, getSelectionMask, invert2D,
+ Sequential, ChildOf,
+ entityQuad, entityWorldMat, getMaskSelection, getSelectionMask, getSourceFailure,
+ invert2D, isGenerating,
multiply2D, rectToQuad, rotate2D, scale2D, store, transformPoint,
translate2D,
} from '@diffusionstudio/runtime';
@@ -106,10 +107,8 @@ function drawHoverOutlines(world: World, ctx: Ctx2D, resolution: number): void {
* It sits in the node's own rotation, one line above its top edge.
*/
function drawHeader(world: World, ctx: Ctx2D, entity: Entity, resolution: number): void {
- const errored = world.query(ChildOf(entity), SourceError);
- const generating = world.query(ChildOf(entity), Generating);
- const failure = errored.at(0)?.get(SourceError)?.value;
- const label = failure || (generating.length ? 'Generating...' : entity.get(Name)?.value);
+ const failure = getSourceFailure(entity);
+ const label = failure || (isGenerating(entity) ? 'Generating...' : entity.get(Name)?.value);
if (!label) return;
const header = getHeaderLayout(world, entity, resolution);
diff --git a/apps/web/src/engine/timeline/render/clip.ts b/apps/web/src/engine/timeline/render/clip.ts
index 943138de..f6dc2b88 100644
--- a/apps/web/src/engine/timeline/render/clip.ts
+++ b/apps/web/src/engine/timeline/render/clip.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 { Caption, CaptionType, Chars, ChildOf, ClipDragOrigin, Computed, Generating, Hidden, Name, Selected, SourceError, TrimDragOrigin, fitsChildren, getGeneratingColor, isCaption, isGroup, isText, store } from '@diffusionstudio/runtime';
+import { Caption, CaptionType, Chars, ClipDragOrigin, Computed, Hidden, Name, Selected, TrimDragOrigin, fitsChildren, getGeneratingColor, getSourceFailure, isCaption, isGenerating, isGroup, isText, store } from '@diffusionstudio/runtime';
import { getDocumentEditor } from '../../editor';
import {
@@ -56,7 +56,7 @@ export function renderClip(
pointer.scope(String(entity.id()));
const asset = getClipAsset(world, entity);
- const error = world.query(ChildOf(entity), SourceError).at(0)?.get(SourceError)?.value;
+ const error = getSourceFailure(entity);
const style = getClipStyle(entity, asset, !!error?.length);
handleBody(world, surface, entity, left, width, row, resolution);
@@ -67,7 +67,7 @@ export function renderClip(
left = framesToPixels(computed.start[entity.id()] ?? 0, resolution);
}
- const generating = world.query(ChildOf(entity), Generating).length > 0;
+ const generating = isGenerating(entity);
ctx.save();
ctx.beginPath();
diff --git a/packages/runtime/src/systems/render.ts b/packages/runtime/src/systems/render.ts
index 0687ebe3..a7bc519b 100644
--- a/packages/runtime/src/systems/render.ts
+++ b/packages/runtime/src/systems/render.ts
@@ -16,9 +16,8 @@ import {
TransitionType,
} from '../constants';
import {
- ChildOf, Hidden, Culled, Generating, Interactive, IsMask,
+ ChildOf, Hidden, Culled, Interactive, IsMask,
ClipsContent, Geometry, Group, Paint, Color, Caption, ScaleMode, Shader,
- SourceError,
BlendMode, Effect, Transition, MixedCornerRadius,
LocalTransform, WorldTransform, Computed, Cache,
Host,
@@ -29,7 +28,7 @@ import {
import { getParentNode } from '../queries/hierarchy';
import { getViewMatrix } from '../queries/camera';
import { colorToHex } from '../utils/color';
-import { FAILED_COLOR, getGeneratingColor } from '../utils/generating';
+import { FAILED_COLOR, getGeneratingColor, getSourceFailure, isGenerating } from '../utils/generating';
import { applyStrokeStyle } from '../utils/stroke';
import { renderText } from '../utils/text';
import { getTransitionWindow } from '../utils/transition';
@@ -587,10 +586,8 @@ function renderStrokes(world: World, entity: Entity): void {
* The pulse a node waiting on a generation is filled with
*/
function renderGenerating(world: World, entity: Entity): void {
- const invariants = world.query(ChildOf(entity), SourceError, Generating);
- const errored = invariants.find(invariant => invariant.get(SourceError));
- const generating = invariants.find(invariant => invariant.get(Generating));
- if (!errored && !generating) return;
+ const errored = getSourceFailure(entity) !== undefined;
+ if (!errored && !isGenerating(entity)) return;
const ctx = getCtx(world);
ctx.fillStyle = errored ? FAILED_COLOR : getGeneratingColor(world);
diff --git a/packages/runtime/src/utils/generating.ts b/packages/runtime/src/utils/generating.ts
index d6a530eb..cf67ecd7 100644
--- a/packages/runtime/src/utils/generating.ts
+++ b/packages/runtime/src/utils/generating.ts
@@ -19,9 +19,9 @@
import { cubicBezier } from 'animejs';
-import { Time } from '../traits';
+import { Cache, Generating, SourceError, Time } from '../traits';
-import type { World } from 'koota';
+import type { Entity, World } from 'koota';
const generatingEase = cubicBezier(0.52, 0.18, 0.56, 0.88);
@@ -33,6 +33,34 @@ const GEN_CYCLE = GEN_DURATION * 2; // full forward+reverse cycle
const GEN_FROM_R = 28, GEN_FROM_G = 28, GEN_FROM_B = 28;
const GEN_TO_R = 41, GEN_TO_G = 41, GEN_TO_B = 41;
+/**
+ * Whether the node is waiting on a generation. The wait sits wherever the
+ * src does: on the node itself where the paint is intrinsic (`<image>`,
+ * `<video>`, `<audio>`, `<captions>`), or on one of its paints where it is
+ * authored as a child (`<rect><imagePaint src /></rect>`).
+ */
+export function isGenerating(entity: Entity): boolean {
+ if (entity.has(Generating)) return true;
+ for (const fill of entity.get(Cache)?.fills ?? []) {
+ if (fill.has(Generating)) return true;
+ }
+ return false;
+}
+
+/**
+ * The reason the node's source failed, from wherever the src sits — the
+ * counterpart of `isGenerating`, and what a still red fill is drawn for.
+ */
+export function getSourceFailure(entity: Entity): string | undefined {
+ const own = entity.get(SourceError)?.value;
+ if (own) return own;
+ for (const fill of entity.get(Cache)?.fills ?? []) {
+ const failure = fill.get(SourceError)?.value;
+ if (failure) return failure;
+ }
+ return undefined;
+}
+
/** Where the pulse stands on the world's clock: 0 at rest, 1 fully lit. */
function generatingFactor(world: World): number {
const t = world.get(Time)?.now ?? 0;