diff --git a/src/stores/widgets/WidgetLayoutStore.ts b/src/stores/widgets/WidgetLayoutStore.ts index 311c7e7c2a..fa1c5d6dac 100644 --- a/src/stores/widgets/WidgetLayoutStore.ts +++ b/src/stores/widgets/WidgetLayoutStore.ts @@ -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); @@ -133,6 +134,11 @@ 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); } @@ -140,15 +146,17 @@ export class WidgetLayoutStore extends ReadyWatchingStore { 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); } }; @@ -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); diff --git a/test/stores/WidgetLayoutStore-test.ts b/test/stores/WidgetLayoutStore-test.ts index 81f373d0f6..ddffe66ae9 100644 --- a/test/stores/WidgetLayoutStore-test.ts +++ b/test/stores/WidgetLayoutStore-test.ts @@ -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"; @@ -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); + }); + }); });