Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Support dynamic room predecessors in WidgetLayoutStore #10326

Merged
Merged
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
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);
});
});
});