You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-25 05:23:13 +03:00
Make sending ContentLoaded optional for a widgetClient (#4086)
* add sendContentLoaded option to widgetClient Signed-off-by: Timo K <toger5@hotmail.de> * review Signed-off-by: Timo K <toger5@hotmail.de> * add tests Signed-off-by: Timo K <toger5@hotmail.de> * another try to get the coverage up Signed-off-by: Timo K <toger5@hotmail.de> * self review Signed-off-by: Timo K <toger5@hotmail.de> --------- Signed-off-by: Timo K <toger5@hotmail.de>
This commit is contained in:
@@ -87,9 +87,12 @@ describe("RoomWidgetClient", () => {
|
|||||||
client.stopClient();
|
client.stopClient();
|
||||||
});
|
});
|
||||||
|
|
||||||
const makeClient = async (capabilities: ICapabilities): Promise<void> => {
|
const makeClient = async (
|
||||||
|
capabilities: ICapabilities,
|
||||||
|
sendContentLoaded: boolean | undefined = undefined,
|
||||||
|
): Promise<void> => {
|
||||||
const baseUrl = "https://example.org";
|
const baseUrl = "https://example.org";
|
||||||
client = createRoomWidgetClient(widgetApi, capabilities, "!1:example.org", { baseUrl });
|
client = createRoomWidgetClient(widgetApi, capabilities, "!1:example.org", { baseUrl }, sendContentLoaded);
|
||||||
expect(widgetApi.start).toHaveBeenCalled(); // needs to have been called early in order to not miss messages
|
expect(widgetApi.start).toHaveBeenCalled(); // needs to have been called early in order to not miss messages
|
||||||
widgetApi.emit("ready");
|
widgetApi.emit("ready");
|
||||||
await client.startClient();
|
await client.startClient();
|
||||||
@@ -143,7 +146,7 @@ describe("RoomWidgetClient", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("messages", () => {
|
describe("initialization", () => {
|
||||||
it("requests permissions for specific message types", async () => {
|
it("requests permissions for specific message types", async () => {
|
||||||
await makeClient({ sendMessage: [MsgType.Text], receiveMessage: [MsgType.Text] });
|
await makeClient({ sendMessage: [MsgType.Text], receiveMessage: [MsgType.Text] });
|
||||||
expect(widgetApi.requestCapabilityForRoomTimeline).toHaveBeenCalledWith("!1:example.org");
|
expect(widgetApi.requestCapabilityForRoomTimeline).toHaveBeenCalledWith("!1:example.org");
|
||||||
@@ -158,6 +161,15 @@ describe("RoomWidgetClient", () => {
|
|||||||
expect(widgetApi.requestCapabilityToReceiveMessage).toHaveBeenCalledWith();
|
expect(widgetApi.requestCapabilityToReceiveMessage).toHaveBeenCalledWith();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("sends content loaded when configured", async () => {
|
||||||
|
await makeClient({});
|
||||||
|
expect(widgetApi.sendContentLoaded).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not sent content loaded when configured", async () => {
|
||||||
|
await makeClient({}, false);
|
||||||
|
expect(widgetApi.sendContentLoaded).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
// No point in testing sending and receiving since it's done exactly the
|
// No point in testing sending and receiving since it's done exactly the
|
||||||
// same way as non-message events
|
// same way as non-message events
|
||||||
});
|
});
|
||||||
@@ -305,12 +317,14 @@ describe("RoomWidgetClient", () => {
|
|||||||
expect(await emittedSync).toEqual(SyncState.Syncing);
|
expect(await emittedSync).toEqual(SyncState.Syncing);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("oidc token", () => {
|
describe("oidc token", () => {
|
||||||
it("requests an oidc token", async () => {
|
it("requests an oidc token", async () => {
|
||||||
await makeClient({});
|
await makeClient({});
|
||||||
expect(await client.getOpenIdToken()).toStrictEqual(testOIDCToken);
|
expect(await client.getOpenIdToken()).toStrictEqual(testOIDCToken);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("gets TURN servers", async () => {
|
it("gets TURN servers", async () => {
|
||||||
const server1: ITurnServer = {
|
const server1: ITurnServer = {
|
||||||
uris: [
|
uris: [
|
||||||
|
|||||||
@@ -108,11 +108,22 @@ export class RoomWidgetClient extends MatrixClient {
|
|||||||
private lifecycle?: AbortController;
|
private lifecycle?: AbortController;
|
||||||
private syncState: SyncState | null = null;
|
private syncState: SyncState | null = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param widgetApi - The widget api to use for communication.
|
||||||
|
* @param capabilities - The capabilities the widget client will request.
|
||||||
|
* @param roomId - The room id the widget is associated with.
|
||||||
|
* @param opts - The configuration options for this client.
|
||||||
|
* @param sendContentLoaded - Whether to send a content loaded widget action immediately after initial setup.
|
||||||
|
* Set to `false` if the widget uses `waitForIFrameLoad=true` (in this case the client does not expect a content loaded action at all),
|
||||||
|
* or if the the widget wants to send the `ContentLoaded` action at a later point in time after the initial setup.
|
||||||
|
*/
|
||||||
public constructor(
|
public constructor(
|
||||||
private readonly widgetApi: WidgetApi,
|
private readonly widgetApi: WidgetApi,
|
||||||
private readonly capabilities: ICapabilities,
|
private readonly capabilities: ICapabilities,
|
||||||
private readonly roomId: string,
|
private readonly roomId: string,
|
||||||
opts: IMatrixClientCreateOpts,
|
opts: IMatrixClientCreateOpts,
|
||||||
|
sendContentLoaded: boolean,
|
||||||
) {
|
) {
|
||||||
super(opts);
|
super(opts);
|
||||||
|
|
||||||
@@ -165,7 +176,7 @@ export class RoomWidgetClient extends MatrixClient {
|
|||||||
// does *not* (yes, that is the right way around) wait for this event. Let's
|
// does *not* (yes, that is the right way around) wait for this event. Let's
|
||||||
// start sending this, then once this has rolled out, we can change element-web to
|
// start sending this, then once this has rolled out, we can change element-web to
|
||||||
// use waitForIFrameLoad=false and have a widget API that's less racy.
|
// use waitForIFrameLoad=false and have a widget API that's less racy.
|
||||||
widgetApi.sendContentLoaded();
|
if (sendContentLoaded) widgetApi.sendContentLoaded();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async startClient(opts: IStartClientOpts = {}): Promise<void> {
|
public async startClient(opts: IStartClientOpts = {}): Promise<void> {
|
||||||
|
|||||||
@@ -164,11 +164,26 @@ export function createClient(opts: ICreateClientOpts): MatrixClient {
|
|||||||
return new MatrixClient(amendClientOpts(opts));
|
return new MatrixClient(amendClientOpts(opts));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a Matrix Client that works in a widget.
|
||||||
|
* This client has a subset of features compared to a full client.
|
||||||
|
* It uses the widget-api to communicate with matrix. (widget \<-\> client \<-\> homeserver)
|
||||||
|
* @returns A new matrix client with a subset of features.
|
||||||
|
* @param opts - The configuration options for this client. These configuration
|
||||||
|
* options will be passed directly to {@link MatrixClient}.
|
||||||
|
* @param widgetApi - The widget api to use for communication.
|
||||||
|
* @param capabilities - The capabilities the widget client will request.
|
||||||
|
* @param roomId - The room id the widget is associated with.
|
||||||
|
* @param sendContentLoaded - Whether to send a content loaded widget action immediately after initial setup.
|
||||||
|
* Set to `false` if the widget uses `waitForIFrameLoad=true` (in this case the client does not expect a content loaded action at all),
|
||||||
|
* or if the the widget wants to send the `ContentLoaded` action at a later point in time after the initial setup.
|
||||||
|
*/
|
||||||
export function createRoomWidgetClient(
|
export function createRoomWidgetClient(
|
||||||
widgetApi: WidgetApi,
|
widgetApi: WidgetApi,
|
||||||
capabilities: ICapabilities,
|
capabilities: ICapabilities,
|
||||||
roomId: string,
|
roomId: string,
|
||||||
opts: ICreateClientOpts,
|
opts: ICreateClientOpts,
|
||||||
|
sendContentLoaded = true,
|
||||||
): MatrixClient {
|
): MatrixClient {
|
||||||
return new RoomWidgetClient(widgetApi, capabilities, roomId, amendClientOpts(opts));
|
return new RoomWidgetClient(widgetApi, capabilities, roomId, amendClientOpts(opts), sendContentLoaded);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user