Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion private/react-native-fantom/runner/bundling.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export async function createBundle(options: BundleOptions): Promise<void> {
let lastBundleResult;
let lastBundleError;

const bundleURL = getBundleURL(options);

// Retry in case Metro hasn't seen the changes in the filesystem yet.
// TODO(T231910841): Remove this when Metro fixes consistency issues when resolving HTTP requests.
let attemps = 0;
Expand All @@ -40,7 +42,7 @@ export async function createBundle(options: BundleOptions): Promise<void> {
lastBundleResult = null;

try {
lastBundleResult = await fetch(getBundleURL(options));
lastBundleResult = await fetch(bundleURL);
} catch (e) {
lastBundleError = e;
}
Expand Down Expand Up @@ -72,6 +74,15 @@ export async function createBundle(options: BundleOptions): Promise<void> {
await lastBundleResult.text(),
'utf8',
);

// Each test uses a unique entrypoint, so the bundle graph will never be
// requested again. Send DELETE to evict Metro's cached dependency graph
// and delta calculator for this bundle, freeing the memory.
try {
await fetch(bundleURL, {method: 'DELETE'});
} catch {
// Best-effort cleanup — don't fail the test if eviction fails.
}
}

export async function createSourceMap(options: BundleOptions): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type MetroServer = NonNullable<RunServerResult?.['httpServer']>;

declare var __FANTOM_METRO_SERVER__: ?RunServerResult;

const SHUTDOWN_TIMEOUT_MS = 5_000;

function getMetroServer(): ?MetroServer {
return typeof __FANTOM_METRO_SERVER__ !== 'undefined' &&
__FANTOM_METRO_SERVER__ != null
Expand All @@ -32,7 +34,11 @@ export default async function globalTeardown(
}

async function stopMetroServer(metroServer: MetroServer): Promise<void> {
return new Promise((resolve, reject) => {
if (!metroServer.listening) {
return;
}

const closed = new Promise<void>((resolve, reject) => {
metroServer.close(error => {
if (error) {
reject(error);
Expand All @@ -41,4 +47,21 @@ async function stopMetroServer(metroServer: MetroServer): Promise<void> {
}
});
});

// $FlowFixMe[method-unbinding] Node 18.2+ API
if (typeof metroServer.closeIdleConnections === 'function') {
metroServer.closeIdleConnections();
}

const timeout = new Promise<void>(resolve => {
setTimeout(() => {
// $FlowFixMe[method-unbinding] Node 18.2+ API
if (typeof metroServer.closeAllConnections === 'function') {
metroServer.closeAllConnections();
}
resolve();
}, SHUTDOWN_TIMEOUT_MS);
});

await Promise.race([closed, timeout]);
}
Loading