mirror of
https://github.com/vector-im/element-web.git
synced 2024-11-15 12:45:11 +08:00
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 <janne@kuschku.de>
This commit is contained in:
parent
acb7dd84ac
commit
edd8865670
@ -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);
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user