From 98c6e5747be366bf3e873de379be4327f868f56b Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Wed, 8 Mar 2023 13:38:27 +0000 Subject: [PATCH 1/3] Support dynamic room predecessors in WidgetLayoutStore --- src/stores/widgets/WidgetLayoutStore.ts | 10 +++++++- test/stores/WidgetLayoutStore-test.ts | 33 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/stores/widgets/WidgetLayoutStore.ts b/src/stores/widgets/WidgetLayoutStore.ts index 311c7e7c2ab..2bc460a1fa1 100644 --- a/src/stores/widgets/WidgetLayoutStore.ts +++ b/src/stores/widgets/WidgetLayoutStore.ts @@ -106,6 +106,7 @@ export class WidgetLayoutStore extends ReadyWatchingStore { private pinnedRef: string; private layoutRef: string; + private dynamicRef: string; 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); } @@ -142,13 +148,15 @@ export class WidgetLayoutStore extends ReadyWatchingStore { this.matrixClient?.off(RoomStateEvent.Events, this.updateRoomFromState); SettingsStore.unwatchSetting(this.pinnedRef); SettingsStore.unwatchSetting(this.layoutRef); + 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); } }; diff --git a/test/stores/WidgetLayoutStore-test.ts b/test/stores/WidgetLayoutStore-test.ts index 81f373d0f69..e2d79231c8d 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,36 @@ describe("WidgetLayoutStore", () => { ] `); }); + + 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); + }); + }); }); From faefdd1c6ba3641bb662ddb7841df014ecbc1b05 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Wed, 8 Mar 2023 14:36:49 +0000 Subject: [PATCH 2/3] Improve TS correctness in WidgetLayoutStore --- src/stores/widgets/WidgetLayoutStore.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/stores/widgets/WidgetLayoutStore.ts b/src/stores/widgets/WidgetLayoutStore.ts index 2bc460a1fa1..fa1c5d6dac3 100644 --- a/src/stores/widgets/WidgetLayoutStore.ts +++ b/src/stores/widgets/WidgetLayoutStore.ts @@ -104,9 +104,9 @@ export class WidgetLayoutStore extends ReadyWatchingStore { }>; } = {}; - private pinnedRef: string; - private layoutRef: string; - private dynamicRef: string; + private pinnedRef: string | undefined; + private layoutRef: string | undefined; + private dynamicRef: string | undefined; private constructor() { super(defaultDispatcher); @@ -146,9 +146,9 @@ export class WidgetLayoutStore extends ReadyWatchingStore { this.byRoom = {}; this.matrixClient?.off(RoomStateEvent.Events, this.updateRoomFromState); - SettingsStore.unwatchSetting(this.pinnedRef); - SettingsStore.unwatchSetting(this.layoutRef); - SettingsStore.unwatchSetting(this.dynamicRef); + 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); } @@ -176,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); From c05a500b7a191f6d8489c0c8abf76158608dc18d Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Wed, 8 Mar 2023 15:25:07 +0000 Subject: [PATCH 3/3] Test to cover onNotReady to quieten SonarCloud --- test/stores/WidgetLayoutStore-test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/stores/WidgetLayoutStore-test.ts b/test/stores/WidgetLayoutStore-test.ts index e2d79231c8d..ddffe66ae9a 100644 --- a/test/stores/WidgetLayoutStore-test.ts +++ b/test/stores/WidgetLayoutStore-test.ts @@ -265,6 +265,15 @@ 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);