Skip to content

Commit 0426f1b

Browse files
cliffhallclaude
andcommitted
review: gate theme observer on initialized + document seed/dep assumptions (PR #1654)
Address the claude[bot] review: - Finding #1 (applied): gate the theme+styles MutationObserver on the view's `initialized` signal, matching the container/displayMode pushes — a theme flip in the construction→handshake window no longer races ui/initialize. The factory seeds the construction-time theme, and the first post-init flip carries the current value. Added tests: pre-init flip is dropped; the live flip test now completes the handshake first. - Finding #2 (applied, comment): note in snapshotHostContext that the `displayMode: "inline"` seed assumes inline-at-open (AppsScreen always opens inline; #1568's displayMode push carries transitions). - Finding #4 (applied, comment): explain why `containerRef` is a build-effect dep yet doesn't force a rebuild (sameInputs ignores it; read lazily). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01BrihGWcrM9JGRyu41nzZYw
1 parent a03a074 commit 0426f1b

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎clients/web/src/components/elements/AppRenderer/AppRenderer.test.tsx‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ describe("AppRenderer", () => {
316316
/>,
317317
);
318318
await flushAsync();
319+
// The theme observer is gated on the view's `initialized` signal, so
320+
// complete the handshake before flipping.
321+
await act(async () => bridge.emit("initialized"));
319322
// Ignore any seeding from Mantine's own mount-time write — assert only the
320323
// flip we trigger below.
321324
bridge.sendHostContextChange.mockClear();
@@ -344,6 +347,30 @@ describe("AppRenderer", () => {
344347
}
345348
});
346349

350+
it("does not push a theme flip before the view is initialized", async () => {
351+
const bridge = createMockBridge();
352+
renderWithMantine(
353+
<AppRenderer
354+
sandboxPath="/sandbox.html"
355+
tool={tool}
356+
bridgeFactory={() => asBridge(bridge)}
357+
/>,
358+
);
359+
await flushAsync();
360+
// No `initialized` emitted — the theme observer is gated, like the
361+
// container and displayMode pushes, so a pre-handshake flip is dropped.
362+
bridge.sendHostContextChange.mockClear();
363+
await act(async () => {
364+
document.documentElement.setAttribute(
365+
"data-mantine-color-scheme",
366+
"dark",
367+
);
368+
await Promise.resolve();
369+
});
370+
expect(bridge.sendHostContextChange).not.toHaveBeenCalled();
371+
document.documentElement.removeAttribute("data-mantine-color-scheme");
372+
});
373+
347374
it("stops observing theme changes after the renderer unmounts", async () => {
348375
const bridge = createMockBridge();
349376
const { unmount } = renderWithMantine(

‎clients/web/src/components/elements/AppRenderer/AppRenderer.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ export function AppRenderer({
256256
});
257257

258258
return scheduleDispose;
259+
// `containerRef` is listed for exhaustive-deps completeness, but a change to
260+
// its identity does NOT force a rebuild: the `sameInputs` check above
261+
// ignores it, so a new ref object hits the StrictMode reuse path (the
262+
// `initialized` handler reads `containerRef?.current` lazily, so the live
263+
// ref is always used regardless). The other deps are the real rebuild keys.
259264
}, [
260265
bridgeFactory,
261266
sandboxPath,
@@ -276,7 +281,12 @@ export function AppRenderer({
276281

277282
// Theme + styles: Mantine writes the resolved scheme to
278283
// `<html data-mantine-color-scheme>`; observe that attribute and forward
279-
// changes through the live bridge.
284+
// changes through the live bridge. Gated on the view's `initialized` signal
285+
// — like the container and displayMode pushes below — so a theme flip in the
286+
// window between bridge construction and the handshake doesn't race
287+
// `ui/initialize`. Nothing is lost by waiting: the factory seeds the
288+
// construction-time theme/styles into the handshake hostContext, and the
289+
// first post-init flip carries the current value.
280290
useEffect(() => {
281291
/* v8 ignore next 5 -- SSR/non-DOM guard: MutationObserver and document are
282292
always defined under happy-dom, so this early return is unreachable in
@@ -288,6 +298,7 @@ export function AppRenderer({
288298
return;
289299
}
290300
const observer = new MutationObserver(() => {
301+
if (!initializedRef.current) return;
291302
const styles = currentStyles();
292303
void bridgeRef.current?.sendHostContextChange({
293304
theme: currentTheme(),

‎clients/web/src/components/elements/AppRenderer/hostContext.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ export function snapshotHostContext(
148148
: undefined;
149149
return {
150150
theme: currentTheme(),
151+
// Seed assumes the app opens inline. AppsScreen always mounts the renderer
152+
// inline (maximize is a later user action), so this holds today; the live
153+
// displayMode push (AppRenderer's displayMode effect, wired by #1568)
154+
// carries any subsequent inline↔fullscreen transition. If a caller ever
155+
// mounts already-maximized, thread the actual mode in here instead.
151156
displayMode: "inline",
152157
availableDisplayModes: [...availableDisplayModes],
153158
...(styles ? { styles } : {}),

0 commit comments

Comments
 (0)