Skip to content

🎥 Make videos programmatically with React

Keep sync store shim internal

Jonny Burgerc043e5f619da
diff --git a/packages/canvas/src/selection.ts b/packages/canvas/src/selection.ts
index c89eb4763c..633168feab 100644
--- a/packages/canvas/src/selection.ts
+++ b/packages/canvas/src/selection.ts
@@ -1,6 +1,6 @@
import {useState} from 'react';
-import {Internals} from 'remotion';
import type {SequenceNodePathInfo} from './get-timeline-sequence-sort-key';
+import {useSyncExternalStore} from './use-sync-external-store';
export type CanvasSelectionItem =
| {
@@ -327,7 +327,7 @@ export const useCanvasSelectionController = (): CanvasSelectionController => {
export const useCanvasSelection = (
controller: CanvasSelectionController,
): CanvasSelectionSnapshot => {
- return Internals.useSyncExternalStore(
+ return useSyncExternalStore(
controller.subscribe,
controller.getSnapshot,
controller.getSnapshot,
diff --git a/packages/canvas/src/use-sync-external-store.ts b/packages/canvas/src/use-sync-external-store.ts
new file mode 100644
index 0000000000..7f15652aeb
--- /dev/null
+++ b/packages/canvas/src/use-sync-external-store.ts
@@ -0,0 +1,11 @@
+import {Internals} from 'remotion';
+
+type UseSyncExternalStore = <TSnapshot>(
+ subscribe: (onStoreChange: () => void) => () => void,
+ getSnapshot: () => TSnapshot,
+ getServerSnapshot?: () => TSnapshot,
+) => TSnapshot;
+
+export const {useSyncExternalStore} = Internals as typeof Internals & {
+ useSyncExternalStore: UseSyncExternalStore;
+};
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index 4c2bb03100..60c7a6f716 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -311,7 +311,6 @@ export const Internals = {
makeRenderResourceManager,
RenderResourceManagerContext,
createRuntimeValueStore,
- useSyncExternalStore,
useUnsafeVideoConfig,
useFrameForVolumeProp,
useTimelinePosition: TimelinePosition.useTimelinePosition,
@@ -477,6 +476,10 @@ export const Internals = {
useCropStyle,
} as const;
+// Keep this available to other Remotion packages without adding it to the
+// public type of Internals.
+Object.assign(Internals, {useSyncExternalStore});
+
export type {
ArrayFieldSchema,
AssetFieldSchema,
diff --git a/packages/studio/src/api/visual-control.ts b/packages/studio/src/api/visual-control.ts
index f093c5c656..68eb1df797 100644
--- a/packages/studio/src/api/visual-control.ts
+++ b/packages/studio/src/api/visual-control.ts
@@ -1,4 +1,5 @@
-import {getRemotionEnvironment, Internals} from 'remotion';
+import {getRemotionEnvironment} from 'remotion';
+import {useSyncExternalStore} from '../helpers/use-sync-external-store';
import {visualControlStore} from '../visual-controls/visual-control-store';
import {
visualControlRef,
@@ -16,7 +17,7 @@ export const visualControl: VisualControlRef['globalVisualControl'] = (
// Subscribe to store changes so the calling component
// re-renders when a visual control value is edited in the sidebar.
// eslint-disable-next-line react-hooks/rules-of-hooks
- Internals.useSyncExternalStore(
+ useSyncExternalStore(
visualControlStore.subscribe,
visualControlStore.getSnapshot,
visualControlStore.getSnapshot,
diff --git a/packages/studio/src/components/Timeline/Timeline.tsx b/packages/studio/src/components/Timeline/Timeline.tsx
index c5b7783972..26928cf183 100644
--- a/packages/studio/src/components/Timeline/Timeline.tsx
+++ b/packages/studio/src/components/Timeline/Timeline.tsx
@@ -24,6 +24,7 @@ import {useIsStill} from '../../helpers/is-current-selected-still';
import {useCachedCompositionComponentInfo} from '../../helpers/open-in-editor';
import {getStudioMaxTimelineTracks} from '../../helpers/studio-runtime-config';
import {timelineSequenceNodePathToKey} from '../../helpers/timeline-node-path-key';
+import {useSyncExternalStore} from '../../helpers/use-sync-external-store';
import {callApi} from '../call-api';
import {ContextMenu} from '../ContextMenu';
import {importAssets, pickFilesToImport} from '../import-assets';
@@ -347,7 +348,7 @@ const TimelineInner: React.FC = () => {
overrideIdToNodePathMappings,
compositions,
]);
- const pendingInsertedElementSelection = Internals.useSyncExternalStore(
+ const pendingInsertedElementSelection = useSyncExternalStore(
subscribeToInsertedElementSelection,
getInsertedElementSelection,
getInsertedElementSelection,
diff --git a/packages/studio/src/helpers/open-in-editor.ts b/packages/studio/src/helpers/open-in-editor.ts
index 6191e45f72..51dc3cc68d 100644
--- a/packages/studio/src/helpers/open-in-editor.ts
+++ b/packages/studio/src/helpers/open-in-editor.ts
@@ -7,7 +7,6 @@ import type {
SymbolicatedStackFrame,
} from '@remotion/studio-shared';
import {useEffect} from 'react';
-import {Internals} from 'remotion';
import {callApi} from '../components/call-api';
import {getSourceMapFilesForSource} from '../components/Timeline/TimelineStack/get-stack';
import type {
@@ -15,6 +14,7 @@ import type {
OriginalPosition,
} from '../error-overlay/react-overlay/utils/get-source-map';
import {getBrowserStudioOperations} from './browser-studio-operations';
+import {useSyncExternalStore} from './use-sync-external-store';
export const openInEditor = (
stack: SymbolicatedStackFrame,
@@ -167,7 +167,7 @@ export const useCachedCompositionComponentInfo = ({
compositionFile: string | null;
compositionId: string | null;
}) => {
- const result = Internals.useSyncExternalStore(
+ const result = useSyncExternalStore(
subscribeToCompositionComponentInfo,
() => {
if (compositionFile === null || compositionId === null) {
diff --git a/packages/studio/src/helpers/use-runtime-values.ts b/packages/studio/src/helpers/use-runtime-values.ts
index 1fabed8780..3ce91f2d95 100644
--- a/packages/studio/src/helpers/use-runtime-values.ts
+++ b/packages/studio/src/helpers/use-runtime-values.ts
@@ -1,9 +1,9 @@
import {useCallback, useMemo} from 'react';
import {
- Internals,
type RuntimeValueStore,
type SequenceRegistrationControls,
} from 'remotion';
+import {useSyncExternalStore} from './use-sync-external-store';
const EMPTY_RUNTIME_VALUES: Readonly<Record<string, unknown>> = {};
const EMPTY_RUNTIME_VALUE_STORE = {
@@ -15,7 +15,7 @@ export const useRuntimeValues = (
controls: SequenceRegistrationControls | null,
): Readonly<Record<string, unknown>> => {
const store = controls?.runtimeValues ?? EMPTY_RUNTIME_VALUE_STORE;
- return Internals.useSyncExternalStore(
+ return useSyncExternalStore(
store.subscribe,
store.getSnapshot,
store.getSnapshot,
@@ -36,11 +36,7 @@ export const useRuntimeStoreValue = (
const store = storeOrNull ?? EMPTY_RUNTIME_VALUE_STORE;
const getSnapshot = useCallback(() => store.getSnapshot()[key], [key, store]);
- return Internals.useSyncExternalStore(
- store.subscribe,
- getSnapshot,
- getSnapshot,
- );
+ return useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);
};
export const useRuntimeValueSelector = <T>({
@@ -78,7 +74,7 @@ export const useRuntimeValueSelector = <T>({
};
}, [isEqual, selector, store]);
- return Internals.useSyncExternalStore(
+ return useSyncExternalStore(
selectedStore.subscribe,
selectedStore.getSnapshot,
selectedStore.getSnapshot,
@@ -122,7 +118,7 @@ export const useRuntimeValueSnapshots = (
};
}, [stores]);
- return Internals.useSyncExternalStore(
+ return useSyncExternalStore(
aggregateStore.subscribe,
aggregateStore.getSnapshot,
aggregateStore.getSnapshot,
diff --git a/packages/studio/src/helpers/use-sync-external-store.ts b/packages/studio/src/helpers/use-sync-external-store.ts
new file mode 100644
index 0000000000..7f15652aeb
--- /dev/null
+++ b/packages/studio/src/helpers/use-sync-external-store.ts
@@ -0,0 +1,11 @@
+import {Internals} from 'remotion';
+
+type UseSyncExternalStore = <TSnapshot>(
+ subscribe: (onStoreChange: () => void) => () => void,
+ getSnapshot: () => TSnapshot,
+ getServerSnapshot?: () => TSnapshot,
+) => TSnapshot;
+
+export const {useSyncExternalStore} = Internals as typeof Internals & {
+ useSyncExternalStore: UseSyncExternalStore;
+};
diff --git a/packages/studio/src/state/timeline-sequence-hover.ts b/packages/studio/src/state/timeline-sequence-hover.ts
index f6f0e49fbc..9d9cd57d63 100644
--- a/packages/studio/src/state/timeline-sequence-hover.ts
+++ b/packages/studio/src/state/timeline-sequence-hover.ts
@@ -1,11 +1,11 @@
import type {Dispatch, SetStateAction} from 'react';
import {createContext, useCallback, useContext, useMemo} from 'react';
-import {Internals} from 'remotion';
import type {SequenceNodePathInfo} from '../helpers/get-timeline-sequence-sort-key';
import {
timelineNodePathInfoToKey,
timelineSequenceNodePathToKey,
} from '../helpers/timeline-node-path-key';
+import {useSyncExternalStore} from '../helpers/use-sync-external-store';
export type TimelineSequenceHover = {
readonly key: string;
@@ -52,7 +52,7 @@ export const TimelineSequenceHoverContext =
export const useTimelineSequenceHoverState = () => {
const store = useContext(TimelineSequenceHoverContext);
- return Internals.useSyncExternalStore(
+ return useSyncExternalStore(
store.subscribe,
store.getSnapshot,
store.getSnapshot,
@@ -71,11 +71,7 @@ export const useIsTimelineSequenceHovered = (nodePathKey: string | null) => {
[nodePathKey, store],
);
- return Internals.useSyncExternalStore(
- store.subscribe,
- getSnapshot,
- getSnapshot,
- );
+ return useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);
};
export const useTimelineSequenceHover = (