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;
}