Skip to content

🎥 Make videos programmatically with React

Restore synchronous timeline trimming with cached limits

Jonny Burgerc58740cc10e6
diff --git a/packages/studio/src/components/InspectorPanel/MultiSequenceField.tsx b/packages/studio/src/components/InspectorPanel/MultiSequenceField.tsx
index 2266e54360..b08289ce51 100644
--- a/packages/studio/src/components/InspectorPanel/MultiSequenceField.tsx
+++ b/packages/studio/src/components/InspectorPanel/MultiSequenceField.tsx
@@ -84,9 +84,7 @@ export const MultiSequenceField: React.FC<{
isStudioInteractivityEnabled() &&
previewServerState.type === 'connected' &&
staticValues;
- const previewGeneration = useRef(0);
const clear = useCallback(() => {
- previewGeneration.current++;
clearTimingLimits();
for (const target of targets) {
clearDragOverrides(target.nodePath);
@@ -95,38 +93,21 @@ export const MultiSequenceField: React.FC<{
const targetsRef = useRef(targets);
targetsRef.current = targets;
useEffect(() => {
- const generation = previewGeneration;
return () => {
- generation.current++;
for (const target of targetsRef.current) {
clearDragOverrides(target.nodePath);
}
};
}, [clearDragOverrides]);
const preview = useCallback(
- async (nextValues: unknown[]) => {
+ (nextValues: unknown[]) => {
if (!editable) {
return;
}
- const generation = ++previewGeneration.current;
- let clamped: unknown[];
- try {
- clamped = await Promise.all(
- targets.map((target, index) =>
- clampTimingValue(target.nodePath, field.key, nextValues[index]),
- ),
- );
- } catch (error) {
- if (generation === previewGeneration.current)
- showNotification(
- `Could not check timing constraints: ${(error as Error).message}`,
- 3000,
- );
- return;
- }
-
- if (generation !== previewGeneration.current) return;
+ const clamped = targets.map((target, index) =>
+ clampTimingValue(target.nodePath, field.key, nextValues[index]),
+ );
for (const [index, target] of targets.entries()) {
setDragOverrides(
target.nodePath,
@@ -147,10 +128,8 @@ export const MultiSequenceField: React.FC<{
}
try {
- const clamped = await Promise.all(
- targets.map((target, index) =>
- clampTimingValue(target.nodePath, field.key, nextValues[index]),
- ),
+ const clamped = targets.map((target, index) =>
+ clampTimingValue(target.nodePath, field.key, nextValues[index]),
);
await saveSequenceProps({
changes: targets.map((target, index) => ({
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index b495be5198..82f8076e45 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -61,6 +61,7 @@ import {OverrideIdToNodePathMappingsRefContext} from '../SequencePropsSubscripti
import {useSelectAsset} from '../use-select-asset';
import {disableSequenceInteractivity} from './disable-sequence-interactivity';
import {duplicateSequencesFromSource} from './duplicate-selected-timeline-item';
+import {getKeyframeTrimConstraints} from './get-keyframe-trim-constraints';
import {
getMultiSequenceContextMenuItems,
getSequenceContextMenuItems,
@@ -1075,7 +1076,24 @@ const useTimelineSequenceState = (props: TimelineSequenceProps) => {
isTransitionSeriesSequence(s) || s.timelineTrack?.role === 'transition';
const [transitionNeighbors, setTransitionNeighbors] =
useState<ReturnType<typeof getTimelineTransitionNeighbors>>(null);
+ const originalSequence = registryEntry?.sequence ?? null;
+ const overrideIdToNodePathMappingsRef = useContext(
+ OverrideIdToNodePathMappingsRefContext,
+ );
+ const nodePath = nodePathInfo?.sequenceSubscriptionKey ?? null;
+ const propStatusesRef = useContext(
+ Internals.VisualModePropStatusesRefContext,
+ );
const onPointerEnter = useCallback(() => {
+ if (nodePath && isStudioInteractivityEnabled()) {
+ getKeyframeTrimConstraints({
+ propStatuses: propStatusesRef.current,
+ overrideIdsToNodePaths: overrideIdToNodePathMappingsRef.current,
+ sequences: sequencesRef.current,
+ affectedNodePaths: [nodePath],
+ });
+ }
+
if (!isTransitionInteraction) return;
setTransitionNeighbors(
getTimelineTransitionNeighbors({
@@ -1083,12 +1101,15 @@ const useTimelineSequenceState = (props: TimelineSequenceProps) => {
sequences: sequencesRef.current,
}),
);
- }, [isTransitionInteraction, s.id, sequencesRef]);
- const originalSequence = registryEntry?.sequence ?? null;
- const overrideIdToNodePathMappingsRef = useContext(
- OverrideIdToNodePathMappingsRefContext,
- );
- const nodePath = nodePathInfo?.sequenceSubscriptionKey ?? null;
+ }, [
+ isTransitionInteraction,
+ nodePath,
+ overrideIdToNodePathMappingsRef,
+ propStatusesRef,
+ s.id,
+ sequencesRef,
+ ]);
+
const mediaDurationDragLimitsRegistry = useContext(
TimelineSequenceMediaDurationDragLimitsContext,
);
diff --git a/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx b/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx
index 764312d41e..611b27914b 100644
--- a/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx
@@ -3,13 +3,7 @@ import {
isSchemaFieldKeyframable,
stringifySequenceSubscriptionKey,
} from '@remotion/studio-shared';
-import React, {
- useCallback,
- useContext,
- useEffect,
- useMemo,
- useRef,
-} from 'react';
+import React, {useCallback, useContext, useMemo, useRef} from 'react';
import {unstable_batchedUpdates} from 'react-dom';
import type {
CanUpdateSequencePropStatus,
@@ -262,8 +256,8 @@ const Value: React.FC<{
: null;
const onSave = useCallback<TimelineFieldOnSave>(
- async (value, options) => {
- value = await clampTimingValue(nodePath, field.key, value);
+ (value, options) => {
+ value = clampTimingValue(nodePath, field.key, value);
if (!clientId) {
return Promise.reject(new Error('Not connected to studio server'));
}
@@ -344,35 +338,13 @@ const Value: React.FC<{
],
);
- const previewGeneration = useRef(0);
- useEffect(
- () => () => {
- previewGeneration.current++;
- },
- [],
- );
const onDragValueChange = useCallback<TimelineFieldOnDragValueChange>(
- async (value) => {
+ (value) => {
if (nodePath === null) {
throw new Error('Cannot drag value');
}
- const generation = ++previewGeneration.current;
- try {
- value = await clampTimingValue(nodePath, field.key, value);
- } catch (error) {
- if (generation === previewGeneration.current) {
- Internals.Log.error(
- {logLevel: 'error', tag: null},
- 'Could not check timing constraints',
- error,
- );
- }
-
- return;
- }
-
- if (generation !== previewGeneration.current) return;
+ value = clampTimingValue(nodePath, field.key, value);
const keyframeChanges = getPlaybackRateChanges(value);
unstable_batchedUpdates(() => {
for (const preview of previewedKeyframes.current) {
@@ -419,7 +391,6 @@ const Value: React.FC<{
);
const onDragEnd = useCallback(() => {
- previewGeneration.current++;
clearTimingLimits();
if (nodePath === null) {
throw new Error('Cannot clear drag value');
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
index 364d53953a..52b1a0b026 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
@@ -30,19 +30,19 @@ import {
isPointerSessionRelease,
startDeferredCapturedPointerSession,
} from '../../helpers/pointer-session';
-import {startPreparedPointerSession} from '../../helpers/start-prepared-pointer-session';
+import {startThresholdPointerSession} from '../../helpers/start-threshold-pointer-session';
import {TIMELINE_PADDING} from '../../helpers/timeline-layout';
import {EditorSnappingContext} from '../../state/editor-snapping';
import {
forceSpecificCursor,
stopForcingSpecificCursor,
} from '../ForceSpecificCursor';
-import {showNotification} from '../Notifications/NotificationCenter';
import {OverrideIdToNodePathMappingsRefContext} from '../SequencePropsSubscriptionProvider';
import type {
MoveEffectKeyframeChange,
MoveSequenceKeyframeChange,
} from './call-move-keyframe';
+import {getKeyframeTrimConstraints} from './get-keyframe-trim-constraints';
import {
getKeyframedSequenceDragTargets,
type TimelineSequenceEffectKeyframeDragTarget,
@@ -56,7 +56,6 @@ import {
getTimelineSequenceTimingLimits,
} from './get-timeline-sequence-timing-limits';
import {getTimelineTransitionNeighbors} from './get-timeline-transition-neighbors';
-import {loadKeyframeTrimConstraints} from './load-keyframe-trim-constraints';
import {
saveSequenceProps,
type SaveSequencePropChange,
@@ -2222,14 +2221,18 @@ const TimelineSequenceLeftEdgeDragHandleInner: React.FC<{
})) ?? [])
: [];
stopPointerSessionRef.current?.();
- stopPointerSessionRef.current = startPreparedPointerSession({
+ stopPointerSessionRef.current = startThresholdPointerSession({
event: downEvent,
captureTarget,
minimumMovement: isTransitionSeries
? timelineSequenceEdgeDragThresholdPx
: null,
- prepare: () =>
- loadKeyframeTrimConstraints({
+ onCancel: () => {
+ stopPointerSessionRef.current = null;
+ finishDrag(false);
+ },
+ onStart: () => {
+ const preparedPropStatuses = getKeyframeTrimConstraints({
propStatuses,
overrideIdsToNodePaths,
sequences,
@@ -2241,17 +2244,7 @@ const TimelineSequenceLeftEdgeDragHandleInner: React.FC<{
: [target.nodePath, target.ripplePrevious.nodePath]
: [target.nodePath],
),
- }),
- onCancel: () => {
- stopPointerSessionRef.current = null;
- finishDrag(false);
- },
- onError: (error) =>
- showNotification(
- `Could not check trim constraints: ${(error as Error).message}`,
- 3000,
- ),
- onReady: (preparedPropStatuses) => {
+ });
const targets = getTargets(preparedPropStatuses);
const draggedKey = stringifySequenceSubscriptionKey(
latestNodePathInfo.sequenceSubscriptionKey,
@@ -3440,31 +3433,25 @@ const TimelineSequenceRightEdgeDragHandleInner: React.FC<{
}) ?? [])
: [];
stopPointerSessionRef.current?.();
- stopPointerSessionRef.current = startPreparedPointerSession({
+ stopPointerSessionRef.current = startThresholdPointerSession({
event: downEvent,
captureTarget,
minimumMovement: isTransitionSeries
? timelineSequenceEdgeDragThresholdPx
: null,
- prepare: () =>
- loadKeyframeTrimConstraints({
+ onCancel: () => {
+ stopPointerSessionRef.current = null;
+ finishDrag(false);
+ },
+ onStart: () => {
+ const preparedPropStatuses = getKeyframeTrimConstraints({
propStatuses,
overrideIdsToNodePaths,
sequences,
affectedNodePaths: getTargets(propStatuses).map(
(target) => target.nodePath,
),
- }),
- onCancel: () => {
- stopPointerSessionRef.current = null;
- finishDrag(false);
- },
- onError: (error) =>
- showNotification(
- `Could not check trim constraints: ${(error as Error).message}`,
- 3000,
- ),
- onReady: (preparedPropStatuses) => {
+ });
const targets = getTargets(preparedPropStatuses);
const draggedKey = stringifySequenceSubscriptionKey(
latestNodePathInfo.sequenceSubscriptionKey,
diff --git a/packages/studio/src/components/Timeline/load-keyframe-trim-constraints.ts b/packages/studio/src/components/Timeline/get-keyframe-trim-constraints.ts
similarity index 60%
rename from packages/studio/src/components/Timeline/load-keyframe-trim-constraints.ts
rename to packages/studio/src/components/Timeline/get-keyframe-trim-constraints.ts
index fb94dd43c4..8e0a4c084c 100644
--- a/packages/studio/src/components/Timeline/load-keyframe-trim-constraints.ts
+++ b/packages/studio/src/components/Timeline/get-keyframe-trim-constraints.ts
@@ -1,3 +1,4 @@
+import type * as Codemods from '@remotion/codemods';
import type {
CanUpdateSequencePropsResponseTrue,
OverrideIdToNodePaths,
@@ -13,16 +14,19 @@ import {
} from '../../helpers/preview-server-events';
type CachedSource = {
- contents: string;
+ source: Promise<{
+ contents: string;
+ codemods: typeof Codemods;
+ }>;
constraints: Map<
string,
CanUpdateSequencePropsResponseTrue['durationDependentKeyframes']
>;
};
-const sourceCache = new Map<string, Promise<CachedSource>>();
+const sourceCache = new Map<string, CachedSource>();
let subscribedToSourceChanges = false;
-export const loadKeyframeTrimConstraints = async ({
+export const getKeyframeTrimConstraints = ({
propStatuses,
overrideIdsToNodePaths,
sequences,
@@ -32,7 +36,7 @@ export const loadKeyframeTrimConstraints = async ({
overrideIdsToNodePaths: OverrideIdToNodePaths;
sequences: readonly TSequence[];
affectedNodePaths: readonly SequencePropsSubscriptionKey[];
-}): Promise<PropStatuses> => {
+}): PropStatuses => {
const affectedKeys = new Set(
affectedNodePaths.map(Internals.makeSequencePropsSubscriptionKey),
);
@@ -98,80 +102,85 @@ export const loadKeyframeTrimConstraints = async ({
});
}
- // Only affected clips and descendants need source analysis. Concurrent
- // gestures/inspector fields share in-flight reads as well as parsed results.
+ // Edits take a synchronous snapshot. Missing source and analysis load in the
+ // background for the next edit, without changing an active gesture's bounds.
const browserOperations = getBrowserStudioOperations();
- const entries = new Map<string, Promise<CachedSource>>();
- for (const source of sources.values()) {
+ const result = {...propStatuses};
+ for (const [key, source] of sources) {
const fileName = source.absolutePath;
- if (entries.has(fileName)) continue;
let entry = sourceCache.get(fileName);
if (!entry) {
- entry = (async (): Promise<CachedSource> => {
- let contents: string;
- if (browserOperations) {
- const value = await browserOperations.getFileSource(fileName);
- if (value === null) throw new Error(`Could not load ${fileName}`);
- contents = value;
- } else {
- const response = await fetch(
- `/api/file-source?f=${encodeURIComponent(fileName)}`,
- {cache: 'no-store'},
- );
- if (!response.ok) throw new Error(`Could not load ${fileName}`);
- contents = await response.text();
- }
+ const pendingSource = (async () => {
+ const [codemods, contents] = await Promise.all([
+ import('@remotion/codemods'),
+ (async () => {
+ if (browserOperations) {
+ const value = await browserOperations.getFileSource(fileName);
+ if (value === null) throw new Error(`Could not load ${fileName}`);
+ return value;
+ }
- return {contents, constraints: new Map()};
+ const response = await fetch(
+ `/api/file-source?f=${encodeURIComponent(fileName)}`,
+ {cache: 'no-store'},
+ );
+ if (!response.ok) throw new Error(`Could not load ${fileName}`);
+ return response.text();
+ })(),
+ ]);
+ return {contents, codemods};
})();
+ entry = {source: pendingSource, constraints: new Map()};
sourceCache.set(fileName, entry);
const pendingEntry = entry;
- entry.catch(() => {
- if (sourceCache.get(fileName) === pendingEntry)
- sourceCache.delete(fileName);
+ pendingSource.catch((error) => {
+ if (sourceCache.get(fileName) !== pendingEntry) return;
+ sourceCache.delete(fileName);
+ Internals.Log.warn(
+ {logLevel: 'info', tag: '@remotion/studio'},
+ `Could not load ${fileName} for keyframe analysis. Using existing trim limits.`,
+ error,
+ );
});
}
- entries.set(fileName, entry);
- }
-
- const [codemods, files] = await Promise.all([
- import('@remotion/codemods'),
- Promise.all(
- [...entries].map(
- async ([fileName, entry]) => [fileName, await entry] as const,
- ),
- ),
- ]);
- for (const [fileName, entry] of entries) {
- if (sourceCache.get(fileName) !== entry) {
- throw new Error(
- 'Source changed while checking trim constraints. Try again.',
- );
- }
- }
-
- const contentsByFile = new Map(files);
- const result = {...propStatuses};
- for (const [key, source] of sources) {
- const status = propStatuses[key];
- if (!status?.canUpdate) continue;
- const cached = contentsByFile.get(source.absolutePath)!;
+ const cached = entry;
const pathKey = JSON.stringify(source.nodePath);
if (!cached.constraints.has(pathKey)) {
- cached.constraints.set(
- pathKey,
- codemods.CodemodsInternals.getDurationDependentKeyframesFromContent({
- fileContents: cached.contents,
- nodePath: source.nodePath,
- }),
+ // Also deduplicate pending analysis of the same node.
+ cached.constraints.set(pathKey, null);
+ cached.source.then(
+ ({contents, codemods}) => {
+ if (sourceCache.get(fileName) !== cached) return;
+ try {
+ cached.constraints.set(
+ pathKey,
+ codemods.CodemodsInternals.getDurationDependentKeyframesFromContent(
+ {
+ fileContents: contents,
+ nodePath: source.nodePath,
+ },
+ ),
+ );
+ } catch (error) {
+ Internals.Log.warn(
+ {logLevel: 'info', tag: '@remotion/studio'},
+ `Could not analyze keyframes in ${fileName}. Using existing trim limits.`,
+ error,
+ );
+ }
+ },
+ () => {
+ // The shared source failure is handled above; other files still work.
+ },
);
}
- result[key] = {
- ...status,
- durationDependentKeyframes: cached.constraints.get(pathKey)!,
- };
+ const status = propStatuses[key];
+ const constraints = cached.constraints.get(pathKey);
+ if (status?.canUpdate && constraints) {
+ result[key] = {...status, durationDependentKeyframes: constraints};
+ }
}
return result;
diff --git a/packages/studio/src/components/Timeline/use-clamp-sequence-timing-value.ts b/packages/studio/src/components/Timeline/use-clamp-sequence-timing-value.ts
index 2254b9036c..277a89cc9d 100644
--- a/packages/studio/src/components/Timeline/use-clamp-sequence-timing-value.ts
+++ b/packages/studio/src/components/Timeline/use-clamp-sequence-timing-value.ts
@@ -4,8 +4,8 @@ import type {SequencePropsSubscriptionKey} from 'remotion';
import {Internals} from 'remotion';
import {calculateTimeline} from '../../helpers/calculate-timeline';
import {OverrideIdToNodePathMappingsRefContext} from '../SequencePropsSubscriptionProvider';
+import {getKeyframeTrimConstraints} from './get-keyframe-trim-constraints';
import {getTimelineSequenceTimingLimits} from './get-timeline-sequence-timing-limits';
-import {loadKeyframeTrimConstraints} from './load-keyframe-trim-constraints';
export const useClampSequenceTimingValue = () => {
const propStatusesRef = useContext(
@@ -15,16 +15,11 @@ export const useClampSequenceTimingValue = () => {
const mappingsRef = useContext(OverrideIdToNodePathMappingsRefContext);
const video = Internals.useVideo();
const limitsRef = useRef(
- new Map<
- string,
- Promise<ReturnType<typeof getTimelineSequenceTimingLimits> | null>
- >(),
+ new Map<string, ReturnType<typeof getTimelineSequenceTimingLimits>>(),
);
- const clearTimingLimits = useCallback(() => {
- limitsRef.current.clear();
- }, []);
+ const clearTimingLimits = useCallback(() => limitsRef.current.clear(), []);
const clampTimingValue = useCallback(
- async (
+ (
nodePath: SequencePropsSubscriptionKey,
fieldKey: string,
value: unknown,
@@ -37,17 +32,17 @@ export const useClampSequenceTimingValue = () => {
)
return value;
const key = stringifySequenceSubscriptionKey(nodePath);
- let pendingLimits = limitsRef.current.get(key);
- if (!pendingLimits) {
+ let limits = limitsRef.current.get(key);
+ if (!limits) {
const sequences = sequencesRef.current;
const tracks = calculateTimeline({
sequences,
overrideIdsToNodePaths: mappingsRef.current,
});
- const preparedProps =
+ const propStatuses =
fieldKey === 'from'
- ? Promise.resolve(propStatusesRef.current)
- : loadKeyframeTrimConstraints({
+ ? propStatusesRef.current
+ : getKeyframeTrimConstraints({
propStatuses: propStatusesRef.current,
overrideIdsToNodePaths: mappingsRef.current,
sequences,
@@ -60,66 +55,52 @@ export const useClampSequenceTimingValue = () => {
track.nodePathInfo.sequenceSubscriptionKey,
) === key,
);
- pendingLimits = preparedProps.then((propStatuses) => {
- let mergedLimits: ReturnType<
- typeof getTimelineSequenceTimingLimits
- > | null = null;
- for (const track of matching) {
- const next = getTimelineSequenceTimingLimits({
- propStatuses,
- movingSequenceIds: null,
- track,
- tracks,
- sequences,
- timelineDurationInFrames: video?.durationInFrames ?? Infinity,
- });
- mergedLimits = mergedLimits
- ? {
- ...next,
- minimumDuration: Math.max(
- mergedLimits.minimumDuration,
- next.minimumDuration,
- ),
- maximumDuration: Math.min(
- mergedLimits.maximumDuration,
- next.maximumDuration,
- ),
- minimumFrom: Math.max(
- mergedLimits.minimumFrom,
- next.minimumFrom,
- ),
- maximumFrom: Math.min(
- mergedLimits.maximumFrom,
- next.maximumFrom,
- ),
- minimumTrimBefore: Math.max(
- mergedLimits.minimumTrimBefore,
- next.minimumTrimBefore,
- ),
- maximumTrimBefore: Math.min(
- mergedLimits.maximumTrimBefore,
- next.maximumTrimBefore,
- ),
- minimumTrimAfter: Math.max(
- mergedLimits.minimumTrimAfter,
- next.minimumTrimAfter,
- ),
- maximumTrimAfter: Math.min(
- mergedLimits.maximumTrimAfter,
- next.maximumTrimAfter,
- ),
- }
- : next;
- }
+ for (const track of matching) {
+ const next = getTimelineSequenceTimingLimits({
+ propStatuses,
+ movingSequenceIds: null,
+ track,
+ tracks,
+ sequences,
+ timelineDurationInFrames: video?.durationInFrames ?? Infinity,
+ });
+ limits = limits
+ ? {
+ ...next,
+ minimumDuration: Math.max(
+ limits.minimumDuration,
+ next.minimumDuration,
+ ),
+ maximumDuration: Math.min(
+ limits.maximumDuration,
+ next.maximumDuration,
+ ),
+ minimumFrom: Math.max(limits.minimumFrom, next.minimumFrom),
+ maximumFrom: Math.min(limits.maximumFrom, next.maximumFrom),
+ minimumTrimBefore: Math.max(
+ limits.minimumTrimBefore,
+ next.minimumTrimBefore,
+ ),
+ maximumTrimBefore: Math.min(
+ limits.maximumTrimBefore,
+ next.maximumTrimBefore,
+ ),
+ minimumTrimAfter: Math.max(
+ limits.minimumTrimAfter,
+ next.minimumTrimAfter,
+ ),
+ maximumTrimAfter: Math.min(
+ limits.maximumTrimAfter,
+ next.maximumTrimAfter,
+ ),
+ }
+ : next;
+ }
- return mergedLimits;
- });
- limitsRef.current.set(key, pendingLimits);
+ if (!limits) return value;
+ limitsRef.current.set(key, limits);
}
- const limits = await pendingLimits;
- if (!limits) return value;
-
const [minimum, maximum] =
fieldKey === 'from'
? [limits.minimumFrom, limits.maximumFrom]
diff --git a/packages/studio/src/helpers/start-prepared-pointer-session.ts b/packages/studio/src/helpers/start-prepared-pointer-session.ts
deleted file mode 100644
index 0c8064a4b5..0000000000
--- a/packages/studio/src/helpers/start-prepared-pointer-session.ts
+++ /dev/null
@@ -1,95 +0,0 @@
-import type {PointerSessionEndReason} from './pointer-session';
-import {
- isPointerSessionRelease,
- startCapturedPointerSession,
-} from './pointer-session';
-
-type PreparedPointerHandlers = {
- onMove: (event: PointerEvent) => void;
- onEnd: (reason: PointerSessionEndReason, event: PointerEvent | null) => void;
-};
-
-export const startPreparedPointerSession = <T>({
- event,
- captureTarget,
- minimumMovement,
- prepare,
- onReady,
- onCancel,
- onError,
-}: {
- event: PointerEvent;
- captureTarget: Element;
- minimumMovement: number | null;
- prepare: () => Promise<T>;
- onReady: (prepared: T) => PreparedPointerHandlers;
- onCancel: () => void;
- onError: (error: unknown) => void;
-}): (() => void) => {
- let active = true;
- let preparing = false;
- let handlers: PreparedPointerHandlers | null = null;
- let latestMove: PointerEvent | null = null;
- let release: {reason: PointerSessionEndReason; event: PointerEvent} | null =
- null;
- const movedEnough = (move: PointerEvent) =>
- minimumMovement === null ||
- Math.abs(move.clientX - event.clientX) >= minimumMovement;
- const beginPreparation = () => {
- preparing = true;
- prepare()
- .then((prepared) => {
- if (!active) return;
- handlers = onReady(prepared);
- if (release) {
- active = false;
- handlers.onEnd(release.reason, release.event);
- } else if (latestMove) {
- handlers.onMove(latestMove);
- }
- })
- .catch((error) => {
- if (!active) return;
- cancel();
- onError(error);
- });
- };
-
- const stopCapture = startCapturedPointerSession({
- event,
- captureTarget,
- onMove: (move) => {
- latestMove = move;
- if (!preparing && movedEnough(move)) beginPreparation();
- handlers?.onMove(move);
- },
- onEnd: (reason, endEvent) => {
- if (!active) return;
- if (handlers) {
- active = false;
- handlers.onEnd(reason, endEvent);
- } else if (
- isPointerSessionRelease(reason, endEvent) &&
- (preparing || movedEnough(endEvent))
- ) {
- // A quick drag can finish before preparation. Apply its final position
- // only once the bounds are known; a new gesture/unmount can cancel it.
- release = {reason, event: endEvent};
- if (!preparing) beginPreparation();
- } else {
- active = false;
- onCancel();
- }
- },
- });
- const cancel = () => {
- if (!active) return;
- stopCapture();
- if (!active) return;
- active = false;
- onCancel();
- };
-
- if (minimumMovement === null) beginPreparation();
- return cancel;
-};
diff --git a/packages/studio/src/helpers/start-threshold-pointer-session.ts b/packages/studio/src/helpers/start-threshold-pointer-session.ts
new file mode 100644
index 0000000000..7baa847c5c
--- /dev/null
+++ b/packages/studio/src/helpers/start-threshold-pointer-session.ts
@@ -0,0 +1,48 @@
+import type {PointerSessionEndReason} from './pointer-session';
+import {
+ isPointerSessionRelease,
+ startCapturedPointerSession,
+} from './pointer-session';
+
+type PointerHandlers = {
+ onMove: (event: PointerEvent) => void;
+ onEnd: (reason: PointerSessionEndReason, event: PointerEvent | null) => void;
+};
+
+export const startThresholdPointerSession = ({
+ event,
+ captureTarget,
+ minimumMovement,
+ onStart,
+ onCancel,
+}: {
+ event: PointerEvent;
+ captureTarget: Element;
+ minimumMovement: number | null;
+ onStart: () => PointerHandlers;
+ onCancel: () => void;
+}): (() => void) => {
+ let handlers = minimumMovement === null ? onStart() : null;
+ const movedEnough = (move: PointerEvent) =>
+ Math.abs(move.clientX - event.clientX) >= (minimumMovement ?? 0);
+ return startCapturedPointerSession({
+ event,
+ captureTarget,
+ onMove: (move) => {
+ if (!handlers && movedEnough(move)) handlers = onStart();
+ handlers?.onMove(move);
+ },
+ onEnd: (reason, endEvent) => {
+ if (
+ !handlers &&
+ isPointerSessionRelease(reason, endEvent) &&
+ movedEnough(endEvent)
+ ) {
+ handlers = onStart();
+ }
+
+ if (handlers) handlers.onEnd(reason, endEvent);
+ else onCancel();
+ },
+ });
+};
diff --git a/packages/studio/src/test/keyframe-trim-constraints.test.ts b/packages/studio/src/test/keyframe-trim-constraints.test.ts
index a01538edd6..1756bcb627 100644
--- a/packages/studio/src/test/keyframe-trim-constraints.test.ts
+++ b/packages/studio/src/test/keyframe-trim-constraints.test.ts
@@ -1,6 +1,7 @@
import {expect, test} from 'bun:test';
import {CodemodsInternals, getNodeProps, getNodes} from '@remotion/codemods';
import type {EventSourceEvent} from '@remotion/studio-shared';
+import {waitFor} from '@testing-library/react';
import type {
OverrideIdToNodePaths,
PropStatuses,
@@ -8,7 +9,7 @@ import type {
TSequence,
} from 'remotion';
import {Internals, interpolate} from 'remotion';
-import {loadKeyframeTrimConstraints} from '../components/Timeline/load-keyframe-trim-constraints';
+import {getKeyframeTrimConstraints} from '../components/Timeline/get-keyframe-trim-constraints';
import {
getTimelineSequenceDurationDragChanges,
getTimelineSequenceDurationDragTargets,
@@ -402,7 +403,7 @@ test('converts descendant constraints through offsets and playback rates', () =>
).toBe(30);
});
-test('loads only affected source, reuses it, and refreshes constraints after source changes', async () => {
+test('trims synchronously while affected source loads and refreshes cached constraints after changes', async () => {
const fixture = makeFixture(
'[10, 21, durationInFrames - 18, durationInFrames - 8]',
'child',
@@ -427,7 +428,7 @@ test('loads only affected source, reuses it, and refreshes constraints after sou
},
});
const load = () =>
- loadKeyframeTrimConstraints({
+ getKeyframeTrimConstraints({
propStatuses: fixture.propStatuses,
overrideIdsToNodePaths: fixture.mappings,
sequences: fixture.sequences,
@@ -444,27 +445,34 @@ test('loads only affected source, reuses it, and refreshes constraints after sou
]!,
});
try {
- const [first, concurrent] = await Promise.all([load(), load()]);
+ const first = load();
+ const concurrent = load();
+ expect(first).not.toBeInstanceOf(Promise);
+ expect(getTargets(fixture, first, 'parent')[0].minimumDuration).toBe(1);
+ expect(getTargets(fixture, concurrent, 'parent')[0].minimumDuration).toBe(
+ 1,
+ );
+ await waitFor(() => {
+ expect(getTargets(fixture, load(), 'parent')[0].minimumDuration).toBe(40);
+ });
expect(reads.sort()).toEqual([
'/trim-test/content.tsx',
'/trim-test/parent.tsx',
]);
- expect(getTargets(fixture, first, 'parent')[0].minimumDuration).toBe(40);
- expect(getTargets(fixture, concurrent, 'parent')[0].minimumDuration).toBe(
- 40,
- );
- await load();
+ // Background completion cannot change bounds already captured by an edit.
+ expect(getTargets(fixture, first, 'parent')[0].minimumDuration).toBe(1);
+ load();
expect(reads).toHaveLength(2);
fixture.files['/trim-test/content.tsx'] = fixture.files[
'/trim-test/content.tsx'
].replace('[10, 21,', '[10, 31,');
sourceChanged();
- const refreshed = await load();
+ expect(getTargets(fixture, load(), 'parent')[0].minimumDuration).toBe(1);
+ await waitFor(() => {
+ expect(getTargets(fixture, load(), 'parent')[0].minimumDuration).toBe(50);
+ });
expect(reads.slice(2)).toEqual(['/trim-test/content.tsx']);
- expect(getTargets(fixture, refreshed, 'parent')[0].minimumDuration).toBe(
- 50,
- );
let finishRead!: (source: string) => void;
pendingRead = new Promise((resolve) => {
@@ -474,13 +482,15 @@ test('loads only affected source, reuses it, and refreshes constraints after sou
const stale = load();
sourceChanged();
finishRead(fixture.files['/trim-test/content.tsx']);
- await expect(stale).rejects.toThrow('Source changed');
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ expect(getTargets(fixture, stale, 'parent')[0].minimumDuration).toBe(1);
pendingRead = Promise.resolve(null);
- await expect(load()).rejects.toThrow('Could not load');
+ expect(getTargets(fixture, load(), 'parent')[0].minimumDuration).toBe(1);
+ await new Promise((resolve) => setTimeout(resolve, 0));
pendingRead = null;
- expect(getTargets(fixture, await load(), 'parent')[0].minimumDuration).toBe(
- 50,
- );
+ await waitFor(() => {
+ expect(getTargets(fixture, load(), 'parent')[0].minimumDuration).toBe(50);
+ });
} finally {
emit({type: 'hmr', hmrEvent: {action: 'building'}});
window.remotion_browserStudio = previousOperations;
diff --git a/packages/studio/src/test/prepared-pointer-session.test.ts b/packages/studio/src/test/prepared-pointer-session.test.ts
deleted file mode 100644
index fe8f024552..0000000000
--- a/packages/studio/src/test/prepared-pointer-session.test.ts
+++ /dev/null
@@ -1,148 +0,0 @@
-import {expect, test} from 'bun:test';
-import {startPreparedPointerSession} from '../helpers/start-prepared-pointer-session';
-
-test.each(['held', 'released'] as const)(
- 'buffers a %s drag until preparation finishes',
- async (state) => {
- const releaseEarly = state === 'released';
- const target = document.createElement('div');
- let ready!: (value: number) => void;
- const preparation = new Promise<number>((resolve) => {
- ready = resolve;
- });
- const events: string[] = [];
- let preparationStarted = false;
- const stop = startPreparedPointerSession({
- event: new PointerEvent('pointerdown', {
- pointerId: 1,
- button: 0,
- buttons: 1,
- }),
- captureTarget: target,
- minimumMovement: 4,
- prepare: () => {
- preparationStarted = true;
- return preparation;
- },
- onReady: (minimum) => {
- events.push(`ready:${minimum}`);
- return {
- onMove: (event) => events.push(`move:${event.clientX}`),
- onEnd: (reason, event) => events.push(`${reason}:${event?.clientX}`),
- };
- },
- onCancel: () => events.push('cancel'),
- onError: () => events.push('error'),
- });
- try {
- expect(preparationStarted).toBe(false);
- window.dispatchEvent(
- new PointerEvent('pointermove', {
- pointerId: 1,
- buttons: 1,
- clientX: 3,
- }),
- );
- expect(preparationStarted).toBe(false);
- for (const clientX of [10, 20])
- window.dispatchEvent(
- new PointerEvent('pointermove', {pointerId: 1, buttons: 1, clientX}),
- );
- expect(preparationStarted).toBe(true);
- if (releaseEarly)
- window.dispatchEvent(
- new PointerEvent('pointerup', {
- pointerId: 1,
- buttons: 0,
- clientX: 30,
- }),
- );
- expect(events).toEqual([]);
- ready(40);
- await preparation;
- expect(events).toEqual(
- releaseEarly ? ['ready:40', 'pointerup:30'] : ['ready:40', 'move:20'],
- );
- if (!releaseEarly)
- window.dispatchEvent(
- new PointerEvent('pointerup', {
- pointerId: 1,
- buttons: 0,
- clientX: 30,
- }),
- );
- expect(events.at(-1)).toBe('pointerup:30');
- } finally {
- stop();
- }
- },
-);
-
-test('cancelling pending preparation cannot restart a completed gesture', async () => {
- for (const cancellation of ['manual', 'pointercancel', 'blur'] as const) {
- let ready!: (value: number) => void;
- const preparation = new Promise<number>((resolve) => {
- ready = resolve;
- });
- const events: string[] = [];
- const stop = startPreparedPointerSession({
- event: new PointerEvent('pointerdown', {
- pointerId: 1,
- button: 0,
- buttons: 1,
- }),
- captureTarget: document.createElement('div'),
- minimumMovement: null,
- prepare: () => preparation,
- onReady: () => {
- events.push('ready');
- return {
- onMove: () => events.push('move'),
- onEnd: () => events.push('end'),
- };
- },
- onCancel: () => events.push('cancel'),
- onError: () => events.push('error'),
- });
- if (cancellation === 'manual') stop();
- else if (cancellation === 'blur') window.dispatchEvent(new Event('blur'));
- else
- window.dispatchEvent(new PointerEvent('pointercancel', {pointerId: 1}));
- ready(40);
- await preparation;
- stop();
- expect(events).toEqual(['cancel']);
- }
-});
-
-test('failed preparation cancels once and reports the error after an early release', async () => {
- let fail!: (error: Error) => void;
- const preparation = new Promise<never>((_resolve, reject) => {
- fail = reject;
- });
- const events: unknown[] = [];
- const error = new Error('Source unavailable');
- const stop = startPreparedPointerSession({
- event: new PointerEvent('pointerdown', {
- pointerId: 1,
- button: 0,
- buttons: 1,
- }),
- captureTarget: document.createElement('div'),
- minimumMovement: null,
- prepare: () => preparation,
- onReady: () => {
- throw new Error('Must not begin a failed drag');
- },
- onCancel: () => events.push('cancel'),
- onError: (failure) => events.push(failure),
- });
- window.dispatchEvent(
- new PointerEvent('pointerup', {pointerId: 1, buttons: 0}),
- );
- fail(error);
- await preparation.catch(() => undefined);
- await Promise.resolve();
- stop();
- expect(events).toEqual(['cancel', error]);
-});
diff --git a/packages/studio/src/test/threshold-pointer-session.test.ts b/packages/studio/src/test/threshold-pointer-session.test.ts
new file mode 100644
index 0000000000..8f2b42adec
--- /dev/null
+++ b/packages/studio/src/test/threshold-pointer-session.test.ts
@@ -0,0 +1,94 @@
+import {expect, test} from 'bun:test';
+import {startThresholdPointerSession} from '../helpers/start-threshold-pointer-session';
+
+test.each(['held', 'released'] as const)(
+ 'handles a %s drag synchronously after the movement threshold',
+ (state) => {
+ const events: string[] = [];
+ const stop = startThresholdPointerSession({
+ event: new PointerEvent('pointerdown', {
+ pointerId: 1,
+ button: 0,
+ buttons: 1,
+ }),
+ captureTarget: document.createElement('div'),
+ minimumMovement: 4,
+ onStart: () => {
+ events.push('start');
+ return {
+ onMove: (event) => events.push(`move:${event.clientX}`),
+ onEnd: (reason, event) => events.push(`${reason}:${event?.clientX}`),
+ };
+ },
+ onCancel: () => events.push('cancel'),
+ });
+ try {
+ window.dispatchEvent(
+ new PointerEvent('pointermove', {
+ pointerId: 1,
+ buttons: 1,
+ clientX: 3,
+ }),
+ );
+ expect(events).toEqual([]);
+ if (state === 'held') {
+ for (const clientX of [10, 20])
+ window.dispatchEvent(
+ new PointerEvent('pointermove', {
+ pointerId: 1,
+ buttons: 1,
+ clientX,
+ }),
+ );
+ expect(events).toEqual(['start', 'move:10', 'move:20']);
+ }
+
+ window.dispatchEvent(
+ new PointerEvent('pointerup', {
+ pointerId: 1,
+ buttons: 0,
+ clientX: 30,
+ }),
+ );
+ expect(events).toEqual(
+ state === 'held'
+ ? ['start', 'move:10', 'move:20', 'pointerup:30']
+ : ['start', 'pointerup:30'],
+ );
+ } finally {
+ stop();
+ }
+ },
+);
+
+test('cancelling before the movement threshold cannot restart a completed gesture', () => {
+ for (const cancellation of ['manual', 'pointercancel', 'blur'] as const) {
+ const events: string[] = [];
+ const stop = startThresholdPointerSession({
+ event: new PointerEvent('pointerdown', {
+ pointerId: 1,
+ button: 0,
+ buttons: 1,
+ }),
+ captureTarget: document.createElement('div'),
+ minimumMovement: 4,
+ onStart: () => {
+ events.push('start');
+ return {
+ onMove: () => events.push('move'),
+ onEnd: () => events.push('end'),
+ };
+ },
+ onCancel: () => events.push('cancel'),
+ });
+ if (cancellation === 'manual') stop();
+ else if (cancellation === 'blur') window.dispatchEvent(new Event('blur'));
+ else
+ window.dispatchEvent(new PointerEvent('pointercancel', {pointerId: 1}));
+ window.dispatchEvent(
+ new PointerEvent('pointermove', {pointerId: 1, buttons: 1, clientX: 30}),
+ );
+ stop();
+ expect(events).toEqual(['cancel']);
+ }
+});