🎥 Make videos programmatically with React
Vendor useSyncExternalStore shim
Jonny Burgerdda7ea8949e6
diff --git a/packages/canvas/src/selection.ts b/packages/canvas/src/selection.tsindex f83c734774..c89eb4763c 100644--- a/packages/canvas/src/selection.ts+++ b/packages/canvas/src/selection.ts@@ -1,4 +1,5 @@-import {useState, useSyncExternalStore} from 'react';+import {useState} from 'react';+import {Internals} from 'remotion';import type {SequenceNodePathInfo} from './get-timeline-sequence-sort-key';export type CanvasSelectionItem =@@ -326,7 +327,7 @@ export const useCanvasSelectionController = (): CanvasSelectionController => {export const useCanvasSelection = (controller: CanvasSelectionController,): CanvasSelectionSnapshot => {- return useSyncExternalStore(+ return Internals.useSyncExternalStore(controller.subscribe,controller.getSnapshot,controller.getSnapshot,diff --git a/packages/core/THIRD_PARTY_LICENSES.md b/packages/core/THIRD_PARTY_LICENSES.mdnew file mode 100644index 0000000000..c5bac94fc3--- /dev/null+++ b/packages/core/THIRD_PARTY_LICENSES.md@@ -0,0 +1,28 @@+# Third-party licenses++## use-sync-external-store++The vendored `useSyncExternalStore` compatibility implementation is adapted+from `use-sync-external-store` 1.5.0.++MIT License++Copyright (c) Meta Platforms, Inc. and affiliates.++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex eafd73f022..4c2bb03100 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -250,6 +250,7 @@ import {type EffectDragOverrides,type PropStatuses,} from './use-schema.js';+import {useSyncExternalStore} from './use-sync-external-store.js';import {useUnsafeVideoConfig} from './use-unsafe-video-config.js';import {useVideo} from './use-video.js';import {validateMediaProps} from './validate-media-props.js';@@ -310,6 +311,7 @@ export const Internals = {makeRenderResourceManager,RenderResourceManagerContext,createRuntimeValueStore,+ useSyncExternalStore,useUnsafeVideoConfig,useFrameForVolumeProp,useTimelinePosition: TimelinePosition.useTimelinePosition,diff --git a/packages/core/src/use-playing.ts b/packages/core/src/use-playing.tsindex 5fae1838ba..7617786179 100644--- a/packages/core/src/use-playing.ts+++ b/packages/core/src/use-playing.ts@@ -1,6 +1,7 @@-import {useSyncExternalStore, useContext} from 'react';+import {useContext} from 'react';import {useTimelineContext} from './timeline-position-state.js';import {SetTimelineContext} from './TimelineContext.js';+import {useSyncExternalStore} from './use-sync-external-store.js';export const usePlaying = () => {const {isPlaying} = useTimelineContext();diff --git a/packages/core/src/use-sync-external-store.ts b/packages/core/src/use-sync-external-store.tsnew file mode 100644index 0000000000..665a64e1b2--- /dev/null+++ b/packages/core/src/use-sync-external-store.ts@@ -0,0 +1,120 @@+/**+ * @license React+ *+ * Copyright (c) Meta Platforms, Inc. and affiliates.+ *+ * Permission is hereby granted, free of charge, to any person obtaining a copy+ * of this software and associated documentation files (the "Software"), to deal+ * in the Software without restriction, including without limitation the rights+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+ * copies of the Software, and to permit persons to whom the Software is+ * furnished to do so, subject to the following conditions:+ *+ * The above copyright notice and this permission notice shall be included in+ * all copies or substantial portions of the Software.+ *+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+ * SOFTWARE.+ *+ * Adapted from use-sync-external-store 1.5.0.+ */++import * as React from 'react';++type UseSyncExternalStore = <TSnapshot>(+ subscribe: (onStoreChange: () => void) => () => void,+ getSnapshot: () => TSnapshot,+ getServerSnapshot?: () => TSnapshot,+) => TSnapshot;++type StoreInstance<TSnapshot> = {+ value: TSnapshot;+ getSnapshot: () => TSnapshot;+};++const objectIs =+ typeof Object.is === 'function'+ ? Object.is+ : (first: unknown, second: unknown) =>+ (first === second &&+ (first !== 0 || 1 / (first as number) === 1 / (second as number))) ||+ (Number.isNaN(first) && Number.isNaN(second));++let didWarnAboutUncachedGetSnapshot = false;++const checkIfSnapshotChanged = <TSnapshot>(+ instance: StoreInstance<TSnapshot>,+): boolean => {+ try {+ return !objectIs(instance.value, instance.getSnapshot());+ } catch {+ return true;+ }+};++const useSyncExternalStoreShimClient: UseSyncExternalStore = (+ subscribe,+ getSnapshot,+) => {+ const value = getSnapshot();++ if (+ process.env.NODE_ENV !== 'production' &&+ !didWarnAboutUncachedGetSnapshot &&+ !objectIs(value, getSnapshot())+ ) {+ // eslint-disable-next-line no-console+ console.error(+ 'The result of getSnapshot should be cached to avoid an infinite loop',+ );+ didWarnAboutUncachedGetSnapshot = true;+ }++ const [{instance}, forceUpdate] = React.useState({+ instance: {value, getSnapshot},+ });++ React.useLayoutEffect(() => {+ instance.value = value;+ instance.getSnapshot = getSnapshot;++ if (checkIfSnapshotChanged(instance)) {+ forceUpdate({instance});+ }+ }, [getSnapshot, instance, subscribe, value]);++ React.useEffect(() => {+ if (checkIfSnapshotChanged(instance)) {+ forceUpdate({instance});+ }++ return subscribe(() => {+ if (checkIfSnapshotChanged(instance)) {+ forceUpdate({instance});+ }+ });+ }, [instance, subscribe]);++ React.useDebugValue(value);+ return value;+};++const useSyncExternalStoreShimServer: UseSyncExternalStore = (+ _subscribe,+ getSnapshot,+) => getSnapshot();++const shim =+ typeof window === 'undefined' ||+ typeof window.document === 'undefined' ||+ typeof window.document.createElement === 'undefined'+ ? useSyncExternalStoreShimServer+ : useSyncExternalStoreShimClient;++export const useSyncExternalStore: UseSyncExternalStore =+ React.useSyncExternalStore ?? shim;diff --git a/packages/studio/src/api/visual-control.ts b/packages/studio/src/api/visual-control.tsindex 4c79ee04ec..f093c5c656 100644--- a/packages/studio/src/api/visual-control.ts+++ b/packages/studio/src/api/visual-control.ts@@ -1,5 +1,4 @@-import {useSyncExternalStore} from 'react';-import {getRemotionEnvironment} from 'remotion';+import {getRemotionEnvironment, Internals} from 'remotion';import {visualControlStore} from '../visual-controls/visual-control-store';import {visualControlRef,@@ -17,7 +16,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- useSyncExternalStore(+ Internals.useSyncExternalStore(visualControlStore.subscribe,visualControlStore.getSnapshot,visualControlStore.getSnapshot,diff --git a/packages/studio/src/components/Timeline/Timeline.tsx b/packages/studio/src/components/Timeline/Timeline.tsxindex 78d47c3e68..c5b7783972 100644--- a/packages/studio/src/components/Timeline/Timeline.tsx+++ b/packages/studio/src/components/Timeline/Timeline.tsx@@ -6,7 +6,6 @@ import React, {useMemo,useRef,useState,- useSyncExternalStore,} from 'react';import {Internals} from 'remotion';import {FastRefreshContext} from '../../fast-refresh-context';@@ -348,7 +347,7 @@ const TimelineInner: React.FC = () => {overrideIdToNodePathMappings,compositions,]);- const pendingInsertedElementSelection = useSyncExternalStore(+ const pendingInsertedElementSelection = Internals.useSyncExternalStore(subscribeToInsertedElementSelection,getInsertedElementSelection,getInsertedElementSelection,diff --git a/packages/studio/src/helpers/open-in-editor.ts b/packages/studio/src/helpers/open-in-editor.tsindex 0b1d4f20ea..6191e45f72 100644--- a/packages/studio/src/helpers/open-in-editor.ts+++ b/packages/studio/src/helpers/open-in-editor.ts@@ -6,7 +6,8 @@ import type {OpenInTerminalResponse,SymbolicatedStackFrame,} from '@remotion/studio-shared';-import {useEffect, useSyncExternalStore} from 'react';+import {useEffect} from 'react';+import {Internals} from 'remotion';import {callApi} from '../components/call-api';import {getSourceMapFilesForSource} from '../components/Timeline/TimelineStack/get-stack';import type {@@ -166,7 +167,7 @@ export const useCachedCompositionComponentInfo = ({compositionFile: string | null;compositionId: string | null;}) => {- const result = useSyncExternalStore(+ const result = Internals.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.tsindex 956a550bd4..1fabed8780 100644--- a/packages/studio/src/helpers/use-runtime-values.ts+++ b/packages/studio/src/helpers/use-runtime-values.ts@@ -1,5 +1,9 @@-import {useCallback, useMemo, useSyncExternalStore} from 'react';-import type {RuntimeValueStore, SequenceRegistrationControls} from 'remotion';+import {useCallback, useMemo} from 'react';+import {+ Internals,+ type RuntimeValueStore,+ type SequenceRegistrationControls,+} from 'remotion';const EMPTY_RUNTIME_VALUES: Readonly<Record<string, unknown>> = {};const EMPTY_RUNTIME_VALUE_STORE = {@@ -11,7 +15,7 @@ export const useRuntimeValues = (controls: SequenceRegistrationControls | null,): Readonly<Record<string, unknown>> => {const store = controls?.runtimeValues ?? EMPTY_RUNTIME_VALUE_STORE;- return useSyncExternalStore(+ return Internals.useSyncExternalStore(store.subscribe,store.getSnapshot,store.getSnapshot,@@ -32,7 +36,11 @@ export const useRuntimeStoreValue = (const store = storeOrNull ?? EMPTY_RUNTIME_VALUE_STORE;const getSnapshot = useCallback(() => store.getSnapshot()[key], [key, store]);- return useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);+ return Internals.useSyncExternalStore(+ store.subscribe,+ getSnapshot,+ getSnapshot,+ );};export const useRuntimeValueSelector = <T>({@@ -70,7 +78,7 @@ export const useRuntimeValueSelector = <T>({};}, [isEqual, selector, store]);- return useSyncExternalStore(+ return Internals.useSyncExternalStore(selectedStore.subscribe,selectedStore.getSnapshot,selectedStore.getSnapshot,@@ -114,7 +122,7 @@ export const useRuntimeValueSnapshots = (};}, [stores]);- return useSyncExternalStore(+ return Internals.useSyncExternalStore(aggregateStore.subscribe,aggregateStore.getSnapshot,aggregateStore.getSnapshot,diff --git a/packages/studio/src/state/timeline-sequence-hover.ts b/packages/studio/src/state/timeline-sequence-hover.tsindex f71140d854..f6f0e49fbc 100644--- a/packages/studio/src/state/timeline-sequence-hover.ts+++ b/packages/studio/src/state/timeline-sequence-hover.ts@@ -1,11 +1,6 @@import type {Dispatch, SetStateAction} from 'react';-import {- createContext,- useCallback,- useContext,- useMemo,- useSyncExternalStore,-} 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,@@ -57,7 +52,7 @@ export const TimelineSequenceHoverContext =export const useTimelineSequenceHoverState = () => {const store = useContext(TimelineSequenceHoverContext);- return useSyncExternalStore(+ return Internals.useSyncExternalStore(store.subscribe,store.getSnapshot,store.getSnapshot,@@ -76,7 +71,11 @@ export const useIsTimelineSequenceHovered = (nodePathKey: string | null) => {[nodePathKey, store],);- return useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);+ return Internals.useSyncExternalStore(+ store.subscribe,+ getSnapshot,+ getSnapshot,+ );};export const useTimelineSequenceHover = (