Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/app-bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,56 @@ describe("App <-> AppBridge integration", () => {
await bridgeTransport.close();
});

describe("host context lifecycle", () => {
it("retains context set before connection for the initialization handshake", async () => {
const errors: Error[] = [];
bridge.onerror = (error) => errors.push(error);

bridge.setHostContext({ theme: "dark", locale: "en-US" });
await flush();
await bridge.connect(bridgeTransport);
await app.connect(appTransport);

expect(app.getHostContext()).toEqual({ theme: "dark", locale: "en-US" });
expect(errors).toEqual([]);
});

it("retains context set after disconnect for the next view", async () => {
const errors: Error[] = [];
bridge.onerror = (error) => errors.push(error);
await bridge.connect(bridgeTransport);
await app.connect(appTransport);
await app.close();

bridge.setHostContext({ theme: "light" });
await flush();

[appTransport, bridgeTransport] = InMemoryTransport.createLinkedPair();
app = new App(testAppInfo, {}, { autoResize: false });
await bridge.connect(bridgeTransport);
await app.connect(appTransport);

expect(app.getHostContext()).toEqual({ theme: "light" });
expect(errors).toEqual([]);
});

it("reports a context notification send failure through onerror", async () => {
const errors: Error[] = [];
bridge.onerror = (error) => errors.push(error);
await bridge.connect(bridgeTransport);
await app.connect(appTransport);
const failure = new Error("Context transport failed");
const send = spyOn(bridgeTransport, "send").mockRejectedValue(failure);
try {
bridge.setHostContext({ theme: "dark" });
await flush();
expect(errors).toEqual([failure]);
} finally {
send.mockRestore();
}
});
});

describe("initialization handshake", () => {
it("App.connect() triggers bridge.oninitialized", async () => {
let initializedFired = false;
Expand Down
8 changes: 7 additions & 1 deletion src/app-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,8 @@ export class AppBridge extends Protocol<BaseContext> {
* `ui/notifications/host-context-changed` notification containing only fields
* that have been added or modified. If no fields have changed, no notification is sent.
* The new context fully replaces the internal state.
* While disconnected, the context is saved for the next initialization handshake.
* Notification failures are reported through {@link onerror `onerror`}.
*
* Common use cases include notifying the view when:
* - Theme changes (light/dark mode toggle)
Expand Down Expand Up @@ -1699,7 +1701,11 @@ export class AppBridge extends Protocol<BaseContext> {
}
if (hasChanges) {
this._hostContext = hostContext;
this.sendHostContextChange(changes);
if (this.transport) {
Promise.resolve(this.sendHostContextChange(changes)).catch((error) => {
this.onerror?.(error);
});
}
}
}

Expand Down