Skip to content

Commit 81ec0d6

Browse files
make buildVM returns the built vm
1 parent e82109c commit 81ec0d6

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

react_on_rails_pro/packages/node-renderer/src/worker/vm.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ interface VMContext {
3939
const vmContexts = new Map<string, VMContext>();
4040

4141
// Track VM creation promises to handle concurrent buildVM requests
42-
const vmCreationPromises = new Map<string, Promise<boolean>>();
42+
const vmCreationPromises = new Map<string, Promise<VMContext>>();
4343

4444
/**
4545
* Returns all bundle paths that have a VM context
@@ -178,18 +178,18 @@ ${smartTrim(result)}`);
178178
}
179179
}
180180

181-
export async function buildVM(filePath: string) {
181+
export async function buildVM(filePath: string): Promise<VMContext> {
182182
// Return existing promise if VM is already being created
183183
if (vmCreationPromises.has(filePath)) {
184-
return vmCreationPromises.get(filePath);
184+
return vmCreationPromises.get(filePath) as Promise<VMContext>;
185185
}
186186

187187
// Check if VM for this bundle already exists
188188
const vmContext = vmContexts.get(filePath);
189189
if (vmContext) {
190190
// Update last used time when accessing existing VM
191191
vmContext.lastUsed = Date.now();
192-
return Promise.resolve(true);
192+
return Promise.resolve(vmContext);
193193
}
194194

195195
// Create a new promise for this VM creation
@@ -306,11 +306,12 @@ export async function buildVM(filePath: string) {
306306
}
307307

308308
// Only now, after VM is fully initialized, store the context
309-
vmContexts.set(filePath, {
309+
const newVmContext: VMContext = {
310310
context,
311311
sharedConsoleHistory,
312312
lastUsed: Date.now(),
313-
});
313+
};
314+
vmContexts.set(filePath, newVmContext);
314315

315316
// Manage pool size after adding new VM
316317
manageVMPoolSize();
@@ -331,7 +332,7 @@ export async function buildVM(filePath: string) {
331332
);
332333
}
333334

334-
return true;
335+
return newVmContext;
335336
} catch (error) {
336337
log.error('Caught Error when creating context in buildVM, %O', error);
337338
errorReporter.error(error as Error);

react_on_rails_pro/packages/node-renderer/tests/helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ export function vmSecondaryBundlePath(testName: string) {
5959

6060
export async function createVmBundle(testName: string) {
6161
await safeCopyFileAsync(getFixtureBundle(), vmBundlePath(testName));
62-
return buildVM(vmBundlePath(testName));
62+
await buildVM(vmBundlePath(testName));
6363
}
6464

6565
export async function createSecondaryVmBundle(testName: string) {
6666
await safeCopyFileAsync(getFixtureSecondaryBundle(), vmSecondaryBundlePath(testName));
67-
return buildVM(vmSecondaryBundlePath(testName));
67+
await buildVM(vmSecondaryBundlePath(testName));
6868
}
6969

7070
export function lockfilePath(testName: string) {

react_on_rails_pro/packages/node-renderer/tests/serverRenderRSCReactComponent.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ describe('serverRenderRSCReactComponent', () => {
6363
// Therefore, we cannot call it directly in the test files. Instead, we run the RSC bundle through the VM and call the method from there.
6464
const getReactOnRailsRSCObject = async () => {
6565
// Use the copied rsc-bundle.js file from temp directory
66-
await buildVM(tempRscBundlePath);
67-
const vmContext = getVMContext(tempRscBundlePath);
66+
const vmContext = await buildVM(tempRscBundlePath);
6867
const { ReactOnRails, React } = vmContext.context;
6968

7069
function SuspensedComponentWithAsyncError() {

0 commit comments

Comments
 (0)