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

Commit edd8865

Browse files
Support dynamic room predecessors in WidgetLayoutStore (#10326)
* Support dynamic room predecessors in WidgetLayoutStore * Improve TS correctness in WidgetLayoutStore * Test to cover onNotReady to quieten SonarCloud --------- Co-authored-by: Janne Mareike Koschinski <[email protected]>
1 parent acb7dd8 commit edd8865

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

src/stores/widgets/WidgetLayoutStore.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
104104
}>;
105105
} = {};
106106

107-
private pinnedRef: string;
108-
private layoutRef: string;
107+
private pinnedRef: string | undefined;
108+
private layoutRef: string | undefined;
109+
private dynamicRef: string | undefined;
109110

110111
private constructor() {
111112
super(defaultDispatcher);
@@ -133,22 +134,29 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
133134
this.matrixClient?.on(RoomStateEvent.Events, this.updateRoomFromState);
134135
this.pinnedRef = SettingsStore.watchSetting("Widgets.pinned", null, this.updateFromSettings);
135136
this.layoutRef = SettingsStore.watchSetting("Widgets.layout", null, this.updateFromSettings);
137+
this.dynamicRef = SettingsStore.watchSetting(
138+
"feature_dynamic_room_predecessors",
139+
null,
140+
this.updateFromSettings,
141+
);
136142
WidgetStore.instance.on(UPDATE_EVENT, this.updateFromWidgetStore);
137143
}
138144

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

142148
this.matrixClient?.off(RoomStateEvent.Events, this.updateRoomFromState);
143-
SettingsStore.unwatchSetting(this.pinnedRef);
144-
SettingsStore.unwatchSetting(this.layoutRef);
149+
if (this.pinnedRef) SettingsStore.unwatchSetting(this.pinnedRef);
150+
if (this.layoutRef) SettingsStore.unwatchSetting(this.layoutRef);
151+
if (this.dynamicRef) SettingsStore.unwatchSetting(this.dynamicRef);
145152
WidgetStore.instance.off(UPDATE_EVENT, this.updateFromWidgetStore);
146153
}
147154

148155
private updateAllRooms = (): void => {
156+
const msc3946ProcessDynamicPredecessor = SettingsStore.getValue("feature_dynamic_room_predecessors");
149157
if (!this.matrixClient) return;
150158
this.byRoom = {};
151-
for (const room of this.matrixClient.getVisibleRooms()) {
159+
for (const room of this.matrixClient.getVisibleRooms(msc3946ProcessDynamicPredecessor)) {
152160
this.recalculateRoom(room);
153161
}
154162
};
@@ -168,7 +176,13 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
168176
if (room) this.recalculateRoom(room);
169177
};
170178

171-
private updateFromSettings = (settingName: string, roomId: string /* and other stuff */): void => {
179+
private updateFromSettings = (
180+
_settingName: string,
181+
roomId: string | null,
182+
_atLevel: SettingLevel,
183+
_newValAtLevel: any,
184+
_newVal: any,
185+
): void => {
172186
if (roomId) {
173187
const room = this.matrixClient?.getRoom(roomId);
174188
if (room) this.recalculateRoom(room);

test/stores/WidgetLayoutStore-test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import WidgetStore, { IApp } from "../../src/stores/WidgetStore";
2121
import { Container, WidgetLayoutStore } from "../../src/stores/widgets/WidgetLayoutStore";
2222
import { stubClient } from "../test-utils";
2323
import defaultDispatcher from "../../src/dispatcher/dispatcher";
24+
import SettingsStore from "../../src/settings/SettingsStore";
2425

2526
// setup test env values
2627
const roomId = "!room:server";
@@ -263,4 +264,45 @@ describe("WidgetLayoutStore", () => {
263264
]
264265
`);
265266
});
267+
268+
it("Can call onNotReady before onReady has been called", () => {
269+
// Just to quieten SonarCloud :-(
270+
271+
// @ts-ignore bypass private ctor for tests
272+
const store = new WidgetLayoutStore();
273+
// @ts-ignore calling private method
274+
store.onNotReady();
275+
});
276+
277+
describe("when feature_dynamic_room_predecessors is not enabled", () => {
278+
beforeAll(() => {
279+
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
280+
});
281+
282+
it("passes the flag in to getVisibleRooms", async () => {
283+
mocked(client.getVisibleRooms).mockRestore();
284+
mocked(client.getVisibleRooms).mockReturnValue([]);
285+
// @ts-ignore bypass private ctor for tests
286+
const store = new WidgetLayoutStore();
287+
await store.start();
288+
expect(client.getVisibleRooms).toHaveBeenCalledWith(false);
289+
});
290+
});
291+
292+
describe("when feature_dynamic_room_predecessors is enabled", () => {
293+
beforeAll(() => {
294+
jest.spyOn(SettingsStore, "getValue").mockImplementation(
295+
(settingName) => settingName === "feature_dynamic_room_predecessors",
296+
);
297+
});
298+
299+
it("passes the flag in to getVisibleRooms", async () => {
300+
mocked(client.getVisibleRooms).mockRestore();
301+
mocked(client.getVisibleRooms).mockReturnValue([]);
302+
// @ts-ignore bypass private ctor for tests
303+
const store = new WidgetLayoutStore();
304+
await store.start();
305+
expect(client.getVisibleRooms).toHaveBeenCalledWith(true);
306+
});
307+
});
266308
});

0 commit comments

Comments
 (0)