2026-08-12 12:17:06 +02:00
|
|
|
/* 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 { store } from './store';
|
|
|
|
|
import {
|
2026-08-16 11:15:03 +02:00
|
|
|
ChildOf, Culled, Sequential, Group, Scene, Audio, Paint, AssetId,
|
2026-08-24 16:32:06 +02:00
|
|
|
Delay, Trim, PlaybackRate, SourceFrameRate, Keyframe, ItemIndex,
|
2026-08-12 12:17:06 +02:00
|
|
|
Position, Offset, Rotation, Scale, UniformScale, Skew, Anchor, Flip,
|
2026-08-18 16:55:01 +02:00
|
|
|
Opacity, Color, Blur, Volume, Effect, CornerRadius, MixedCornerRadius,
|
2026-08-18 22:49:27 +02:00
|
|
|
ColorStop, StrokeStyle, Size, Computed, Active, Stage, IsMask,
|
2026-08-22 20:21:59 +02:00
|
|
|
ImageDecoderHandle, VideoDecoderHandle,
|
2026-08-19 14:22:18 +02:00
|
|
|
AudioDecoderHandle, CaptionDecoderHandle, WaveformHandle,
|
2026-08-23 16:22:17 +02:00
|
|
|
ShaderHostHandle, AudioBusHandle,
|
2026-08-12 12:17:06 +02:00
|
|
|
} from '../traits';
|
|
|
|
|
import { getParentEntity } from '../queries/hierarchy';
|
2026-08-19 23:59:09 +02:00
|
|
|
import { evictFromCaches, rebuildCaches, refileMask } from '../actions/cache';
|
2026-08-25 09:30:24 +02:00
|
|
|
import { syncStagePlayback } from '../actions/playback';
|
2026-08-16 11:15:03 +02:00
|
|
|
import { disposeDecoders, disconnectAudioBus } from '../media/dispose';
|
2026-08-12 12:17:06 +02:00
|
|
|
import {
|
|
|
|
|
reactToChildAttached, reactToChildDetached, reactToAssetChange,
|
|
|
|
|
reactToPaintChange, recomputeEntityTimeRange, propagateTimeRangeDown,
|
2026-08-17 09:36:07 +02:00
|
|
|
bubbleTimeRangeUp,
|
2026-08-12 12:17:06 +02:00
|
|
|
} from '../actions/timing';
|
|
|
|
|
import { propagateSize, resolveConstraintOffsets } from '../actions/resize';
|
2026-08-19 17:14:17 +02:00
|
|
|
import { resetAnimatedValues } from '../systems/motion';
|
2026-08-12 12:17:06 +02:00
|
|
|
|
|
|
|
|
import type { Entity, Trait, World } from 'koota';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wire the runtime invariants into a world. Returns a disposer (worlds are
|
|
|
|
|
* normally destroyed outright, but tests recycling the 16-world budget can
|
|
|
|
|
* unhook explicitly).
|
|
|
|
|
*/
|
|
|
|
|
export function observeWorld(world: World): () => void {
|
|
|
|
|
const subs: (() => void)[] = [];
|
|
|
|
|
|
|
|
|
|
// ── Structure ─────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
subs.push(world.onAdd(ChildOf('*'), (child, parent) => {
|
|
|
|
|
rebuildCaches(world, child, parent);
|
|
|
|
|
propagateSize(world, child);
|
|
|
|
|
resolveConstraintOffsets(world, child);
|
2026-08-12 12:38:31 +02:00
|
|
|
disconnectAudioBus(world, child);
|
2026-08-12 12:17:06 +02:00
|
|
|
reactToChildAttached(world, child);
|
2026-08-25 09:30:24 +02:00
|
|
|
syncStagePlayback(child);
|
|
|
|
|
syncStagePlayback(parent);
|
2026-08-12 12:17:06 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// On re-target (and destroy) the remove event fires while the child is
|
|
|
|
|
// still present in the old parent's queries, so it is excluded by hand.
|
2026-08-16 11:15:03 +02:00
|
|
|
// Destroy cascades parent-first through ChildOf (autoDestroy 'orphan'),
|
|
|
|
|
// so a descendant's parent may already be gone: nothing to rebuild then.
|
2026-08-19 23:59:09 +02:00
|
|
|
// The eviction follows the rebuild rather than replacing it: a destroy has
|
|
|
|
|
// already taken the child's traits, so the rebuild files nothing and the
|
|
|
|
|
// child would stay in the lists it was in.
|
2026-08-12 12:17:06 +02:00
|
|
|
subs.push(world.onRemove(ChildOf('*'), (child, parent) => {
|
2026-08-12 12:38:31 +02:00
|
|
|
disconnectAudioBus(world, child);
|
2026-08-16 11:15:03 +02:00
|
|
|
if (!world.has(parent)) return;
|
|
|
|
|
rebuildCaches(world, child, parent, child);
|
2026-08-19 23:59:09 +02:00
|
|
|
evictFromCaches(world, child, parent);
|
2026-08-16 11:15:03 +02:00
|
|
|
reactToChildDetached(world, child);
|
2026-08-25 09:30:24 +02:00
|
|
|
syncStagePlayback(parent);
|
2026-08-12 12:17:06 +02:00
|
|
|
}));
|
|
|
|
|
|
2026-08-16 11:15:03 +02:00
|
|
|
// Live media handles are released with their trait. Destroy removes every
|
|
|
|
|
// trait of every entity in the subtree, so this covers deletion; the
|
|
|
|
|
// subscriber runs before the store slot is cleared, so the value is still
|
|
|
|
|
// readable.
|
|
|
|
|
subs.push(world.onRemove(ImageDecoderHandle, (e) => e.get(ImageDecoderHandle)?.dispose()));
|
|
|
|
|
subs.push(world.onRemove(VideoDecoderHandle, (e) => e.get(VideoDecoderHandle)?.dispose()));
|
|
|
|
|
subs.push(world.onRemove(AudioDecoderHandle, (e) => e.get(AudioDecoderHandle)?.reset()));
|
|
|
|
|
subs.push(world.onRemove(CaptionDecoderHandle, (e) => e.get(CaptionDecoderHandle)?.dispose()));
|
2026-08-19 14:22:18 +02:00
|
|
|
subs.push(world.onRemove(WaveformHandle, (e) => e.get(WaveformHandle)?.dispose()));
|
2026-08-16 11:15:03 +02:00
|
|
|
subs.push(world.onRemove(ShaderHostHandle, (e) => e.get(ShaderHostHandle)?.dispose()));
|
|
|
|
|
subs.push(world.onRemove(AudioBusHandle, (e) => e.get(AudioBusHandle)?.disconnect()));
|
2026-08-12 12:38:31 +02:00
|
|
|
|
|
|
|
|
// Off-screen entities release their decoders (frame caches are the bulk
|
|
|
|
|
// of media memory); the next resolve recreates them.
|
|
|
|
|
subs.push(world.onAdd(Culled, (entity) => {
|
|
|
|
|
disposeDecoders(world, entity);
|
2026-08-12 12:17:06 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Keyframes re-sort by frame, siblings re-sort by index.
|
|
|
|
|
subs.push(world.onChange(Keyframe, (entity) => {
|
|
|
|
|
rebuildCaches(world, entity, getParentEntity(entity));
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
subs.push(world.onChange(ItemIndex, (entity) => {
|
|
|
|
|
rebuildCaches(world, entity, getParentEntity(entity));
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// ── Model invariants ──────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
// A sequence is a group without spatial identity of its own.
|
|
|
|
|
subs.push(world.onAdd(Sequential, (entity) => {
|
|
|
|
|
entity.add(Group);
|
|
|
|
|
entity.remove(Position, Offset, Rotation, Scale, Skew, Anchor, Flip);
|
|
|
|
|
}));
|
|
|
|
|
|
2026-08-18 22:49:27 +02:00
|
|
|
const refile = (mask: boolean) => (entity: Entity) => {
|
|
|
|
|
const parent = getParentEntity(entity);
|
|
|
|
|
if (parent === null || !world.has(parent)) return;
|
|
|
|
|
refileMask(world, entity, parent, mask);
|
|
|
|
|
};
|
|
|
|
|
subs.push(world.onAdd(IsMask, refile(true)));
|
|
|
|
|
subs.push(world.onRemove(IsMask, refile(false)));
|
|
|
|
|
|
2026-08-17 15:48:08 +02:00
|
|
|
// One active entity per world, and only a root.
|
|
|
|
|
subs.push(world.onAdd(Active, (entity) => {
|
|
|
|
|
for (const other of [...world.query(Active)]) {
|
|
|
|
|
if (other !== entity) {
|
|
|
|
|
other.remove(Active);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
subs.push(world.onAdd(ChildOf('*'), (child, parent) => {
|
|
|
|
|
if (child.has(Active) && !parent.has(Stage)) {
|
|
|
|
|
child.remove(Active);
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
2026-08-12 12:17:06 +02:00
|
|
|
// ── Time ranges ───────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
const recomputeAndBubble = (entity: Entity) => {
|
|
|
|
|
recomputeEntityTimeRange(world, entity);
|
|
|
|
|
bubbleTimeRangeUp(world, entity);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
subs.push(world.onAdd(Group, recomputeAndBubble));
|
|
|
|
|
subs.push(world.onAdd(Scene, recomputeAndBubble));
|
|
|
|
|
subs.push(world.onAdd(Audio, recomputeAndBubble));
|
|
|
|
|
|
|
|
|
|
subs.push(world.onChange(Paint, (entity) => {
|
|
|
|
|
reactToPaintChange(world, entity);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
subs.push(world.onChange(AssetId, (entity) => {
|
|
|
|
|
reactToAssetChange(world, entity);
|
|
|
|
|
}));
|
|
|
|
|
|
2026-08-18 16:09:54 +02:00
|
|
|
subs.push(world.onRemove(AssetId, (entity) => {
|
2026-08-22 20:21:59 +02:00
|
|
|
reactToAssetChange(world, entity, AssetId);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// The rate a frames directory plays at is the whole of how long it is, so a
|
|
|
|
|
// change to it is a change of source length — the same recompute a new
|
|
|
|
|
// asset gets, not the propagate the other authored time traits get.
|
|
|
|
|
subs.push(world.onAdd(SourceFrameRate, (entity) => {
|
|
|
|
|
reactToAssetChange(world, entity);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
subs.push(world.onChange(SourceFrameRate, (entity) => {
|
|
|
|
|
reactToAssetChange(world, entity);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
subs.push(world.onRemove(SourceFrameRate, (entity) => {
|
|
|
|
|
reactToAssetChange(world, entity, SourceFrameRate);
|
2026-08-18 16:09:54 +02:00
|
|
|
}));
|
|
|
|
|
|
2026-08-12 12:17:06 +02:00
|
|
|
const propagateAndBubble = (entity: Entity) => {
|
|
|
|
|
propagateTimeRangeDown(world, entity);
|
|
|
|
|
bubbleTimeRangeUp(world, entity);
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-24 16:32:06 +02:00
|
|
|
for (const trait of [Delay, Trim, PlaybackRate]) {
|
2026-08-17 09:36:07 +02:00
|
|
|
subs.push(world.onAdd(trait, propagateAndBubble));
|
|
|
|
|
subs.push(world.onChange(trait, propagateAndBubble));
|
|
|
|
|
subs.push(world.onRemove(trait, (entity) => {
|
|
|
|
|
propagateTimeRangeDown(world, entity, trait);
|
|
|
|
|
bubbleTimeRangeUp(world, entity);
|
|
|
|
|
}));
|
|
|
|
|
}
|
2026-08-12 12:17:06 +02:00
|
|
|
|
|
|
|
|
// ── Authored → Computed mirrors ───────────────────────────
|
|
|
|
|
// Base values for entities the motion system skips (no animation data);
|
|
|
|
|
// animated entities re-derive them every frame via resetAnimatedValues.
|
|
|
|
|
// Registered on add and change so both spawn-with-value and add-then-set
|
2026-08-19 17:14:17 +02:00
|
|
|
// arrive in Computed. Removal re-derives from what is left (the trait
|
|
|
|
|
// being removed counts as gone), so an unset prop falls back to its
|
|
|
|
|
// default on the canvas rather than holding the last value.
|
2026-08-12 12:17:06 +02:00
|
|
|
|
2026-08-19 17:14:17 +02:00
|
|
|
const mirror = (trait: Trait, apply: (entity: Entity) => void, onRemove = true) => {
|
2026-08-12 12:17:06 +02:00
|
|
|
subs.push(world.onAdd(trait, apply));
|
|
|
|
|
subs.push(world.onChange(trait, apply));
|
2026-08-19 17:14:17 +02:00
|
|
|
if (onRemove) subs.push(world.onRemove(trait, (entity) => resetAnimatedValues(world, entity, trait)));
|
2026-08-12 12:17:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mirror(Position, (entity) => {
|
|
|
|
|
const computed = store(world, Computed);
|
|
|
|
|
const { x, y } = entity.get(Position)!;
|
|
|
|
|
computed.positionX[entity.id()] = x;
|
|
|
|
|
computed.positionY[entity.id()] = y;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(Offset, (entity) => {
|
|
|
|
|
const computed = store(world, Computed);
|
|
|
|
|
const { x, y } = entity.get(Offset)!;
|
|
|
|
|
computed.offsetX[entity.id()] = x;
|
|
|
|
|
computed.offsetY[entity.id()] = y;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(Rotation, (entity) => {
|
|
|
|
|
store(world, Computed).rotation[entity.id()] = entity.get(Rotation)!.value;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(Scale, (entity) => {
|
|
|
|
|
const computed = store(world, Computed);
|
|
|
|
|
const { x, y } = entity.get(Scale)!;
|
|
|
|
|
computed.scaleX[entity.id()] = x;
|
|
|
|
|
computed.scaleY[entity.id()] = y;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(UniformScale, (entity) => {
|
|
|
|
|
const computed = store(world, Computed);
|
|
|
|
|
const { value } = entity.get(UniformScale)!;
|
|
|
|
|
computed.scaleX[entity.id()] = value;
|
|
|
|
|
computed.scaleY[entity.id()] = value;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(Skew, (entity) => {
|
|
|
|
|
const computed = store(world, Computed);
|
|
|
|
|
const { x, y } = entity.get(Skew)!;
|
|
|
|
|
computed.skewX[entity.id()] = x;
|
|
|
|
|
computed.skewY[entity.id()] = y;
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-18 16:55:01 +02:00
|
|
|
mirror(Opacity, (entity) => {
|
|
|
|
|
store(world, Computed).opacity[entity.id()] = entity.get(Opacity)!.value;
|
2026-08-12 12:17:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(Color, (entity) => {
|
|
|
|
|
store(world, Computed).color[entity.id()] = entity.get(Color)!.value;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(Blur, (entity) => {
|
|
|
|
|
store(world, Computed).blur[entity.id()] = entity.get(Blur)!.value;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(Volume, (entity) => {
|
|
|
|
|
store(world, Computed).volume[entity.id()] = entity.get(Volume)!.value;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(Effect, (entity) => {
|
|
|
|
|
store(world, Computed).value[entity.id()] = entity.get(Effect)!.value;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(CornerRadius, (entity) => {
|
|
|
|
|
store(world, Computed).cornerRadius[entity.id()] = entity.get(CornerRadius)!.value;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(MixedCornerRadius, (entity) => {
|
|
|
|
|
const computed = store(world, Computed);
|
|
|
|
|
const radius = entity.get(MixedCornerRadius)!;
|
|
|
|
|
computed.cornerRadiusTopLeft[entity.id()] = radius.topLeft;
|
|
|
|
|
computed.cornerRadiusTopRight[entity.id()] = radius.topRight;
|
|
|
|
|
computed.cornerRadiusBottomRight[entity.id()] = radius.bottomRight;
|
|
|
|
|
computed.cornerRadiusBottomLeft[entity.id()] = radius.bottomLeft;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(ColorStop, (entity) => {
|
2026-08-18 16:55:01 +02:00
|
|
|
store(world, Computed).stopOffset[entity.id()] = entity.get(ColorStop)!.offset;
|
2026-08-12 12:17:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mirror(StrokeStyle, (entity) => {
|
|
|
|
|
store(world, Computed).strokeWidth[entity.id()] = entity.get(StrokeStyle)!.width;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Size flows into Computed via propagation (descendants owning a Size
|
Remove deprecated `bitecs` dependency and clean up engine components
- Removed the `bitecs` package from `package-lock.json` and `package.json` as it is no longer needed.
- Deleted unused engine components and API files, including `constants.ts`, `index.ts`, `selection.ts`, `thumbnail.ts`, `assets.ts`, `base.ts`, `clipboard.ts`, `components.ts`, `engine.ts`, `entities.ts`, `events.ts`, `folders.ts`, and `frame.ts`, to streamline the codebase.
- Updated the `Canvas` component to remove references to the deprecated `bitecs` canvas.
- Refactored import paths in `projects-view.tsx` to align with the new structure.
2026-08-24 09:04:13 +02:00
|
|
|
// re-derive too). Deserialize is a plain set, so the propagation rides on
|
|
|
|
|
// the event rather than on whatever restored the size.
|
2026-08-12 12:17:06 +02:00
|
|
|
mirror(Size, (entity) => {
|
|
|
|
|
propagateSize(world, entity);
|
2026-08-19 17:14:17 +02:00
|
|
|
}, false);
|
2026-08-12 12:17:06 +02:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
for (const unsubscribe of subs) unsubscribe();
|
|
|
|
|
};
|
|
|
|
|
}
|