From c57d8463b9d394a22cd39d2ada0501d457034ab8 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 17 Jul 2024 10:58:07 +0100 Subject: [PATCH] Clear settings store cache on logout (#12786) And clear it in the widget layout store test, also fixing the tests which didn't actually individually as they actually relied on the state bleeding between tests. --- src/Lifecycle.ts | 1 + src/settings/SettingsStore.ts | 6 +++++ src/settings/handlers/LocalEchoWrapper.ts | 5 +++++ src/settings/handlers/SettingsHandler.ts | 5 +++++ test/stores/WidgetLayoutStore-test.ts | 27 ++++++++++++----------- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Lifecycle.ts b/src/Lifecycle.ts index f851ddddf6..e2141cd94d 100644 --- a/src/Lifecycle.ts +++ b/src/Lifecycle.ts @@ -1056,6 +1056,7 @@ export async function onLoggedOut(): Promise { await clearStorage({ deleteEverything: true }); LifecycleCustomisations.onLoggedOutAndStorageCleared?.(); await PlatformPeg.get()?.clearStorage(); + SettingsStore.reset(); // Do this last, so we can make sure all storage has been cleared and all // customisations got the memo. diff --git a/src/settings/SettingsStore.ts b/src/settings/SettingsStore.ts index a746485368..6e3e9e3e1f 100644 --- a/src/settings/SettingsStore.ts +++ b/src/settings/SettingsStore.ts @@ -134,6 +134,12 @@ export default class SettingsStore { // Counter used for generation of watcher IDs private static watcherCount = 1; + public static reset(): void { + for (const handler of Object.values(LEVEL_HANDLERS)) { + handler.reset(); + } + } + /** * Gets all the feature-style setting names. * @returns {string[]} The names of the feature settings. diff --git a/src/settings/handlers/LocalEchoWrapper.ts b/src/settings/handlers/LocalEchoWrapper.ts index ea216710e7..39724e5bc2 100644 --- a/src/settings/handlers/LocalEchoWrapper.ts +++ b/src/settings/handlers/LocalEchoWrapper.ts @@ -83,4 +83,9 @@ export default class LocalEchoWrapper extends SettingsHandler { public isSupported(): boolean { return this.handler.isSupported(); } + + public reset(): void { + this.cache = {}; + this.handler.reset(); + } } diff --git a/src/settings/handlers/SettingsHandler.ts b/src/settings/handlers/SettingsHandler.ts index d9cfdaca1d..db0ec7d730 100644 --- a/src/settings/handlers/SettingsHandler.ts +++ b/src/settings/handlers/SettingsHandler.ts @@ -61,4 +61,9 @@ export default abstract class SettingsHandler { * @returns {boolean} True if this level is supported on the current device. */ public abstract isSupported(): boolean; + + /** + * Resets the handler, clearing any caches or other stored data. Called on user logout. + */ + public reset(): void {} } diff --git a/test/stores/WidgetLayoutStore-test.ts b/test/stores/WidgetLayoutStore-test.ts index ddffe66ae9..17fbf9f2fa 100644 --- a/test/stores/WidgetLayoutStore-test.ts +++ b/test/stores/WidgetLayoutStore-test.ts @@ -57,6 +57,8 @@ describe("WidgetLayoutStore", () => { off: jest.fn(), getApps: () => mockApps, } as unknown as WidgetStore); + + SettingsStore.reset(); }); beforeAll(() => { @@ -156,9 +158,14 @@ describe("WidgetLayoutStore", () => { await store.start(); expect(roomUpdateListener).toHaveBeenCalled(); - expect(store.getContainerWidgets(mockRoom, Container.Top)).toEqual([mockApps[0]]); + expect(store.getContainerWidgets(mockRoom, Container.Top)).toEqual([]); expect(store.getContainerWidgets(mockRoom, Container.Center)).toEqual([]); - expect(store.getContainerWidgets(mockRoom, Container.Right)).toEqual([mockApps[1], mockApps[2], mockApps[3]]); + expect(store.getContainerWidgets(mockRoom, Container.Right)).toEqual([ + mockApps[0], + mockApps[1], + mockApps[2], + mockApps[3], + ]); }); it("should clear the layout and emit an update if there are no longer apps in the room", () => { @@ -238,21 +245,15 @@ describe("WidgetLayoutStore", () => { "widgets": { "1": { "container": "top", - "height": 23, - "index": 2, - "width": 64, + "height": undefined, + "index": 0, + "width": 100, }, "2": { - "container": "top", - "height": 23, - "index": 0, - "width": 10, + "container": "right", }, "3": { - "container": "top", - "height": 23, - "index": 1, - "width": 26, + "container": "right", }, "4": { "container": "right",