|
1 | | -import { getBridgeServer } from '@react-native-harness/bridge/server'; |
| 1 | +import { |
| 2 | + getBridgeServer, |
| 3 | + BridgeServer, |
| 4 | +} from '@react-native-harness/bridge/server'; |
2 | 5 | import { BridgeClientFunctions } from '@react-native-harness/bridge'; |
3 | | -import { HarnessPlatform } from '@react-native-harness/platforms'; |
4 | | -import { getMetroInstance } from '@react-native-harness/bundler-metro'; |
5 | | -import { InitializationTimeoutError } from './errors.js'; |
| 6 | +import { |
| 7 | + HarnessPlatform, |
| 8 | + HarnessPlatformRunner, |
| 9 | +} from '@react-native-harness/platforms'; |
| 10 | +import { |
| 11 | + getMetroInstance, |
| 12 | + Reporter, |
| 13 | + ReportableEvent, |
| 14 | +} from '@react-native-harness/bundler-metro'; |
| 15 | +import { InitializationTimeoutError, MaxAppRestartsError } from './errors.js'; |
6 | 16 | import { Config as HarnessConfig } from '@react-native-harness/config'; |
7 | | -import pRetry from 'p-retry'; |
8 | | - |
9 | | -const BRIDGE_READY_TIMEOUT = 10000; |
10 | 17 |
|
11 | 18 | export type Harness = { |
12 | 19 | runTests: BridgeClientFunctions['runTests']; |
13 | 20 | restart: () => Promise<void>; |
14 | 21 | dispose: () => Promise<void>; |
15 | 22 | }; |
16 | 23 |
|
| 24 | +export const waitForAppReady = async (options: { |
| 25 | + metroEvents: Reporter; |
| 26 | + serverBridge: BridgeServer; |
| 27 | + platformInstance: HarnessPlatformRunner; |
| 28 | + bundleStartTimeout: number; |
| 29 | + maxRestarts: number; |
| 30 | + signal: AbortSignal; |
| 31 | +}): Promise<void> => { |
| 32 | + const { |
| 33 | + metroEvents, |
| 34 | + serverBridge, |
| 35 | + platformInstance, |
| 36 | + bundleStartTimeout, |
| 37 | + maxRestarts, |
| 38 | + signal, |
| 39 | + } = options; |
| 40 | + |
| 41 | + let restartCount = 0; |
| 42 | + let isBundling = false; |
| 43 | + let bundleTimeoutId: NodeJS.Timeout | null = null; |
| 44 | + |
| 45 | + const clearBundleTimeout = () => { |
| 46 | + if (bundleTimeoutId) { |
| 47 | + clearTimeout(bundleTimeoutId); |
| 48 | + bundleTimeoutId = null; |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + return new Promise<void>((resolve, reject) => { |
| 53 | + // Handle abort signal |
| 54 | + signal.addEventListener('abort', () => { |
| 55 | + clearBundleTimeout(); |
| 56 | + reject(new DOMException('The operation was aborted', 'AbortError')); |
| 57 | + }); |
| 58 | + |
| 59 | + // Start/restart the bundle timeout |
| 60 | + const startBundleTimeout = () => { |
| 61 | + clearBundleTimeout(); |
| 62 | + bundleTimeoutId = setTimeout(() => { |
| 63 | + if (isBundling) return; // Don't restart while bundling |
| 64 | + |
| 65 | + if (restartCount >= maxRestarts) { |
| 66 | + cleanup(); |
| 67 | + reject(new MaxAppRestartsError(restartCount + 1)); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + restartCount++; |
| 72 | + platformInstance.restartApp().catch(reject); |
| 73 | + startBundleTimeout(); // Reset timer for next attempt |
| 74 | + }, bundleStartTimeout); |
| 75 | + }; |
| 76 | + |
| 77 | + // Metro event listener |
| 78 | + const onMetroEvent = (event: ReportableEvent) => { |
| 79 | + if (event.type === 'bundle_build_started') { |
| 80 | + isBundling = true; |
| 81 | + clearBundleTimeout(); // Cancel restart timer while bundling |
| 82 | + } else if ( |
| 83 | + event.type === 'bundle_build_done' || |
| 84 | + event.type === 'bundle_build_failed' |
| 85 | + ) { |
| 86 | + isBundling = false; |
| 87 | + startBundleTimeout(); // Reset timer after bundle completes |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + // Bridge ready listener |
| 92 | + const onReady = () => { |
| 93 | + cleanup(); |
| 94 | + resolve(); |
| 95 | + }; |
| 96 | + |
| 97 | + const cleanup = () => { |
| 98 | + clearBundleTimeout(); |
| 99 | + metroEvents.removeListener(onMetroEvent); |
| 100 | + serverBridge.off('ready', onReady); |
| 101 | + }; |
| 102 | + |
| 103 | + // Setup listeners |
| 104 | + metroEvents.addListener(onMetroEvent); |
| 105 | + serverBridge.once('ready', onReady); |
| 106 | + |
| 107 | + // Start the app and timeout |
| 108 | + platformInstance.restartApp().catch(reject); |
| 109 | + startBundleTimeout(); |
| 110 | + }); |
| 111 | +}; |
| 112 | + |
17 | 113 | const getHarnessInternal = async ( |
18 | 114 | config: HarnessConfig, |
19 | 115 | platform: HarnessPlatform, |
@@ -46,23 +142,14 @@ const getHarnessInternal = async ( |
46 | 142 | } |
47 | 143 |
|
48 | 144 | try { |
49 | | - await pRetry( |
50 | | - () => |
51 | | - new Promise<void>((resolve, reject) => { |
52 | | - signal.addEventListener('abort', () => { |
53 | | - reject(new DOMException('The operation was aborted', 'AbortError')); |
54 | | - }); |
55 | | - |
56 | | - serverBridge.once('ready', () => resolve()); |
57 | | - platformInstance.restartApp().catch(reject); |
58 | | - }), |
59 | | - { |
60 | | - minTimeout: BRIDGE_READY_TIMEOUT, |
61 | | - maxTimeout: BRIDGE_READY_TIMEOUT, |
62 | | - retries: Infinity, |
63 | | - signal, |
64 | | - } |
65 | | - ); |
| 145 | + await waitForAppReady({ |
| 146 | + metroEvents: metroInstance.events, |
| 147 | + serverBridge, |
| 148 | + platformInstance: platformInstance as HarnessPlatformRunner, |
| 149 | + bundleStartTimeout: config.bundleStartTimeout, |
| 150 | + maxRestarts: config.maxAppRestarts, |
| 151 | + signal, |
| 152 | + }); |
66 | 153 | } catch (error) { |
67 | 154 | await dispose(); |
68 | 155 | throw error; |
|
0 commit comments