🎥 Make videos programmatically with React
Reuse unchanged React commit metadata subtrees
Jonny Burger3f82ea517276
diff --git a/packages/sdk/src/install-fiber-sequence-order-observer.ts b/packages/sdk/src/install-fiber-sequence-order-observer.tsindex 7d73620895..f0bc5ef923 100644--- a/packages/sdk/src/install-fiber-sequence-order-observer.ts+++ b/packages/sdk/src/install-fiber-sequence-order-observer.ts@@ -3,6 +3,7 @@ import type {AnyComposition, TSequence} from 'remotion';import {Internals} from 'remotion';type Fiber = {+ readonly alternate?: Fiber | null;readonly child: Fiber | null;readonly memoizedProps: unknown;readonly sibling: Fiber | null;@@ -40,6 +41,28 @@ type CommittedMetadata = NonNullable<>[typeof metadataProp]>;+type FiberProjection = {+ readonly metadata: CommittedMetadata | null;+ readonly outline:+ | {readonly type: 'host'; readonly node: Element | Text}+ | {readonly type: 'portal' | 'hidden'}+ | null;+ readonly children: readonly FiberProjection[];+ readonly hasMetadata: boolean;+};++type CommittedFiberSnapshot = {+ readonly fiber: Fiber;+ readonly child: Fiber | null;+ readonly children: readonly CommittedFiberSnapshot[];+ readonly projectedChildren: readonly FiberProjection[];+ readonly projection: FiberProjection | null;+};++type FiberProjectionCache = {+ current: CommittedFiberSnapshot | null;+};+const getCommittedMetadata = (props: unknown): CommittedMetadata | null => {if (typeof props !== 'object' || props === null) {return null;@@ -51,6 +74,102 @@ const getCommittedMetadata = (props: unknown): CommittedMetadata | null => {: null;};+const projectCommittedFiberTree = (+ root: Fiber,+ cache: FiberProjectionCache | null,+) => {+ let outlineCollectionFailed = false;+ const visit = (+ fiber: Fiber,+ previous: CommittedFiberSnapshot | null,+ ): CommittedFiberSnapshot => {+ const metadata = getCommittedMetadata(fiber.memoizedProps);+ let outline: FiberProjection['outline'] = null;+ if (!outlineCollectionFailed) {+ try {+ if (fiber.tag === 22 && fiber.memoizedState !== null) {+ outline = {type: 'hidden'};+ } else if (fiber.tag === 4) {+ outline = {type: 'portal'};+ } else if (+ (fiber.tag === 5 || fiber.tag === 6) &&+ fiber.stateNode !== null+ ) {+ outline = {type: 'host', node: fiber.stateNode as Element | Text};+ }+ } catch {+ outlineCollectionFailed = true;+ }+ }++ let childSnapshots: readonly CommittedFiberSnapshot[];+ let projectedChildren: readonly FiberProjection[];+ if (previous !== null && previous.child === fiber.child) {+ // React clones the direct child list before rendering descendants. A+ // retained child pointer is its own bailout check. Keep snapshots in the+ // last committed tree, not a lifetime Fiber cache: alternates are recycled.+ childSnapshots = previous.children;+ projectedChildren = previous.projectedChildren;+ } else {+ const previousChildren = new Map(+ previous?.children.map((entry) => [entry.fiber, entry]),+ );+ const snapshots: CommittedFiberSnapshot[] = [];+ const next: FiberProjection[] = [];+ let {child} = fiber;+ while (child !== null) {+ const childSnapshot = visit(+ child,+ previousChildren.get(child) ??+ (child.alternate+ ? (previousChildren.get(child.alternate) ?? null)+ : null),+ );+ snapshots.push(childSnapshot);+ if (childSnapshot.projection !== null) {+ next.push(childSnapshot.projection);+ }++ child = child.sibling;+ }++ childSnapshots = snapshots;+ projectedChildren = next;+ }++ let children = projectedChildren;+ if (outline !== null) {+ // Host nodes end the incoming DOM frontier; portals and hidden trees+ // cannot contribute to it. Only nested registration scopes remain useful.+ children = children.filter((child) => child.hasMetadata);+ }++ const hasMetadata =+ metadata !== null || children.some((child) => child.hasMetadata);+ const projection: FiberProjection | null =+ metadata === null && outline?.type !== 'host' && children.length === 0+ ? null+ : metadata === null && outline === null && children.length === 1+ ? children[0]+ : {metadata, outline, children, hasMetadata};+ return {+ fiber,+ child: fiber.child,+ children: childSnapshots,+ projectedChildren,+ projection,+ };+ };++ const snapshot = visit(root, cache?.current ?? null);+ if (cache !== null) {+ // Geometry failures must retry from live Fibers on the next commit.+ cache.current = outlineCollectionFailed ? null : snapshot;+ }++ return {projection: snapshot.projection, outlineCollectionFailed};+};+type CommittedSequenceRegistration = {readonly onCommit: (sequences: readonly TSequence[],@@ -115,7 +234,9 @@ export const collectCommitOrderFromFiber = (CommittedCompositionRegistration> | null;} | null = null,+ projectionCache: FiberProjectionCache | null = null,) => {+ const projected = projectCommittedFiberTree(root.current, projectionCache);const compositionRegistrations = compositionRegistry?.registrations ?? null;const previousCompositionRegistrations =compositionRegistry?.previous ?? null;@@ -123,7 +244,7 @@ export const collectCommitOrderFromFiber = (RefObject<Element | null>,(Element | Text)[]>();- let outlineCollectionFailed = false;+ let {outlineCollectionFailed} = projected;const sequencesByManager = new Map<string,{@@ -159,26 +280,13 @@ export const collectCommitOrderFromFiber = (>();const visit = (- fiber: Fiber,+ fiber: FiberProjection,currentSequenceManagerId: string | null,currentCompositionManagerId: string | null,outlineCollectors: readonly (Element | Text)[][] | null,) => {- const metadata = getCommittedMetadata(fiber.memoizedProps);- if (outlineCollectors === null && metadata === null) {- // No metadata or geometry starts here inside an already hidden tree.- // Keep visiting descendants for registrations without rebuilding outlines.- let hiddenChild = fiber.child;- while (hiddenChild !== null) {- visit(- hiddenChild,- currentSequenceManagerId,- currentCompositionManagerId,- null,- );- hiddenChild = hiddenChild.sibling;- }-+ const {metadata} = fiber;+ if (!fiber.hasMetadata && !outlineCollectors?.length) {return;}@@ -268,13 +376,12 @@ export const collectCommitOrderFromFiber = (try {// A portal ends the current DOM group, but can contain new sequences.// Hidden Offscreen trees must not contribute geometry, including new groups.- const {tag} = fiber;+ const {outline} = fiber;const skipOutline =- outlineCollectors === null ||- (tag === 22 && fiber.memoizedState !== null);+ outlineCollectors === null || outline?.type === 'hidden';childOutlineCollectors = skipOutline? null- : tag === 4+ : outline?.type === 'portal'? []: outlineCollectors;if (metadata?.type === 'sequence') {@@ -291,11 +398,10 @@ export const collectCommitOrderFromFiber = (if (childOutlineCollectors !== null &&childOutlineCollectors.length > 0 &&- (tag === 5 || tag === 6) &&- fiber.stateNode !== null+ outline?.type === 'host') {for (const collector of childOutlineCollectors) {- collector.push(fiber.stateNode as Element | Text);+ collector.push(outline.node);}// Only first-level DOM nodes belong to this group. Keep traversing@@ -311,19 +417,20 @@ export const collectCommitOrderFromFiber = (}}- let {child} = fiber;- while (child !== null) {+ for (const child of fiber.children) {visit(child,sequenceManagerId,compositionManagerId,childOutlineCollectors,);- child = child.sibling;}};- visit(root.current, null, null, []);+ if (projected.projection !== null) {+ visit(projected.projection, null, null, []);+ }+for (const [ref, nodes] of outlineNodesByRef) {try {Internals.SequenceOutlineInternals.setNodes(ref, nodes);@@ -404,11 +511,16 @@ export const installFiberCommitOrderObserver = (FiberRoot,Map<string, CommittedCompositionRegistration>>();+ const projectionCachesByRoot = new WeakMap<FiberRoot, FiberProjectionCache>();hook.onCommitFiberRoot = function (...args) {let order: ReturnType<typeof collectCommitOrderFromFiber> | null = null;try {const [, root] = args;+ const projectionCache = projectionCachesByRoot.get(root) ?? {+ current: null,+ };+ projectionCachesByRoot.set(root, projectionCache);const registrations = new Map<string, CommittedSequenceRegistration>();const previousRegistrations = registrationsByRoot.get(root);const compositionRegistrations = new Map<@@ -425,6 +537,7 @@ export const installFiberCommitOrderObserver = (registrations: compositionRegistrations,previous: previousCompositionRegistrations ?? null,},+ projectionCache,);registrationsByRoot.set(root, registrations);compositionRegistrationsByRoot.set(root, compositionRegistrations);@@ -468,6 +581,7 @@ export const installFiberCommitOrderObserver = (} catch {// A registration collection or delivery failure permanently selects effect// fallback for this hook. Retrying registration would risk repeated teardown.+ projectionCachesByRoot.delete(args[1]);hook[Internals.CommittedMetadataInternals.failureMarker] = true;order = null;try {