Skip to content

🎥 Make videos programmatically with React

Isolate outline and order failures from commit registration

Jonny Burgerbc2f111ac0a5
diff --git a/packages/core/src/SequenceManager.tsx b/packages/core/src/SequenceManager.tsx
index fcde65d99b..60b41639ec 100644
--- a/packages/core/src/SequenceManager.tsx
+++ b/packages/core/src/SequenceManager.tsx
@@ -880,6 +880,7 @@ export const SequenceManagerProvider: React.FC<{
const onCommitRootSequences = useCallback(
(sequences: readonly TSequence[], sequenceIds: readonly string[]) => {
if (!rootObservationFailedRef.current) {
+ observedCommitRef.current = true;
onCommitSequences(sequenceManagerId, sequences, sequenceIds);
}
},
@@ -931,7 +932,6 @@ export const SequenceManagerProvider: React.FC<{
return;
}
- observedCommitRef.current = true;
if (commitRegistrationEnabled) {
return;
}
diff --git a/packages/core/src/sequence-outline.ts b/packages/core/src/sequence-outline.ts
index 298b62800e..6ccad94783 100644
--- a/packages/core/src/sequence-outline.ts
+++ b/packages/core/src/sequence-outline.ts
@@ -29,10 +29,10 @@ export const SequenceOutlineInternals = {
return;
}
- nodesByRef.set(ref, nodes);
ref.current =
nodes.length === 1 && nodes[0].nodeType === 1
? (nodes[0] as Element)
: null;
+ nodesByRef.set(ref, nodes);
},
};
diff --git a/packages/core/src/wrap-remotion-context.tsx b/packages/core/src/wrap-remotion-context.tsx
index 02b75cffc7..5691b0620c 100644
--- a/packages/core/src/wrap-remotion-context.tsx
+++ b/packages/core/src/wrap-remotion-context.tsx
@@ -14,10 +14,8 @@ import {RenderAssetManager} from './RenderAssetManager.js';
import {ResolveCompositionContext} from './ResolveCompositionConfig.js';
import {
COMMIT_REGISTRATION_ERROR_EVENT,
- COMMIT_ORDER_EVENT,
isCommitRegistrationObserverInstalled,
SequenceManagerOrderMarker,
- type CommitOrderEventDetail,
} from './sequence-order-marker.js';
import {SequenceContext} from './SequenceContext.js';
import {
@@ -150,6 +148,7 @@ const ForwardedSequenceRegistry: React.FC<RemotionContextProviderProps> = ({
return;
}
+ observedCommitRef.current = true;
scope?.onCommitSequences(scopeId, sequences, sequenceIds);
},
[scope, scopeId],
@@ -179,14 +178,6 @@ const ForwardedSequenceRegistry: React.FC<RemotionContextProviderProps> = ({
});
};
- const onCommitOrder = (event: Event) => {
- const {detail} = event as CustomEvent<CommitOrderEventDetail>;
- if (detail.sequenceManagers.some((item) => item.managerId === scopeId)) {
- observedCommitRef.current = true;
- }
- };
-
- window.addEventListener(COMMIT_ORDER_EVENT, onCommitOrder);
window.addEventListener(COMMIT_REGISTRATION_ERROR_EVENT, onFailure);
if (commitRegistrationEnabled) {
queueMicrotask(() => {
@@ -202,7 +193,6 @@ const ForwardedSequenceRegistry: React.FC<RemotionContextProviderProps> = ({
return () => {
unmountedRef.current = true;
- window.removeEventListener(COMMIT_ORDER_EVENT, onCommitOrder);
window.removeEventListener(COMMIT_REGISTRATION_ERROR_EVENT, onFailure);
// Strict Mode and Fast Refresh restart effects without removing the marker.
queueMicrotask(() => {
diff --git a/packages/sdk/src/install-fiber-sequence-order-observer.ts b/packages/sdk/src/install-fiber-sequence-order-observer.ts
index b70852198e..62dde6e4e9 100644
--- a/packages/sdk/src/install-fiber-sequence-order-observer.ts
+++ b/packages/sdk/src/install-fiber-sequence-order-observer.ts
@@ -132,6 +132,7 @@ export const collectCommitOrderFromFiber = (
RefObject<Element | null>,
(Element | Text)[]
>();
+ let outlineCollectionFailed = false;
const sequencesByManager = new Map<
string,
{
@@ -173,17 +174,6 @@ export const collectCommitOrderFromFiber = (
currentCompositionManagerId: string | null,
outlineCollectors: readonly (Element | Text)[][] | null,
) => {
- // A portal ends the current DOM group, but can contain new sequences:
- // Studio itself renders the composition through a portal. Hidden
- // Offscreen trees must not contribute geometry, including new groups.
- const skipOutline =
- outlineCollectors === null ||
- (fiber.tag === 22 && fiber.memoizedState !== null);
- let childOutlineCollectors = skipOutline
- ? null
- : fiber.tag === 4
- ? []
- : outlineCollectors;
const isSequenceManagerMarker = hasFiberMarker(
fiber,
Internals.CommitOrderInternals.sequenceManagerMarker,
@@ -261,7 +251,11 @@ export const collectCommitOrderFromFiber = (
});
}
- if (hasFiberMarker(fiber, Internals.CommitOrderInternals.sequenceMarker)) {
+ const isSequenceMarker = hasFiberMarker(
+ fiber,
+ Internals.CommitOrderInternals.sequenceMarker,
+ );
+ if (isSequenceMarker) {
const props = fiber.memoizedProps;
if (sequenceManagerId !== null) {
const sequenceId = getStringProp(fiber.memoizedProps, 'sequenceId');
@@ -280,21 +274,6 @@ export const collectCommitOrderFromFiber = (
}
}
}
-
- const outlineRef =
- typeof props === 'object' && props !== null
- ? (Reflect.get(props, 'outlineChildrenRef') as
- | RefObject<Element | null>
- | null
- | undefined)
- : null;
- if (outlineRef) {
- const nodes: (Element | Text)[] = [];
- outlineNodesByRef.set(outlineRef, nodes);
- if (childOutlineCollectors !== null) {
- childOutlineCollectors = [...childOutlineCollectors, nodes];
- }
- }
}
if (
@@ -345,19 +324,61 @@ export const collectCommitOrderFromFiber = (
}
}
- if (
- childOutlineCollectors !== null &&
- childOutlineCollectors.length > 0 &&
- (fiber.tag === 5 || fiber.tag === 6) &&
- fiber.stateNode !== null
- ) {
- for (const collector of childOutlineCollectors) {
- collector.push(fiber.stateNode as Element | Text);
- }
+ let childOutlineCollectors = outlineCollectors;
+ if (outlineCollectionFailed) {
+ childOutlineCollectors = null;
+ } else {
+ 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 skipOutline =
+ outlineCollectors === null ||
+ (tag === 22 && fiber.memoizedState !== null);
+ childOutlineCollectors = skipOutline
+ ? null
+ : tag === 4
+ ? []
+ : outlineCollectors;
+ if (isSequenceMarker) {
+ const props = fiber.memoizedProps;
+ const outlineRef =
+ typeof props === 'object' && props !== null
+ ? (Reflect.get(props, 'outlineChildrenRef') as
+ | RefObject<Element | null>
+ | null
+ | undefined)
+ : null;
+ if (outlineRef) {
+ const nodes: (Element | Text)[] = [];
+ outlineNodesByRef.set(outlineRef, nodes);
+ if (childOutlineCollectors !== null) {
+ childOutlineCollectors = [...childOutlineCollectors, nodes];
+ }
+ }
+ }
+
+ if (
+ childOutlineCollectors !== null &&
+ childOutlineCollectors.length > 0 &&
+ (tag === 5 || tag === 6) &&
+ fiber.stateNode !== null
+ ) {
+ for (const collector of childOutlineCollectors) {
+ collector.push(fiber.stateNode as Element | Text);
+ }
- // Only first-level DOM nodes belong to this group. Keep traversing
- // for sequence order and for new groups nested inside this element.
- childOutlineCollectors = [];
+ // Only first-level DOM nodes belong to this group. Keep traversing
+ // for sequence order and for new groups nested inside this element.
+ childOutlineCollectors = [];
+ }
+ } catch {
+ // Discard incomplete geometry for this commit without losing registrations.
+ // The next commit retries outline collection with a fresh map.
+ outlineCollectionFailed = true;
+ outlineNodesByRef.clear();
+ childOutlineCollectors = null;
+ }
}
let {child} = fiber;
@@ -374,7 +395,11 @@ export const collectCommitOrderFromFiber = (
visit(root.current, null, null, []);
for (const [ref, nodes] of outlineNodesByRef) {
- Internals.SequenceOutlineInternals.setNodes(ref, nodes);
+ try {
+ Internals.SequenceOutlineInternals.setNodes(ref, nodes);
+ } catch {
+ // A failed outline publication is retried on the next commit.
+ }
}
const sequenceManagers = [...sequencesByManager].map(
@@ -456,6 +481,7 @@ export const installFiberCommitOrderObserver = (
>();
hook.onCommitFiberRoot = function (...args) {
+ let order: ReturnType<typeof collectCommitOrderFromFiber> | null = null;
try {
const [, root] = args;
const registrations = new Map<string, CommittedSequenceRegistration>();
@@ -466,7 +492,7 @@ export const installFiberCommitOrderObserver = (
>();
const previousCompositionRegistrations =
compositionRegistrationsByRoot.get(root);
- const order = collectCommitOrderFromFiber(
+ order = collectCommitOrderFromFiber(
root,
registrations,
previousRegistrations ?? null,
@@ -514,12 +540,29 @@ export const installFiberCommitOrderObserver = (
}
}
}
+ } catch {
+ // A registration collection or delivery failure permanently selects effect
+ // fallback for this hook. Retrying registration would risk repeated teardown.
+ hook[Internals.CommitOrderInternals.failureMarker] = true;
+ order = null;
+ try {
+ target.dispatchEvent(
+ new CustomEvent(
+ Internals.CommitOrderInternals.registrationErrorEventName,
+ ),
+ );
+ } catch {
+ // Notification failure must not prevent the previous hook from running.
+ }
+ }
- if (
- order.outlineCount > 0 ||
+ if (
+ order !== null &&
+ (order.outlineCount > 0 ||
order.sequenceManagers.length > 0 ||
- order.compositionManagers.length > 0
- ) {
+ order.compositionManagers.length > 0)
+ ) {
+ try {
target.dispatchEvent(
new CustomEvent(Internals.CommitOrderInternals.eventName, {
detail: {
@@ -528,15 +571,9 @@ export const installFiberCommitOrderObserver = (
},
}),
);
+ } catch {
+ // Order notifications are independent of registration and retry next commit.
}
- } catch {
- // Fiber is private React API. An unsupported shape must not break the host.
- hook[Internals.CommitOrderInternals.failureMarker] = true;
- target.dispatchEvent(
- new CustomEvent(
- Internals.CommitOrderInternals.registrationErrorEventName,
- ),
- );
}
return previousOnCommitFiberRoot?.apply(this, args);