Skip to content

πŸŽ₯ Make videos programmatically with React

dedupe

JonnyBurger739058bad359
diff --git a/packages/studio/src/components/Timeline/sequence-props-subscription-store.ts b/packages/studio/src/components/Timeline/sequence-props-subscription-store.ts
new file mode 100644
index 0000000000..49f571af42
--- /dev/null
+++ b/packages/studio/src/components/Timeline/sequence-props-subscription-store.ts
@@ -0,0 +1,123 @@
+import type {SequenceSchema} from 'remotion';
+import {Internals} from 'remotion';
+import {callApi} from '../call-api';
+
+type Key = string;
+
+const makeKey = (fileName: string, line: number, column: number): Key =>
+ `${fileName}\0${line}\0${column}`;
+
+type SubscribeResult = Awaited<
+ ReturnType<typeof callApi<'/api/subscribe-to-sequence-props'>>
+>;
+
+type ApplyResult = (result: SubscribeResult) => void;
+
+type Entry = {
+ refCount: number;
+ promise: Promise<SubscribeResult>;
+ fileName: string;
+ clientId: string;
+ apply: ApplyResult | null;
+};
+
+const entries = new Map<Key, Entry>();
+
+export const acquireSequencePropsSubscription = ({
+ fileName,
+ line,
+ column,
+ schema,
+ clientId,
+ apply,
+}: {
+ fileName: string;
+ line: number;
+ column: number;
+ schema: SequenceSchema;
+ clientId: string;
+ apply: ApplyResult;
+}): {release: () => void} => {
+ const key = makeKey(fileName, line, column);
+ let entry = entries.get(key);
+
+ if (!entry) {
+ const promise = callApi('/api/subscribe-to-sequence-props', {
+ fileName,
+ line,
+ column,
+ schema,
+ clientId,
+ });
+ const created: Entry = {
+ refCount: 0,
+ promise,
+ fileName,
+ clientId,
+ apply,
+ };
+ entries.set(key, created);
+ entry = created;
+
+ promise
+ .then((result) => {
+ const current = entries.get(key);
+ if (current !== created || !current.apply) {
+ return;
+ }
+
+ const cb = current.apply;
+ current.apply = null;
+ cb(result);
+ })
+ .catch((err) => {
+ const current = entries.get(key);
+ if (current !== created) {
+ return;
+ }
+
+ current.apply = null;
+ Internals.Log.error(err);
+ });
+ }
+
+ entry.refCount++;
+ const acquired = entry;
+
+ let released = false;
+ return {
+ release: () => {
+ if (released) {
+ return;
+ }
+
+ released = true;
+ acquired.refCount--;
+
+ if (acquired.refCount > 0) {
+ return;
+ }
+
+ if (entries.get(key) === acquired) {
+ entries.delete(key);
+ }
+
+ acquired.promise
+ .then((result) => {
+ if (!result.canUpdate) {
+ return;
+ }
+
+ return callApi('/api/unsubscribe-from-sequence-props', {
+ fileName: acquired.fileName,
+ nodePath: result.nodePath,
+ clientId: acquired.clientId,
+ });
+ })
+ .catch(() => {
+ // Ignore β€” either the subscribe failed (nothing to clean up) or
+ // the unsubscribe failed (server-side TTL will handle it).
+ });
+ },
+ };
+};
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 07cf05834d..5e20d49610 100644
--- a/packages/studio/src/components/Timeline/use-sequence-props-subscription.ts
+++ b/packages/studio/src/components/Timeline/use-sequence-props-subscription.ts
@@ -1,9 +1,9 @@
-import {useContext, useEffect, useMemo, useRef} from 'react';
+import {useContext, useEffect, useMemo} from 'react';
import {Internals} from 'remotion';
-import type {SequenceSchema, SequenceNodePath} 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 {callApi} from '../call-api';
+import {acquireSequencePropsSubscription} from './sequence-props-subscription-store';
export const useSequencePropsSubscription = (
overrideId: string,
@@ -35,15 +35,6 @@ export const useSequencePropsSubscription = (
};
}, [originalLocation]);
- const isMountedRef = useRef(true);
-
- useEffect(() => {
- isMountedRef.current = true;
- return () => {
- isMountedRef.current = false;
- };
- }, []);
-
const locationSource = validatedLocation?.source ?? null;
const locationLine = validatedLocation?.line ?? null;
const locationColumn = validatedLocation?.column ?? null;
@@ -63,39 +54,27 @@ export const useSequencePropsSubscription = (
return;
}
- let resolvedNodePath: SequenceNodePath | null = null;
- callApi('/api/subscribe-to-sequence-props', {
+ const {release} = acquireSequencePropsSubscription({
fileName: locationSource,
line: locationLine,
column: locationColumn,
schema,
clientId,
- })
- .then((result) => {
- if (result.canUpdate) {
- resolvedNodePath = result.nodePath;
-
- setOverrideIdToNodePath(overrideId, {
- nodePath: result.nodePath,
- jsxInMapCallback: result.jsxInMapCallback,
- });
- setCodeValues(result.nodePath, result.props);
+ apply: (result) => {
+ if (!result.canUpdate) {
+ return;
}
- })
- .catch((err) => {
- Internals.Log.error(err);
- });
- return () => {
- if (resolvedNodePath) {
- callApi('/api/unsubscribe-from-sequence-props', {
- fileName: locationSource,
- nodePath: resolvedNodePath,
- clientId,
- }).catch(() => {
- // Ignore unsubscribe errors
+ setOverrideIdToNodePath(overrideId, {
+ nodePath: result.nodePath,
+ jsxInMapCallback: result.jsxInMapCallback,
});
- }
+ setCodeValues(result.nodePath, result.props);
+ },
+ });
+
+ return () => {
+ release();
};
}, [
clientId,