Skip to content

🎥 Make videos programmatically with React

Avoid timeline rerenders for split actions

JonnyBurger9aec3e17c444
diff --git a/packages/studio/src/components/SequencePropsSubscriptionProvider.tsx b/packages/studio/src/components/SequencePropsSubscriptionProvider.tsx
index b9bf78df27..5058892f04 100644
--- a/packages/studio/src/components/SequencePropsSubscriptionProvider.tsx
+++ b/packages/studio/src/components/SequencePropsSubscriptionProvider.tsx
@@ -1,4 +1,11 @@
-import {useState, useCallback, useContext, useMemo} from 'react';
+import {
+ createContext,
+ useCallback,
+ useContext,
+ useMemo,
+ useRef,
+ useState,
+} from 'react';
import {Internals} from 'remotion';
import type {
OverrideIdToNodePaths,
@@ -8,6 +15,10 @@ import type {
TSequence,
} from 'remotion';
+export const OverrideIdToNodePathMappingsRefContext = createContext<{
+ readonly current: OverrideIdToNodePaths;
+}>({current: {}});
+
export const getReadOnlyOverrideIdToNodePathMappings = (
sequences: readonly TSequence[],
): OverrideIdToNodePaths => {
@@ -47,6 +58,10 @@ export const SequencePropsSubscriptionProvider: React.FC<{
: null,
[sequences],
);
+ const overrideIdToNodePathMappings =
+ readOnlyOverrideToNodePathMap ?? overrideToNodePathMap;
+ const overrideIdToNodePathMappingsRef = useRef(overrideIdToNodePathMappings);
+ overrideIdToNodePathMappingsRef.current = overrideIdToNodePathMappings;
const setOverrideIdToNodePath = useCallback(
(overrideId: string, state: SequencePropsSubscriptionKey | null) => {
@@ -73,21 +88,24 @@ export const SequencePropsSubscriptionProvider: React.FC<{
);
const getters = useMemo((): OverrideToNodePathGetters => {
- return {
- overrideIdToNodePathMappings:
- readOnlyOverrideToNodePathMap ?? overrideToNodePathMap,
- };
- }, [overrideToNodePathMap, readOnlyOverrideToNodePathMap]);
+ return {overrideIdToNodePathMappings};
+ }, [overrideIdToNodePathMappings]);
const setters = useMemo((): OverrideToNodeSetters => {
return {setOverrideIdToNodePath};
}, [setOverrideIdToNodePath]);
return (
- <Internals.OverrideIdsToNodePathsGettersContext.Provider value={getters}>
- <Internals.OverrideIdsToNodePathsSettersContext.Provider value={setters}>
- {children}
- </Internals.OverrideIdsToNodePathsSettersContext.Provider>
- </Internals.OverrideIdsToNodePathsGettersContext.Provider>
+ <OverrideIdToNodePathMappingsRefContext.Provider
+ value={overrideIdToNodePathMappingsRef}
+ >
+ <Internals.OverrideIdsToNodePathsGettersContext.Provider value={getters}>
+ <Internals.OverrideIdsToNodePathsSettersContext.Provider
+ value={setters}
+ >
+ {children}
+ </Internals.OverrideIdsToNodePathsSettersContext.Provider>
+ </Internals.OverrideIdsToNodePathsGettersContext.Provider>
+ </OverrideIdToNodePathMappingsRefContext.Provider>
);
};
diff --git a/packages/studio/src/components/Timeline/TimelineDeleteKeybindings.tsx b/packages/studio/src/components/Timeline/TimelineDeleteKeybindings.tsx
index bf11198393..ca738c31f4 100644
--- a/packages/studio/src/components/Timeline/TimelineDeleteKeybindings.tsx
+++ b/packages/studio/src/components/Timeline/TimelineDeleteKeybindings.tsx
@@ -4,6 +4,7 @@ import {Internals} from 'remotion';
import {StudioServerConnectionCtx} from '../../helpers/client-id';
import {useKeybinding} from '../../helpers/use-keybinding';
import {useConfirmationDialog} from '../ConfirmationDialog';
+import {OverrideIdToNodePathMappingsRefContext} from '../SequencePropsSubscriptionProvider';
import {duplicateSelectedTimelineItems} from './duplicate-selected-timeline-item';
import {getCurrentFrame} from './imperative-state';
import {splitSelectedTimelineItems} from './split-selected-timeline-item';
@@ -17,8 +18,8 @@ export const TimelineDeleteKeybindings: React.FC = () => {
const keybindings = useKeybinding();
const {previewServerState} = useContext(StudioServerConnectionCtx);
const sequencesRef = useContext(Internals.SequenceManagerRefContext);
- const {overrideIdToNodePathMappings} = useContext(
- Internals.OverrideIdsToNodePathsGettersContext,
+ const overrideIdToNodePathMappingsRef = useContext(
+ OverrideIdToNodePathMappingsRefContext,
);
const propStatusesRef = useContext(
Internals.VisualModePropStatusesRefContext,
@@ -76,7 +77,7 @@ export const TimelineDeleteKeybindings: React.FC = () => {
const splitPromise = splitSelectedTimelineItems({
selections: selectedItems,
sequences: sequencesRef.current,
- overrideIdsToNodePaths: overrideIdToNodePathMappings,
+ overrideIdsToNodePaths: overrideIdToNodePathMappingsRef.current,
propStatuses: propStatusesRef.current,
splitFrame: getCurrentFrame(),
});
@@ -103,7 +104,7 @@ export const TimelineDeleteKeybindings: React.FC = () => {
currentSelection,
deleteTimelineItems,
keybindings,
- overrideIdToNodePathMappings,
+ overrideIdToNodePathMappingsRef,
propStatusesRef,
previewServerState,
sequencesRef,
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index 271752f9b1..a871010fce 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -47,6 +47,7 @@ import {AudioWaveform} from '../AudioWaveform';
import {useConfirmationDialog} from '../ConfirmationDialog';
import {ContextMenu} from '../ContextMenu';
import {useSelectComposition} from '../InitialCompositionLoader';
+import {OverrideIdToNodePathMappingsRefContext} from '../SequencePropsSubscriptionProvider';
import {useSelectAsset} from '../use-select-asset';
import {disableSequenceInteractivity} from './disable-sequence-interactivity';
import {duplicateSequencesFromSource} from './duplicate-selected-timeline-item';
@@ -438,8 +439,8 @@ const TimelineSequenceInner: React.FC<{
const video = Internals.useVideo();
const {sequences} = useContext(Internals.SequenceManager);
- const {overrideIdToNodePathMappings} = useContext(
- Internals.OverrideIdsToNodePathsGettersContext,
+ const overrideIdToNodePathMappingsRef = useContext(
+ OverrideIdToNodePathMappingsRefContext,
);
const renderWindow = useContext(TimelineViewportContext);
const mediaDurationDragLimitsRegistry = useContext(
@@ -651,12 +652,12 @@ const TimelineSequenceInner: React.FC<{
splitSelectedTimelineItems({
selections: selectedItems,
sequences,
- overrideIdsToNodePaths: overrideIdToNodePathMappings,
+ overrideIdsToNodePaths: overrideIdToNodePathMappingsRef.current,
propStatuses,
splitFrame: getCurrentFrame(),
})?.catch(() => undefined);
}, [
- overrideIdToNodePathMappings,
+ overrideIdToNodePathMappingsRef,
previewInteractive,
propStatuses,
selectedItems,
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
index d753bd8418..d163d84a6b 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
@@ -72,6 +72,7 @@ import {
cropFieldKeys,
rotateFieldKey,
} from '../selected-outline-types';
+import {OverrideIdToNodePathMappingsRefContext} from '../SequencePropsSubscriptionProvider';
import {useSelectAsset} from '../use-select-asset';
import {disableSequenceInteractivity} from './disable-sequence-interactivity';
import {duplicateSequencesFromSource} from './duplicate-selected-timeline-item';
@@ -315,11 +316,13 @@ const TimelineSequenceItemInner: React.FC<{
showProvisionalVisibilityToggle,
}) => {
const nodePath = nodePathInfo?.sequenceSubscriptionKey ?? null;
- const {sequences} = useContext(Internals.SequenceManager);
- const {overrideIdToNodePathMappings} = useContext(
- Internals.OverrideIdsToNodePathsGettersContext,
+ const sequencesRef = useContext(Internals.SequenceManagerRefContext);
+ const overrideIdToNodePathMappingsRef = useContext(
+ OverrideIdToNodePathMappingsRefContext,
+ );
+ const propStatusesRef = useContext(
+ Internals.VisualModePropStatusesRefContext,
);
- const {propStatuses} = useContext(Internals.VisualModePropStatusesContext);
const {hovered, onPointerEnter, onPointerLeave} =
useTimelineSequenceHover(nodePathInfo);
const {previewServerState} = useContext(StudioServerConnectionCtx);
@@ -459,18 +462,18 @@ const TimelineSequenceItemInner: React.FC<{
splitSelectedTimelineItems({
selections: selectedItems,
- sequences,
- overrideIdsToNodePaths: overrideIdToNodePathMappings,
- propStatuses,
+ sequences: sequencesRef.current,
+ overrideIdsToNodePaths: overrideIdToNodePathMappingsRef.current,
+ propStatuses: propStatusesRef.current,
splitFrame: getCurrentFrame(),
})?.catch(() => undefined);
}, [
- overrideIdToNodePathMappings,
+ overrideIdToNodePathMappingsRef,
previewInteractive,
- propStatuses,
+ propStatusesRef,
selectedItems,
selectedSequenceNodePathInfos,
- sequences,
+ sequencesRef,
]);
const onDeleteSequenceFromSource = useCallback(() => {
diff --git a/packages/studio/src/test/sequence-props-subscription-provider.test.tsx b/packages/studio/src/test/sequence-props-subscription-provider.test.tsx
new file mode 100644
index 0000000000..ad5b8cbdf2
--- /dev/null
+++ b/packages/studio/src/test/sequence-props-subscription-provider.test.tsx
@@ -0,0 +1,71 @@
+import {afterEach, expect, test} from 'bun:test';
+import {act, cleanup, render} from '@testing-library/react';
+import {type ContextType, useContext} from 'react';
+import {Internals, type SequencePropsSubscriptionKey} from 'remotion';
+import {
+ OverrideIdToNodePathMappingsRefContext,
+ SequencePropsSubscriptionProvider,
+} from '../components/SequencePropsSubscriptionProvider';
+
+afterEach(cleanup);
+
+test('updates the imperative node path mappings without rerendering consumers', () => {
+ let refConsumerRenders = 0;
+ let reactiveConsumerRenders = 0;
+ let mappingsRef: ContextType<typeof OverrideIdToNodePathMappingsRefContext> =
+ {current: {}};
+ let setOverrideIdToNodePath: ContextType<
+ typeof Internals.OverrideIdsToNodePathsSettersContext
+ >['setOverrideIdToNodePath'] = () => {
+ throw new Error('Node path setter was not initialized');
+ };
+
+ const RefConsumer = () => {
+ refConsumerRenders++;
+ mappingsRef = useContext(OverrideIdToNodePathMappingsRefContext);
+ return null;
+ };
+
+ const ReactiveConsumer = () => {
+ reactiveConsumerRenders++;
+ useContext(Internals.OverrideIdsToNodePathsGettersContext);
+ return null;
+ };
+
+ const Setter = () => {
+ setOverrideIdToNodePath = useContext(
+ Internals.OverrideIdsToNodePathsSettersContext,
+ ).setOverrideIdToNodePath;
+ return null;
+ };
+
+ render(
+ <Internals.SequenceManager.Provider
+ value={{
+ registerSequence: () => undefined,
+ sequences: [],
+ unregisterSequence: () => undefined,
+ updateSequence: null,
+ }}
+ >
+ <SequencePropsSubscriptionProvider>
+ <RefConsumer />
+ <ReactiveConsumer />
+ <Setter />
+ </SequencePropsSubscriptionProvider>
+ </Internals.SequenceManager.Provider>,
+ );
+
+ const nodePath: SequencePropsSubscriptionKey = {
+ absolutePath: '/project/src/Composition.tsx',
+ effectKeys: [],
+ nodePath: ['body', 0],
+ sequenceKeys: ['from', 'durationInFrames'],
+ videoConfigValues: null,
+ };
+ act(() => setOverrideIdToNodePath('clip', nodePath));
+
+ expect(reactiveConsumerRenders).toBe(2);
+ expect(refConsumerRenders).toBe(1);
+ expect(mappingsRef.current).toEqual({clip: nodePath});
+});