🎥 Make videos programmatically with React
Group sequence subscriptions by source file to reuse parsed snapshots
Jonny Burger216088164d05
diff --git a/packages/studio-server/src/preview-server/routes/subscribe-to-sequence-props.ts b/packages/studio-server/src/preview-server/routes/subscribe-to-sequence-props.tsindex a35b5c83aa..851e5151ba 100644--- a/packages/studio-server/src/preview-server/routes/subscribe-to-sequence-props.ts+++ b/packages/studio-server/src/preview-server/routes/subscribe-to-sequence-props.ts@@ -1,3 +1,4 @@+import path from 'node:path';import type {SubscribeToSequencePropsBatchRequest,SubscribeToSequencePropsBatchResponse,@@ -13,42 +14,46 @@ export const subscribeToSequenceProps: ApiHandler<> = ({input, remotionRoot, logLevel}) => {// Source contents are shared only within this synchronous subscription batch.const fileContentsCache = new Map<string, string>();- const unresolvedByFile = new Map<- string,- {index: number; line: number; column: number}[]- >();+ const requestsByFile = new Map<string, number[]>();for (const [index, request] of input.requests.entries()) {- if (request.nodePath !== null) {- continue;- }-- const unresolved = unresolvedByFile.get(request.fileName) ?? [];- unresolved.push({index, line: request.line, column: request.column});- unresolvedByFile.set(request.fileName, unresolved);+ const absolutePath = path.resolve(remotionRoot, request.fileName);+ const indexes = requestsByFile.get(absolutePath) ?? [];+ indexes.push(index);+ requestsByFile.set(absolutePath, indexes);}- const resolvedNodePaths = new Map<number, SequenceNodePath>();- for (const [fileName, unresolved] of unresolvedByFile) {- try {- const nodePaths = resolveSequencePropsNodePathsFromFilename({- fileName,- targets: unresolved,- remotionRoot,- fileContentsCache,- });- for (const [index, nodePath] of nodePaths.entries()) {- if (nodePath) {- resolvedNodePaths.set(unresolved[index].index, nodePath);+ const results: SubscribeToSequencePropsBatchResponse['results'] = [];+ // Finish each file before moving on so large batches do not evict and+ // repeatedly parse the same source snapshot from the bounded AST cache.+ for (const indexes of requestsByFile.values()) {+ const unresolved = indexes+ .filter((index) => input.requests[index].nodePath === null)+ .map((index) => ({+ index,+ line: input.requests[index].line,+ column: input.requests[index].column,+ }));+ const resolvedNodePaths = new Map<number, SequenceNodePath>();+ if (unresolved.length > 0) {+ try {+ const nodePaths = resolveSequencePropsNodePathsFromFilename({+ fileName: input.requests[indexes[0]].fileName,+ targets: unresolved,+ remotionRoot,+ fileContentsCache,+ });+ for (const [index, nodePath] of nodePaths.entries()) {+ if (nodePath) {+ resolvedNodePaths.set(unresolved[index].index, nodePath);+ }}+ } catch {+ // Let each subscription produce its usual not-found response.}- } catch {- // Let each subscription produce its usual not-found response.}- }- const results = input.requests.map(- (- {+ for (const index of indexes) {+ const {fileName,line,column,@@ -58,10 +63,8 @@ export const subscribeToSequenceProps: ApiHandler<assetKeys,effects,clientId,- },- index,- ) =>- subscribeToSequencePropsWatchers({+ } = input.requests[index];+ results[index] = subscribeToSequencePropsWatchers({fileName,line,column,@@ -76,7 +79,9 @@ export const subscribeToSequenceProps: ApiHandler<logLevel,fileContentsCache,- }),- );+ });+ }+ }+return Promise.resolve({results});};