MCPcopy Index your code
hub / github.com/NativeScript/NativeScript / withEvents

Function withEvents

packages/vite/hmr/client/index.ts:1270–1317  ·  view source on GitHub ↗
(ms = 6000)

Source from the content-addressed store, hash-verified

1268 return pred();
1269 };
1270 const withEvents = async (ms = 6000) => {
1271 try {
1272 const a: any = (App as any).android;
1273 if (!a || typeof a.on !== 'function' || typeof a.off !== 'function') {
1274 if (VERBOSE) console.log('[hmr-client] [android] events API not available; polling');
1275 return await waitFor(isReady, ms, 60);
1276 }
1277 if (isReady()) {
1278 if (VERBOSE) console.log('[hmr-client] [android] activity already ready');
1279 return true;
1280 }
1281 return await new Promise<boolean>((resolve) => {
1282 let done = false;
1283 const tWait = Date.now();
1284 const finish = (ok: boolean) => {
1285 if (done) return;
1286 done = true;
1287 try {
1288 a.off(a.activityStartedEvent || 'activityStarted', onAny);
1289 a.off(a.activityResumedEvent || 'activityResumed', onAny);
1290 } catch {}
1291 if (VERBOSE)
1292 console.log('[hmr-client] [android] readiness result', {
1293 ok,
1294 elapsedMs: Date.now() - tWait,
1295 });
1296 resolve(ok);
1297 };
1298 const onAny = (evt?: any) => {
1299 if (VERBOSE)
1300 console.log('[hmr-client] [android] activity event', {
1301 type: evt?.eventName || 'unknown',
1302 });
1303 if (isReady()) finish(true);
1304 };
1305 try {
1306 a.on(a.activityStartedEvent || 'activityStarted', onAny);
1307 a.on(a.activityResumedEvent || 'activityResumed', onAny);
1308 if (VERBOSE) console.log('[hmr-client] [android] waiting for activity events…');
1309 } catch {
1310 return resolve(waitFor(isReady, ms, 60));
1311 }
1312 setTimeout(() => finish(isReady()), ms);
1313 });
1314 } catch {
1315 return await waitFor(isReady, ms, 60);
1316 }
1317 };
1318 const ok = await withEvents(6000);
1319 if (!ok && VERBOSE) console.warn('[hmr-client] [android] activity not ready; proceeding with best-effort');
1320 }

Callers 1

performResetRootFunction · 0.85

Calls 7

waitForFunction · 0.85
isReadyFunction · 0.70
finishFunction · 0.70
onMethod · 0.65
resolveFunction · 0.50
setTimeoutFunction · 0.50
logMethod · 0.45

Tested by

no test coverage detected