Skip to content

Commit

Permalink
Support dynamic room predecessors in WidgetLayoutStore (#10326)
Browse files Browse the repository at this point in the history
* Support dynamic room predecessors in WidgetLayoutStore

* Improve TS correctness in WidgetLayoutStore

* Test to cover onNotReady to quieten SonarCloud

---------

Co-authored-by: Janne Mareike Koschinski <janne@kuschku.de>
  • Loading branch information
andybalaam and justjanne committed Mar 9, 2023
1 parent acb7dd8 commit edd8865
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/stores/widgets/WidgetLayoutStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
}>;
} = {};

private pinnedRef: string;
private layoutRef: string;
private pinnedRef: string | undefined;
private layoutRef: string | undefined;
private dynamicRef: string | undefined;

private constructor() {
super(defaultDispatcher);
Expand Down Expand Up @@ -133,22 +134,29 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
this.matrixClient?.on(RoomStateEvent.Events, this.updateRoomFromState);
this.pinnedRef = SettingsStore.watchSetting("Widgets.pinned", null, this.updateFromSettings);
this.layoutRef = SettingsStore.watchSetting("Widgets.layout", null, this.updateFromSettings);
this.dynamicRef = SettingsStore.watchSetting(
"feature_dynamic_room_predecessors",
null,
this.updateFromSettings,
);
WidgetStore.instance.on(UPDATE_EVENT, this.updateFromWidgetStore);
}

protected async onNotReady(): Promise<void> {
this.byRoom = {};

this.matrixClient?.off(RoomStateEvent.Events, this.updateRoomFromState);
SettingsStore.unwatchSetting(this.pinnedRef);
SettingsStore.unwatchSetting(this.layoutRef);
if (this.pinnedRef) SettingsStore.unwatchSetting(this.pinnedRef);
if (this.layoutRef) SettingsStore.unwatchSetting(this.layoutRef);
if (this.dynamicRef) SettingsStore.unwatchSetting(this.dynamicRef);
WidgetStore.instance.off(UPDATE_EVENT, this.updateFromWidgetStore);
}

private updateAllRooms = (): void => {
const msc3946ProcessDynamicPredecessor = SettingsStore.getValue("feature_dynamic_room_predecessors");
if (!this.matrixClient) return;
this.byRoom = {};
for (const room of this.matrixClient.getVisibleRooms()) {
for (const room of this.matrixClient.getVisibleRooms(msc3946ProcessDynamicPredecessor)) {
this.recalculateRoom(room);
}
};
Expand All @@ -168,7 +176,13 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
if (room) this.recalculateRoom(room);
};

private updateFromSettings = (settingName: string, roomId: string /* and other stuff */): void => {
private updateFromSettings = (
_settingName: string,
roomId: string | null,
_atLevel: SettingLevel,
_newValAtLevel: any,
_newVal: any,
): void => {
if (roomId) {
const room = this.matrixClient?.getRoom(roomId);
if (room) this.recalculateRoom(room);
Expand Down
42 changes: 42 additions & 0 deletions test/stores/WidgetLayoutStore-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import WidgetStore, { IApp } from "../../src/stores/WidgetStore";
import { Container, WidgetLayoutStore } from "../../src/stores/widgets/WidgetLayoutStore";
import { stubClient } from "../test-utils";
import defaultDispatcher from "../../src/dispatcher/dispatcher";
import SettingsStore from "../../src/settings/SettingsStore";

// setup test env values
const roomId = "!room:server";
Expand Down Expand Up @@ -263,4 +264,45 @@ describe("WidgetLayoutStore", () => {
]
`);
});

it("Can call onNotReady before onReady has been called", () => {
// Just to quieten SonarCloud :-(

// @ts-ignore bypass private ctor for tests
const store = new WidgetLayoutStore();
// @ts-ignore calling private method
store.onNotReady();
});

describe("when feature_dynamic_room_predecessors is not enabled", () => {
beforeAll(() => {
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
});

it("passes the flag in to getVisibleRooms", async () => {
mocked(client.getVisibleRooms).mockRestore();
mocked(client.getVisibleRooms).mockReturnValue([]);
// @ts-ignore bypass private ctor for tests
const store = new WidgetLayoutStore();
await store.start();
expect(client.getVisibleRooms).toHaveBeenCalledWith(false);
});
});

describe("when feature_dynamic_room_predecessors is enabled", () => {
beforeAll(() => {
jest.spyOn(SettingsStore, "getValue").mockImplementation(
(settingName) => settingName === "feature_dynamic_room_predecessors",
);
});

it("passes the flag in to getVisibleRooms", async () => {
mocked(client.getVisibleRooms).mockRestore();
mocked(client.getVisibleRooms).mockReturnValue([]);
// @ts-ignore bypass private ctor for tests
const store = new WidgetLayoutStore();
await store.start();
expect(client.getVisibleRooms).toHaveBeenCalledWith(true);
});
});
});

0 comments on commit edd8865

Please sign in to comment.