🎥 Make videos programmatically with React
fix(studio): keep default props watchers scoped to each client file
JonnyBurgeraf12e1dc8a2c
diff --git a/packages/studio-server/src/preview-server/default-props-watchers.ts b/packages/studio-server/src/preview-server/default-props-watchers.tsindex 273330476d..52517637de 100644--- a/packages/studio-server/src/preview-server/default-props-watchers.ts+++ b/packages/studio-server/src/preview-server/default-props-watchers.ts@@ -9,25 +9,27 @@ import {computeCanUpdateDefaultPropsFromContent,} from './routes/can-update-default-props';-// Per-compositionId watcher on the file that registers the composition+// Each client watches its own resolved registration file or root-file fallback.const watchers: Record<string,- {- unwatch: () => void;- compositionFile: string;- clientIds: Set<string>;- }+ Map<string, {unwatch: () => void; clientIds: Set<string>}>> = {};const removeClient = (compositionId: string, clientId: string) => {- const watcher = watchers[compositionId];- if (!watcher?.clientIds.has(clientId)) {+ const compositionWatchers = watchers[compositionId];+ if (!compositionWatchers) {return;}- watcher.clientIds.delete(clientId);- if (watcher.clientIds.size === 0) {- watcher.unwatch();+ for (const [file, watcher] of compositionWatchers) {+ watcher.clientIds.delete(clientId);+ if (watcher.clientIds.size === 0) {+ watcher.unwatch();+ compositionWatchers.delete(file);+ }+ }++ if (compositionWatchers.size === 0) {delete watchers[compositionId];}};@@ -59,10 +61,10 @@ export const subscribeToDefaultPropsWatchers = async ({return result;}- // The composition may have moved to another file since the watcher was installed- if (watchers[compositionId]?.compositionFile !== compositionFile) {- watchers[compositionId]?.unwatch();- const clientIds = new Set(watchers[compositionId]?.clientIds);+ watchers[compositionId] ??= new Map();+ const compositionWatchers = watchers[compositionId];+ if (!compositionWatchers.has(compositionFile)) {+ const clientIds = new Set<string>();const {unwatch} = installFileWatcher({file: compositionFile,existenceOnly: false,@@ -88,10 +90,10 @@ export const subscribeToDefaultPropsWatchers = async ({});},});- watchers[compositionId] = {unwatch, compositionFile, clientIds};+ compositionWatchers.set(compositionFile, {unwatch, clientIds});}- watchers[compositionId].clientIds.add(clientId);+ compositionWatchers.get(compositionFile)!.clientIds.add(clientId);return result;};diff --git a/packages/studio-server/src/test/default-props-file.test.ts b/packages/studio-server/src/test/default-props-file.test.tsindex 81db251608..36a30d8cac 100644--- a/packages/studio-server/src/test/default-props-file.test.ts+++ b/packages/studio-server/src/test/default-props-file.test.ts@@ -214,7 +214,7 @@ test('saving default props only writes and undoes the registering file', async (}});-test('watches the registration file the client sends', async () => {+test('keeps each client subscribed to its own default-props file', async () => {const {remotionRoot, entryPoint, files} = makeProject();const events: unknown[] = [];const cleanupFileWatcher = setFileWatcherRegistry(@@ -239,6 +239,20 @@ test('watches the registration file the client sends', async () => {remotionRoot,entryPoint,});+ await subscribeToDefaultPropsWatchers({+ compositionId: 'TitlePanel',+ clientId: 'fallback-client',+ symbolicatedStack: null,+ remotionRoot,+ entryPoint,+ });+ await subscribeToDefaultPropsWatchers({+ compositionId: 'TitlePanel',+ clientId: 'wrong-file-client',+ symbolicatedStack: stackFor('src/nested/Other.tsx'),+ remotionRoot,+ entryPoint,+ });writeFileAndNotifyFileWatchers({file: files.root,@@ -246,6 +260,22 @@ test('watches the registration file the client sends', async () => {originatorClientId: undefined,metadata: null,});+ await Promise.resolve();+ expect(events).toEqual([+ {+ clientId: 'fallback-client',+ event: {+ type: 'default-props-updatable-changed',+ compositionId: 'TitlePanel',+ result: {+ canUpdate: false,+ reason:+ 'Could not find or extract defaultProps for composition "TitlePanel"',+ },+ },+ },+ ]);+ events.length = 0;writeFileAndNotifyFileWatchers({file: files.showcase,content: showcaseContents.replace("'Left Trim'", "'Right Trim'"),@@ -267,8 +297,47 @@ test('watches the registration file the client sends', async () => {},},]);++ // Resolving the fallback client's stack moves only that client's subscription.+ await subscribeToDefaultPropsWatchers({+ compositionId: 'TitlePanel',+ clientId: 'fallback-client',+ symbolicatedStack: stackFor('src/Showcase.tsx'),+ remotionRoot,+ entryPoint,+ });+ unsubscribeClientDefaultPropsWatchers('client');+ events.length = 0;+ writeFileAndNotifyFileWatchers({+ file: files.root,+ content: rootContents,+ originatorClientId: undefined,+ metadata: null,+ });+ writeFileAndNotifyFileWatchers({+ file: files.showcase,+ content: showcaseContents,+ originatorClientId: undefined,+ metadata: null,+ });+ await Promise.resolve();+ expect(events).toEqual([+ {+ clientId: 'fallback-client',+ event: {+ type: 'default-props-updatable-changed',+ compositionId: 'TitlePanel',+ result: {+ canUpdate: true,+ currentDefaultProps: {children: 'Left Trim', style: null},+ },+ },+ },+ ]);} finally {unsubscribeClientDefaultPropsWatchers('client');+ unsubscribeClientDefaultPropsWatchers('fallback-client');+ unsubscribeClientDefaultPropsWatchers('wrong-file-client');cleanupLiveEvents();cleanupFileWatcher();rmSync(remotionRoot, {recursive: true, force: true});