Return the widget from initializeWidget instead of assigning a global

Element Call reached the widget API through a mutable module-level
binding, which every consumer imported directly. Nothing outside the app
shell needs it any more, so hand it back from initializeWidget and thread
it through: the initializer returns it, main passes it to App, and App
uses it to build the host bridge and to await the client the host is
lending us.

ClientContext's loadClient is now only about restoring or creating a
session of Element Call's own, since a widget's client arrives as a prop
like any other host's would.

Also fixes an early return added in the previous commit, which skipped
starting the analytics settings listener when a client was supplied. That
was harmless until now, but would have stopped analytics following the
user's choices in widget mode.

sdk/main.ts asked the host to close by hand; it now uses the bridge, which
also stops the transport as the app does.
This commit is contained in:
Valere
2026-09-02 16:56:09 +02:00
parent f6f47ede62
commit 182be8676d
7 changed files with 84 additions and 72 deletions
+12 -12
View File
@@ -58,21 +58,21 @@ export interface WidgetHelpers {
}
/**
* A point of access to the widget API, if the app is running as a widget. This
* is initialized with `initializeWidget`. This should happen at the top level because the widget messaging
* needs to be set up ASAP on load to ensure it doesn't miss any requests.
*/
export let widget: WidgetHelpers | null = null;
/**
* Should be called as soon as possible on app start. (In the initilizer before react)
* Connects to the widget API, if Element Call is running as a widget.
*
* Should be called as soon as possible on app start (in the initializer, before
* React), because the widget messaging needs to be set up ASAP on load to
* ensure it doesn't miss any requests.
*
* @returns A point of access to the widget API, or null if Element Call is not
* running as a widget.
*/
// this needs to be a seperate call and cannot be done on import to allow us to spy on methods in here before
// execution.
export const initializeWidget = (
rtcApplication: string = "m.call",
sendRoomEvents = false,
): void => {
): WidgetHelpers | null => {
try {
const {
widgetId,
@@ -202,14 +202,14 @@ export const initializeWidget = (
return client;
};
widget = { api, lazyActions, client: clientPromise() };
return { api, lazyActions, client: clientPromise() };
} else {
if (import.meta.env.MODE !== "test")
logger.info("No widget API available");
widget = null;
return null;
}
} catch (e) {
logger.warn("Continuing without the widget API", e);
widget = null;
return null;
}
};