🎥 Make videos programmatically with React
Cache expanded timeline selectors by structural runtime values
Jonny Burger77ff19c3058e
diff --git a/packages/studio/src/components/Timeline/use-expanded-track-keyframe-rows.ts b/packages/studio/src/components/Timeline/use-expanded-track-keyframe-rows.tsindex 8891c3e525..8cd09c523b 100644--- a/packages/studio/src/components/Timeline/use-expanded-track-keyframe-rows.ts+++ b/packages/studio/src/components/Timeline/use-expanded-track-keyframe-rows.ts@@ -8,7 +8,6 @@ import {type TimelineTreeNode,} from '../../helpers/timeline-layout';import {timelineNodePathInfoToKey} from '../../helpers/timeline-node-path-key';-import {useRuntimeValueSelector} from '../../helpers/use-runtime-values';import {ExpandedTracksGetterContext} from '../ExpandedTracksProvider';import {getNodeCanEditEasing,@@ -23,6 +22,7 @@ import {isTimelineExpandedNodeSelected,} from './timeline-expanded-filter';import {useTimelineSelection} from './TimelineSelection';+import {useTimelineStructureSelector} from './use-timeline-structure-selector';export type ExpandedTrackKeyframeRow = {readonly height: number;@@ -117,7 +117,7 @@ export const useExpandedTrackKeyframeRows = ({nodePathInfo,],);- const tree = useRuntimeValueSelector({+ const tree = useTimelineStructureSelector({controls: sequence.controls,selector: selectTree,isEqual: areTimelineTreeLayoutsEqual,diff --git a/packages/studio/src/components/Timeline/use-timeline-expanded-tree.ts b/packages/studio/src/components/Timeline/use-timeline-expanded-tree.tsindex 6e12281953..bd1760c003 100644--- a/packages/studio/src/components/Timeline/use-timeline-expanded-tree.ts+++ b/packages/studio/src/components/Timeline/use-timeline-expanded-tree.ts@@ -19,6 +19,7 @@ import {type TimelineSelection,useTimelineSelection,} from './TimelineSelection';+import {useTimelineStructureSelector} from './use-timeline-structure-selector';export const useTimelineSequenceHasExpandableContent = ({sequence,@@ -131,11 +132,12 @@ export const useTimelineSequenceHasExpandableContent = ({],);- return useRuntimeValueSelector({+ return useTimelineStructureSelector({// Runtime values can change active enum fields, but cannot introduce a// selection or keyframes. Most code-animated tracks need no subscription.controls: canHaveExpandableContent ? sequence.controls : null,selector: selectHasExpandableContent,+ isEqual: null,});};diff --git a/packages/studio/src/components/Timeline/use-timeline-structure-selector.ts b/packages/studio/src/components/Timeline/use-timeline-structure-selector.tsnew file mode 100644index 0000000000..841a27be94--- /dev/null+++ b/packages/studio/src/components/Timeline/use-timeline-structure-selector.ts@@ -0,0 +1,82 @@+import {useMemo} from 'react';+import type {InteractivitySchema, SequenceRegistrationControls} from 'remotion';+import {useRuntimeValueSelector} from '../../helpers/use-runtime-values';++// Timeline tree structure depends on enum choices and array lengths, while+// animated field values are read by their editors through the runtime store.+export const useTimelineStructureSelector = <T>({+ controls,+ selector,+ isEqual,+}: {+ readonly controls: SequenceRegistrationControls | null;+ readonly selector: (values: Readonly<Record<string, unknown>>) => T;+ readonly isEqual: ((first: T, second: T) => boolean) | null;+}): T => {+ const schema = controls?.schema;+ const store = controls?.runtimeValues;+ const selectStructure = useMemo(() => {+ const keys = new Map<string, boolean>();+ const schemas: InteractivitySchema[] = schema ? [schema] : [];+ for (const current of schemas) {+ for (const [key, field] of Object.entries(current)) {+ if (field.type === 'enum') {+ keys.set(key, true);+ schemas.push(...Object.values(field.variants));+ } else if (field.type === 'array' && !keys.has(key)) {+ keys.set(key, false);+ }+ }+ }++ let previous: unknown[] | null = null;+ let result: T;+ return (values: Readonly<Record<string, unknown>>) => {+ const next: unknown[] = [];+ let cacheable = true;+ for (const [key, isEnum] of keys) {+ // Absent keys do not resolve source values or drag overrides.+ const present = Object.prototype.propertyIsEnumerable.call(values, key);+ next.push(present);+ if (!present) continue;+ const value = values[key];+ if (isEnum) {+ // Object-to-key coercion can change without a new reference.+ if (+ value !== null &&+ (typeof value === 'object' || typeof value === 'function')+ ) {+ cacheable = false;+ }++ next.push(value);+ } else {+ // Capture length now, including arrays later mutated in place.+ next.push(Array.isArray(value) ? value.length : null);+ }+ }++ const last = previous;+ if (+ cacheable &&+ last !== null &&+ last.length === next.length &&+ next.every((value, index) => Object.is(value, last[index]))+ ) {+ return result;+ }++ result = selector(values);+ previous = cacheable ? next : null;+ return result;+ };+ // Tree nodes hold runtime-store references, so a new store must reset the cache.+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [schema, store, selector]);++ return useRuntimeValueSelector({+ controls,+ selector: selectStructure,+ isEqual: isEqual ?? Object.is,+ });+};