Skip to content

🎥 Make videos programmatically with React

simplify

JonnyBurger68ebb2d777a0
diff --git a/packages/studio/src/components/Timeline/SequencePropsObserver.tsx b/packages/studio/src/components/Timeline/SequencePropsObserver.tsx
new file mode 100644
index 0000000000..1965f94d57
--- /dev/null
+++ b/packages/studio/src/components/Timeline/SequencePropsObserver.tsx
@@ -0,0 +1,29 @@
+import type {EventSourceEvent} from '@remotion/studio-shared';
+import {useContext, useEffect} from 'react';
+import {Internals} from 'remotion';
+import {StudioServerConnectionCtx} from '../../helpers/client-id';
+
+export const SequencePropsObserver = () => {
+ const {subscribeToEvent} = useContext(StudioServerConnectionCtx);
+ const {setCodeValues} = useContext(Internals.VisualModeSettersContext);
+
+ useEffect(() => {
+ const handleEvent = (event: EventSourceEvent) => {
+ if (event.type !== 'sequence-props-updated') {
+ return;
+ }
+
+ if (event.result.canUpdate) {
+ setCodeValues(event.nodePath, event.result.props);
+ } else {
+ setCodeValues(event.nodePath, null);
+ }
+ };
+
+ const unsubscribe = subscribeToEvent('sequence-props-updated', handleEvent);
+
+ return () => {
+ unsubscribe();
+ };
+ }, [setCodeValues, subscribeToEvent]);
+};
diff --git a/packages/studio/src/components/Timeline/sequence-props-subscription-store.ts b/packages/studio/src/components/Timeline/sequence-props-subscription-store.ts
index 20bbbf8b7b..8165cfe0fc 100644
--- a/packages/studio/src/components/Timeline/sequence-props-subscription-store.ts
+++ b/packages/studio/src/components/Timeline/sequence-props-subscription-store.ts
@@ -1,108 +1,8 @@
-import type {EventSourceEvent} from '@remotion/studio-shared';
-import {Internals, type CanUpdateSequencePropStatus} from 'remotion';
+import type {CanUpdateSequencePropsResponse} from '@remotion/studio-shared';
+import {Internals} from 'remotion';
import type {SequenceSchema, SequenceNodePath} from 'remotion';
import {callApi} from '../call-api';
-export type SequencePropsSnapshot = {
- nodePath: SequenceNodePath | null;
- jsxInMapCallback: boolean;
- props: Record<string, CanUpdateSequencePropStatus> | null;
-};
-
-const INITIAL_SNAPSHOT: SequencePropsSnapshot = {
- nodePath: null,
- jsxInMapCallback: false,
- props: null,
-};
-
-type SubscribeToEvent = (
- type: EventSourceEvent['type'],
- listener: (event: EventSourceEvent) => void,
-) => () => void;
-
-type Entry = {
- clientId: string;
- fileName: string;
- refCount: number;
- snapshot: SequencePropsSnapshot;
- listeners: Set<(s: SequencePropsSnapshot) => void>;
- tornDown: boolean;
-};
-
-const entries = new Map<string, Entry>();
-let globalUnsubscribe: (() => void) | null = null;
-
-const makeKey = (fileName: string, line: number, column: number): string =>
- `${fileName}|${line}|${column}`;
-
-const nodePathsEqual = (
- a: SequenceNodePath | null,
- b: SequenceNodePath | null,
-): boolean => {
- if (!a || !b) {
- return false;
- }
-
- return JSON.stringify(a) === JSON.stringify(b);
-};
-
-const notify = (entry: Entry) => {
- for (const listener of entry.listeners) {
- listener(entry.snapshot);
- }
-};
-
-const handleEvent = (event: EventSourceEvent) => {
- if (event.type !== 'sequence-props-updated') {
- return;
- }
-
- for (const entry of entries.values()) {
- if (entry.fileName !== event.fileName) {
- continue;
- }
-
- if (!nodePathsEqual(entry.snapshot.nodePath, event.nodePath)) {
- continue;
- }
-
- if (event.result.canUpdate) {
- entry.snapshot = {
- nodePath: entry.snapshot.nodePath,
- jsxInMapCallback: event.result.jsxInMapCallback,
- props: event.result.props,
- };
- } else {
- entry.snapshot = {
- nodePath: entry.snapshot.nodePath,
- jsxInMapCallback: false,
- props: null,
- };
- }
-
- notify(entry);
- }
-};
-
-const ensureGlobalListener = (subscribeToEvent: SubscribeToEvent) => {
- if (globalUnsubscribe) {
- return;
- }
-
- globalUnsubscribe = subscribeToEvent('sequence-props-updated', handleEvent);
-};
-
-const teardownGlobalListenerIfEmpty = () => {
- if (entries.size > 0) {
- return;
- }
-
- if (globalUnsubscribe) {
- globalUnsubscribe();
- globalUnsubscribe = null;
- }
-};
-
const fireUnsubscribe = (
fileName: string,
nodePath: SequenceNodePath,
@@ -123,7 +23,6 @@ export const acquireSequencePropsSubscription = ({
line,
column,
schema,
- subscribeToEvent,
onChange,
}: {
clientId: string;
@@ -131,92 +30,30 @@ export const acquireSequencePropsSubscription = ({
line: number;
column: number;
schema: SequenceSchema;
- subscribeToEvent: SubscribeToEvent;
- onChange: (snapshot: SequencePropsSnapshot) => void;
+ onChange: (snapshot: CanUpdateSequencePropsResponse) => void;
}): (() => void) => {
- const key = makeKey(fileName, line, column);
- let entry = entries.get(key);
-
- if (!entry) {
- const newEntry: Entry = {
- clientId,
- fileName,
- refCount: 0,
- snapshot: INITIAL_SNAPSHOT,
- listeners: new Set(),
- tornDown: false,
- };
- entries.set(key, newEntry);
- ensureGlobalListener(subscribeToEvent);
+ let resolvedNodePath: SequenceNodePath | null = null;
+ callApi('/api/subscribe-to-sequence-props', {
+ fileName,
+ line,
+ column,
+ schema,
+ clientId,
+ })
+ .then((result) => {
+ if (result.canUpdate) {
+ resolvedNodePath = result.nodePath;
+ }
- callApi('/api/subscribe-to-sequence-props', {
- fileName,
- line,
- column,
- schema,
- clientId,
+ onChange(result);
})
- .then((result) => {
- if (newEntry.tornDown) {
- if (result.canUpdate) {
- fireUnsubscribe(fileName, result.nodePath, clientId);
- }
-
- return;
- }
-
- if (result.canUpdate) {
- newEntry.snapshot = {
- nodePath: result.nodePath,
- jsxInMapCallback: result.jsxInMapCallback,
- props: result.props,
- };
- } else {
- newEntry.snapshot = INITIAL_SNAPSHOT;
- }
+ .catch((err) => {
+ Internals.Log.error(err);
+ });
- notify(newEntry);
- })
- .catch((err) => {
- if (newEntry.tornDown) {
- return;
- }
-
- Internals.Log.error(err);
- newEntry.snapshot = INITIAL_SNAPSHOT;
- notify(newEntry);
- });
-
- entry = newEntry;
- }
-
- entry.refCount += 1;
- entry.listeners.add(onChange);
- onChange(entry.snapshot);
-
- let released = false;
return () => {
- if (released) {
- return;
- }
-
- released = true;
- const e = entry as Entry;
- e.listeners.delete(onChange);
- e.refCount -= 1;
-
- if (e.refCount > 0) {
- return;
- }
-
- e.tornDown = true;
- const resolvedNodePath = e.snapshot.nodePath;
- entries.delete(key);
-
if (resolvedNodePath) {
- fireUnsubscribe(e.fileName, resolvedNodePath, e.clientId);
+ fireUnsubscribe(fileName, resolvedNodePath, clientId);
}
-
- teardownGlobalListenerIfEmpty();
};
};
diff --git a/packages/studio/src/components/Timeline/use-sequence-props-subscription.ts b/packages/studio/src/components/Timeline/use-sequence-props-subscription.ts
index 5e1c4e596f..86985e6d28 100644
--- a/packages/studio/src/components/Timeline/use-sequence-props-subscription.ts
+++ b/packages/studio/src/components/Timeline/use-sequence-props-subscription.ts
@@ -1,12 +1,10 @@
-import {useCallback, useContext, useEffect, useMemo, useRef} from 'react';
-import {Internals, type CanUpdateSequencePropStatus} from 'remotion';
-import type {SequenceSchema, NodePathsState, SequenceNodePath} from 'remotion';
+import type {CanUpdateSequencePropsResponse} from '@remotion/studio-shared';
+import {useContext, useEffect, useMemo, useRef} from 'react';
+import {Internals} from 'remotion';
+import type {SequenceSchema} from 'remotion';
import type {OriginalPosition} from '../../error-overlay/react-overlay/utils/get-source-map';
import {StudioServerConnectionCtx} from '../../helpers/client-id';
-import {
- acquireSequencePropsSubscription,
- type SequencePropsSnapshot,
-} from './sequence-props-subscription-store';
+import {acquireSequencePropsSubscription} from './sequence-props-subscription-store';
export const useSequencePropsSubscription = (
overrideId: string,
@@ -19,26 +17,7 @@ export const useSequencePropsSubscription = (
Internals.OverrideIdsToNodePathsSettersContext,
);
- const setPropStatusesForSequence = useCallback(
- (
- nodePath: SequenceNodePath,
- statuses: Record<string, CanUpdateSequencePropStatus> | null,
- ) => {
- setCodeValues(nodePath, statuses);
- },
- [setCodeValues],
- );
-
- const setSubscriptionStateForSequence = useCallback(
- (nextState: NodePathsState) => {
- setOverrideIdToNodePath(overrideId, nextState);
- },
- [overrideId, setOverrideIdToNodePath],
- );
-
- const {previewServerState: state, subscribeToEvent} = useContext(
- StudioServerConnectionCtx,
- );
+ const {previewServerState: state} = useContext(StudioServerConnectionCtx);
const clientId = state.type === 'connected' ? state.clientId : undefined;
const validatedLocation = useMemo(() => {
@@ -85,13 +64,13 @@ export const useSequencePropsSubscription = (
return;
}
- const onChange = (snapshot: SequencePropsSnapshot) => {
- setSubscriptionStateForSequence({
- nodePath: snapshot.nodePath,
- jsxInMapCallback: snapshot.jsxInMapCallback,
- });
- if (snapshot.nodePath) {
- setPropStatusesForSequence(snapshot.nodePath, snapshot.props);
+ const onChange = (snapshot: CanUpdateSequencePropsResponse) => {
+ if (snapshot.canUpdate) {
+ setOverrideIdToNodePath(overrideId, {
+ nodePath: snapshot.nodePath,
+ jsxInMapCallback: snapshot.jsxInMapCallback,
+ });
+ setCodeValues(snapshot.nodePath, snapshot.props);
}
};
@@ -101,7 +80,6 @@ export const useSequencePropsSubscription = (
line: locationLine,
column: locationColumn,
schema,
- subscribeToEvent,
onChange,
});
@@ -115,10 +93,10 @@ export const useSequencePropsSubscription = (
locationColumn,
locationLine,
locationSource,
+ overrideId,
schema,
- setPropStatusesForSequence,
- setSubscriptionStateForSequence,
- subscribeToEvent,
+ setCodeValues,
+ setOverrideIdToNodePath,
visualModeEnabled,
]);
};