Skip to content

Commit 8ccb513

Browse files
fix(runtime): block harness init until HMR is disabled
Ensure we only report harness ready after HMR has been disabled. This prevents Metro from pushing updates mid-run if tests change during execution.
1 parent 81078fa commit 8ccb513

3 files changed

Lines changed: 25 additions & 18 deletions

File tree

packages/runtime/src/__tests__/initialize.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ describe('initialize', () => {
1515
// ok
1616
});
1717

18-
disableHMRWhenReady(disable, 50);
18+
const promise = disableHMRWhenReady(disable, 50);
1919
await vi.runAllTimersAsync();
20+
await promise;
2021

2122
expect(disable).toHaveBeenCalledTimes(2);
2223
});

packages/runtime/src/disableHMRWhenReady.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@ export function disableHMRWhenReady(
33
retriesLeft: number,
44
schedule: (cb: () => void) => void = (cb) => setTimeout(cb, 0),
55
) {
6-
try {
7-
disable();
8-
} catch (error) {
9-
if (
10-
retriesLeft > 0 &&
11-
error instanceof Error &&
12-
error.message.includes('Expected HMRClient.setup() call at startup.')
13-
) {
14-
schedule(() => disableHMRWhenReady(disable, retriesLeft - 1, schedule));
15-
return;
6+
return new Promise<void>((resolve, reject) => {
7+
function attempt(remaining: number) {
8+
try {
9+
disable();
10+
resolve();
11+
} catch (error) {
12+
if (
13+
remaining > 0 &&
14+
error instanceof Error &&
15+
error.message.includes('Expected HMRClient.setup() call at startup.')
16+
) {
17+
schedule(() => attempt(remaining - 1));
18+
return;
19+
}
20+
21+
reject(error);
22+
}
1623
}
1724

18-
throw error;
19-
}
25+
attempt(retriesLeft);
26+
});
2027
}

packages/runtime/src/initialize.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ const HMRClient =
2222

2323
// Wait for HMRClient to be initialized
2424
setTimeout(() => {
25-
disableHMRWhenReady(() => HMRClient.disable(), 50);
26-
27-
// Initialize the React Native Harness
28-
void getClient().then((client) =>
29-
client.rpc.reportReady(getDeviceDescriptor())
25+
void disableHMRWhenReady(() => HMRClient.disable(), 50).then(() =>
26+
getClient().then((client) =>
27+
client.rpc.reportReady(getDeviceDescriptor())
28+
)
3029
);
3130
});
3231

0 commit comments

Comments
 (0)