Wait for the Even app host before choosing browser-preview mode

Found on the first real-phone run: the Even app's WebView provided
window.flutter_inappwebview after the page's script had started, so the
page wrongly fell back to browser-preview mode and never created the HUD
page on the glasses. The page now waits (up to 6 s, polling and listening
for the WebView's platform-ready event) and, if the host appears later
still, reloads once into the real mode. Diagnostics show how long
detection took and the browser's user agent.

Tests: 205 passed (7 new); simulator 6 passed. Not reviewed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-21 12:14:39 -08:00
co-authored by Claude Fable 5.2
parent 98c7b03aa7
commit e5559dc5e7
4 changed files with 199 additions and 4 deletions
+79
View File
@@ -0,0 +1,79 @@
/**
* Decide whether this page is running inside the Even app.
*
* The Even app's WebView provides `window.flutter_inappwebview.callHandler`, but not
* necessarily before the page's own script starts: on a real phone it appeared after our
* module had already run (first device test, 2026-11-21), although the simulator provides it
* at once. So the page waits for it, and also listens for the WebView's own "platform ready"
* event, before concluding it is in an ordinary browser.
*/
export interface HostWindow {
flutter_inappwebview?: { callHandler?: unknown };
addEventListener(type: string, listener: () => void): void;
removeEventListener(type: string, listener: () => void): void;
}
export interface HostDetection {
found: boolean;
/** How long the page waited before deciding. */
waitedMs: number;
}
export const HOST_WAIT_MS = 6000;
/** The event flutter_inappwebview dispatches on `window` once its JavaScript bridge is usable. */
export const PLATFORM_READY_EVENT = 'flutterInAppWebViewPlatformReady';
export function hostPresent(win: HostWindow): boolean {
return typeof win.flutter_inappwebview?.callHandler === 'function';
}
export function waitForEvenHost(
win: HostWindow,
options: { timeoutMs?: number; pollMs?: number; now?: () => number } = {},
): Promise<HostDetection> {
const timeoutMs = options.timeoutMs ?? HOST_WAIT_MS;
const pollMs = options.pollMs ?? 50;
const now = options.now ?? Date.now;
const started = now();
return new Promise((resolve) => {
let done = false;
const finish = (found: boolean) => {
if (done) return;
done = true;
clearInterval(poll);
clearTimeout(deadline);
win.removeEventListener(PLATFORM_READY_EVENT, onReady);
resolve({ found, waitedMs: now() - started });
};
const check = () => {
if (hostPresent(win)) finish(true);
};
// The event alone is not proof (anything can dispatch it); it only prompts a re-check.
const onReady = () => check();
const poll = setInterval(check, pollMs);
const deadline = setTimeout(() => finish(hostPresent(win)), timeoutMs);
win.addEventListener(PLATFORM_READY_EVENT, onReady);
check();
});
}
/**
* After the page has settled on browser-preview mode, keep watching: if the Even host turns
* up after all, call `onLate` once (the page reloads itself into the real mode).
*/
export function watchForLateHost(win: HostWindow, onLate: () => void, pollMs = 500): () => void {
let fired = false;
const check = () => {
if (fired || !hostPresent(win)) return;
fired = true;
stop();
onLate();
};
const timer = setInterval(check, pollMs);
win.addEventListener(PLATFORM_READY_EVENT, check);
const stop = () => {
clearInterval(timer);
win.removeEventListener(PLATFORM_READY_EVENT, check);
};
return stop;
}
+25 -4
View File
@@ -2,6 +2,7 @@ import { waitForEvenAppBridge } from '@evenrealities/even_hub_sdk';
import { measureTextWrap } from '@evenrealities/pretext';
import { Controller } from './core/controller.ts';
import { EvenDevice } from './device/evenAdapter.ts';
import { waitForEvenHost, watchForLateHost, type HostWindow } from './device/hostDetect.ts';
import { BrowserPreviewDevice } from './device/previewDevice.ts';
import type { DevicePort } from './device/port.ts';
import { mountPhoneUi } from './ui/phone.ts';
@@ -13,15 +14,35 @@ import './ui/phone.css';
* bridge, so a clearly-labelled preview device is used instead: it has no
* microphone and proves nothing about hardware.
*/
const RELOAD_FLAG = 'hermeven.reloadedForHost';
async function resolveDevice(): Promise<{ device: DevicePort; runtime: string }> {
const hasHost = typeof (window as { flutter_inappwebview?: unknown }).flutter_inappwebview !== 'undefined';
if (hasHost) {
const host = window as unknown as HostWindow;
// The Even app can provide its connection a moment after the page starts, so wait for it
// rather than deciding on the first instant.
const detection = await waitForEvenHost(host);
if (detection.found) {
const bridge = await waitForEvenAppBridge();
return { device: new EvenDevice(bridge), runtime: 'Even app host (SDK 0.0.16)' };
return { device: new EvenDevice(bridge), runtime: `Even app host (SDK 0.0.16), found after ${detection.waitedMs} ms` };
}
const preview = document.getElementById('preview')!;
preview.hidden = false;
return { device: new BrowserPreviewDevice(preview), runtime: 'Browser preview: no glasses, no microphone' };
// If the host appears later still, reload once into the real mode (once, so a host that
// comes and goes cannot put the page in a reload loop).
watchForLateHost(host, () => {
let already = false;
try {
already = sessionStorage.getItem(RELOAD_FLAG) === '1';
sessionStorage.setItem(RELOAD_FLAG, '1');
} catch {
already = true;
}
if (!already) location.reload();
});
return {
device: new BrowserPreviewDevice(preview),
runtime: `Browser preview: no glasses, no microphone (no Even host after ${detection.waitedMs} ms)`,
};
}
const { device, runtime } = await resolveDevice();
+1
View File
@@ -196,6 +196,7 @@ export function mountPhoneUi(root: HTMLElement, controller: Controller, defaults
rows(diagList, [
['Runtime', defaults.runtime],
['Page origin', location.origin],
['Browser', navigator.userAgent],
['HUD page', s.hudReady ? 'created' : 'not created'],
['Device id', s.deviceId || 'none'],
['Conversation id', s.state.conversationId ?? 'none'],